2016-11-11 26 views
0

我的宏的目的是讓用戶在他們的模型中選擇他們想檢查硬編碼的範圍。宏然後在彙總表上打印工作表,單元格地址和硬代碼的值。如果您僅從一張紙上進行選擇,該宏目前效果很好;但是,如果將選擇擴展到多個工作表,宏將創建多個工作表,而不是僅打算執行一個工作表。預先感謝您的時間和幫助VBA - 防止添加多張表格

Set RngCon = Selection.SpecialCells(xlCellTypeConstants, xlNumbers) 

Set SumWS = Worksheets.Add 
Username = InputBox("Please create a name for the output sheet (i.e. Whs Industry Hard Codes)") 
SumWS.Name = Username 

x = 1 
    SumWS.Cells(x, 1) = "Worksheet" 
    SumWS.Cells(x, 2) = "Address" 
    SumWS.Cells(x, 3) = "Value" 

For Each c In RngCon 
    x = x + 1 
    SumWS.Cells(x, 1) = c.Worksheet.Name 
    SumWS.Cells(x, 2) = c.Address(False, False) 
    SumWS.Cells(x, 3) = c.Value 
Next c 

回答

0

你可以做這樣的事情:

Sub test() 

    Dim SumWS As Worksheet 
    Dim ws As Worksheet 
    Dim SelectedSheets() As String 
    Dim n As Long 
    Dim i As Long 

    n = 0 
    For Each ws In ActiveWindow.SelectedSheets 
     ReDim Preserve SelectedSheets(n) 
     SelectedSheets(n) = ws.Name 
     n = n + 1 
    Next 

    Sheets(SelectedSheets(0)).Select 
    Set SumWS = Worksheets.Add 

    Debug.Print "Sum Sheet: " & SumWS.Name 
    For i = LBound(SelectedSheets) To UBound(SelectedSheets) 
     Debug.Print "Selected Sheet #" & i & ": " & SelectedSheets(i) 
    Next i 
End Sub 

在第一個爲你保存選定表中的數組。然後,您可以選擇一個特定工作表並添加總表。第二個顯示如何處理存儲的信息。您可以循環選定的工作表以獲取所有值,並根據需要再次選擇它們。

學分亞洲時報Siddharth潰敗(Similar case