2015-04-05 52 views
1

我有一個包含50多個工作表的Excel文檔,所有工作表都有相似的命名約定。 因爲這對用戶來說非常不友好,所以我寫了一個VBA宏,它創建了一個名爲summary的工作表,其中所有工作表的列表都以表格形式超鏈接,並以Sheet AB和C作爲Column和Sheet 1和2行。如何在Excel的每個工作表中搜索特定列

現在我試圖通過表1和表2中特定列的每一行,並查找任何對SheetB,SheetC和SheetD的引用,並查找找到的每個引用,並且我想標記創建矩陣。

我不知道如何做到這一點。任何援助將不勝感激。

我已成功搜索Sheet 1和2以獲取SheetB的任何引用,如下所示,但我不確定如何更新彙總表中的相應單元格。

Function findWord(word As String, wSheet As String) As Boolean 

    Dim LastRow As Long 
    Dim i As Long 
    LastRow = Worksheets(wSheet).Cells(Rows.Count, "D").End(xlUp).Row 

    For i = LastRow To 1 Step -1 
     If Worksheets(wSheet).Range("D" & i).Value = word Then 
      findWord = True 
      Exit Function 
     End If 
    Next i 
End Function 


For Each wsSheet In wbBook.Worksheets 
    If (wsSheet.Name <> wsActive.Name) And (Left(wsSheet.Name, 4) <> "fact") Then 
    For i = 2 To lastColumn 
     MsgBox wsSheet.Name 
     If findWord(columnNames(counter2), wsSheet.Name) Then 
      'Update summary sheet 
     End If 
     counter = counter2 + 1 
    Next i 

End If 
Next wsSheet 
+2

我不清楚,你是什麼意思*「我想標記」*。至於搜索所有的出現,你可以在'For Each mySheet in worksheets'中使用'Do while loop'和'Find'方法。但是你必須提出一些你的試驗,並且在你遇到問題的時候更加具體。 – 2015-04-05 09:13:02

+1

謝謝。當我說「我想標記」時,我的意思是我想在該行中添加一個X.例如,我在Sheet1中發現了一個包含SheetA的字符串,然後將X添加到我的彙總表中。最後,我的彙總表應該看起來像是非十字架(如果有意義的話)。 – isaiah 2015-04-06 08:28:34

回答

1

如果結果在「彙總表」你正在尋找的是與此類似:
Table on Summary sheet

然後你可以使用這樣的事情(閱讀說明裏面的代碼的註釋)

Sub MarkReferencesToSheets() 

    Dim wsSummary As Worksheet 'sheet with summary table matrix 
    Dim wsSheetRow As Worksheet 'sheets in which we will search references to other sheets 
    Dim strSheetColumnName As String 'name of the reference we are looking for 
    Dim intSheetRow As Integer 'for loop purposes 
    Dim intSheetColumn As Integer 'for loop purposes 

    Set wsSummary = Sheets("Summary") 

    For intSheetRow = 2 To 3 'change to suit; headers for rows in summary sheet 
     Set wsSheetRow = Worksheets(wsSummary.Cells(intSheetRow, 1).Value) 
     For intSheetColumn = 2 To 4 'change to suit; headers for columns in summary sheet 
      strSheetColumnName = wsSummary.Cells(1, intSheetColumn) 'name of sheet we are looking for 
      If Not wsSheetRow.Columns(4).Find(strSheetColumnName) Is Nothing Then 'look only in column "D", or 4 
       wsSummary.Cells(intSheetRow, intSheetColumn) = "X" ' if we found it, mark it 
      Else 
       'if you want something else in the cell when reference is not found, put it here 
      End If 
     Next intSheetColumn 
    Next intSheetRow 

End Sub 
+0

謝謝Branislav。這工作!是的,這正是我所尋找的。 – isaiah 2015-04-06 16:47:07

相關問題