首先,在信贷到期时给予贷款。这是使用来自Mr.Excel.com和exceltip.com的代码。在VBA中搜索整个工作簿,然后将结果粘贴到另一个工作表中
现在,我已经得到了这一点的,我想创建一个搜索功能,将通过在Excel中我9页文件搜索,发现被输入到搜索框中的值的方式。然后将这些值粘贴到工作簿的第一页上。
我需要什么,在我的代码修改,使其粘贴到搜索页面上的正确位置?我试图在最后一个循环中更改事物,因为那是我得到“运行时错误91.对象变量或块变量未设置”的地方。
我GOOGLE了这个错误,但总是变量拧我,这样可能是问题。 The search page. This is where the Debugger stops. 这是到目前为止我的代码。
Sub Find_one()
'Find Function For ERF Spreadsheet'
'Type in Box, Press Button, Display the Results'
Dim ws As Worksheet, Found As Range
Dim myText As String, FirstAddress As String
Dim AddressStr As String, foundNum As Integer
myText = Range("D5")
If myText = "" Then Exit Sub
For Each ws In ThisWorkbook.Worksheets
With ws
'Do not search sheet1'
If ws.Name = "Sheet1" Then GoTo myNext
Set Found = .UsedRange.Find(What:=myText, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
If Not Found Is Nothing Then
FirstAddress = Found.Address
Do
foundNum = foundNum + 1
AddressStr = AddressStr & .Name & " " & Found.Address & vbCrLf
Set Found = .UsedRange.FindNext(Found)
'Found.EntireRow.Copy _
'Destination:=Worksheets("Sheet4").Range("A65536").End(xlUp).Offset(1, 0)
Loop While Not Found Is Nothing And Found.Address <> FirstAddress
End If
myNext:
End With
Next ws
If Len(AddressStr) Then
Sheet8.Range("B18") = ws.Cells(x, 1)
Sheet8.Range("C18") = ws.Cells(x, 2)
Sheet8.Range("D18") = ws.Cells(x, 3)
Sheet8.Range("E18") = ws.Cells(x, 4)
Sheet8.Range("F18") = ws.Cells(x, 5)
Sheet8.Range("G18") = ws.Cells(x, 6)
Sheet8.Range("H18") = ws.Cells(x, 7)
Sheet8.Range("I18") = ws.Cells(x, 8)
Sheet8.Range("J18") = ws.Cells(x, 9)
Else:
MsgBox "Unable to find " & myText & " in this workbook.", vbExclamation
End If
End Sub
这是最后一个循环的原代码...
If Len(AddressStr) Then
MsgBox "Found: """ & myText & """ " & foundNum & " times." & vbCr & _
AddressStr, vbOKOnly, myText & " found in these cells"
Else:
MsgBox "Unable to find " & myText & " in this workbook.", vbExclamation
End If
如果你试图将图片,看起来他们并没有通过。 – dwirony
我弄明白了。 –
我认为一个问题是,看着你的工作簿的附加图片,你的工作表名称是字母和“搜索”,但你参考基于“Sheet1”和“Sheet4”的工作表。尝试用工作表(“C”)或ws.Name =“搜索”替换那些与您正在使用的工作表的实际名称。或者,如果您不想使用他们的名字,可以使用工作表(1)或表格(4)参考他们的位置。 – Dexloft