首先感謝大家,您一直以來都非常支持使用我非常幼稚的VBA代碼來塑造我。隨着我的進步,我遇到了新的問題。使用VBA查找內容:=在表格中檢索值
我的挑戰是我正在使用此代碼在所有打開的工作簿中查找值。如果找到了,剩下的代碼就會執行。我希望我的最終用戶能夠輕易改變所發現的價值,而不必深入代碼。所以,我添加了一張名爲Data Specs
的ThisWorkbook
。我希望當我的代碼開始發現某些東西時,它應該查找單元格A2
中此表中的值。
Sheets("Add File Here").Select
If IsEmpty(Range("A1")) Then
Worksheets("Master Mapper").Activate
Dim answer003 As Integer
answer003 = MsgBox("Please check the Data Sheet. No value found in first row! Do you wish to find Cvent003 file in open workbooks and start process?", vbYesNo + vbQuestion, "Review & Proceed")
If answer003 = vbYes Then
'Starts here
Dim wSheet As Worksheet
Dim wBook As Workbook
Dim rFound As Range
Dim bFound As Boolean
Dim lngLastRow2 As Long
Dim Cvent003Val As Long
Cvent003Val = ThisWorkbook.Sheets("Data Specs").Cells("A2").Value
On Error Resume Next
For Each wBook In Application.Workbooks
For Each wSheet In wBook.Worksheets
Set rFound = Nothing
Set rFound = wSheet.Range("D1:D2").Find(What:=Cvent003Val, SearchFormat:=True, After:=wSheet.Range("D1"), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=True)
'rFound.Cells.Select
If Not rFound Is Nothing Then
bFound = True
Application.Goto rFound, True
'Rows(1, 2).EntireRow.Hidden = True
lngLastRow2 = Cells(Cells.Rows.Count, "B").End(xlUp).Row
Range("A1:G" & lngLastRow2).Copy
ThisWorkbook.Worksheets("Add File Here").Activate
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Exit For
End If
Next wSheet
If bFound = True Then Exit For
Next wBook
If rFound Is Nothing Then
MsgBox "No open file for Cvent003 Meetings Found. Make sure the most recent Cvent003 Excel WB is open!", vbCritical + vbOKOnly
Exit Sub
End If
你的問題是什麼?我在你的問題中看不到問題。 –
'Set rFound = wSheet.Range(「D1:D2」)。Find(What:= ThisWorkbook.Sheets(「Data Specs」)。Range(「A2」),SearchFormat:= True,After:= wSheet.Range 「D1」),_ LookIn:= xlValues,LookAt:= xlPart,SearchOrder:= xlByRows,_ SearchDirection:= xlPrevious,MatchCase:= True)' 這是我正在使用的代碼,在單元格中查找查找值。現在,我希望以類似的方式更改'After'!對不起,我想我不是很清楚,但我希望你們都像我一樣幫助我。 @EganWolf –
任何幫助將不勝感激!謝謝 –