[英]VBA Excel .Find function to find variable
我是VBA的初学者。 我正在编写一个代码,该代码将在生产的某个特定时期内生成封装项目的报告。 我正在努力使用带有变量的查找功能
我试图将我的变量声明为各种类型的数据。 我试过了字符串,变量,范围。 如果我手动在括号中输入值,则整个代码将起作用,但是如果我正在寻找变量,它将找不到任何内容
编辑:看来我收到一个不匹配的错误13。蒂姆的评论后变得很清楚,如果我关闭“ On Error Resume Next语句”,这正在发生。
Sub wypelnijtabelkedatapivot()
Dim rng1 As Range, rng2 As Range
Dim cell1 As Range, cell2 As Range
Dim lookitem As String
Dim lookproduct As String
Dim sheetlookitem As String
Dim wb As Workbook
Dim ws As Worksheet
Dim lnRow, lncolumn As Long
Dim kollookitem As Range 'should that be integer?
Dim rowlookitem As Range 'should that be integer?
Application.ScreenUpdating = False
Application.Calculation = xlManual
Columns(384).ClearContents
Set wb = ActiveWorkbook
Set ws = Sheets("datapivot")
With ws
last = .Range("NU:NU").Find("Grand Total").Row
Set rng2 = .Range(ws.Cells(10, 384), ws.Cells(last, 384))
Set rng1 = .Range(ws.Cells(10, 1), ws.Cells(last, 383))
End With
lnRow = 31
lncolumn = 2
For Each cell2 In rng2
If Not Err <> 0 Then
cell2.Value = Left(Replace(Replace(Replace(cell2.Offset(0, 1), "PRE ", ""), "-", ""), " ", ""), 9)
Else
cell.Value = ""
End If
On Error GoTo 0
Next cell2
For Each cell1 In rng1
On Error Resume Next
lookitem = ws.Cells(9, cell1.Column)
lookproduct = ws.Cells(cell1.Row, 385)
sheetlookitem = ws.Cells(cell1.Row, 384).Value
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'PROBLEM IS HERE'
'if I replace lookproduct variable with an actual value in bracket
'e.g "PRE BYL-05-0375-IW-0001", then the row number is returned.
'if I use variable, nothing is returned.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''IT WILL FIND A VALUE as in here'''''''''''rowlookitem = wb.Sheets(sheetlookitem).Columns("B:B").Find(What:="PRE 05-BYL-0375-BS-0300", After:=ActiveCell, LookIn:=xlValues, _
''''''''''LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
''''''''''MatchCase:=False, SearchFormat:=False).Row
rowlookitem = wb.Sheets(sheetlookitem).Columns("B:B").Find(What:=lookproduct, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Row
kollookitem = wb.Sheets(sheetlookitem).Rows("31:31").Find(What:=lookproduct, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
If Not Err <> 0 Then
cell1.Value = ActiveWorkbook.Worksheets(sheetlookitem).Cells(rowlookitem, kollookitem).Value
Else
cell.Value = ""
End If
On Error GoTo 0
Next cell1
Application.Calculation = xlAutomatic
End Sub
编辑:
Set rowlookitem = wb.Sheets(sheetlookitem).Columns("B:B").Find(What:=lookproduct, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rowlookitem Is Empty Then
rowlookitem.Value = rowlookitem.Row
Else
MsgBox "nothing found"
End If
编辑2:
玩了几个小时之后,我终于设法使所有工作正常进行。 我实际上是使用“ noobish”方法制作的,但是它可以工作!
如果有人感兴趣的话,这里是整个代码。 不匹配是问题所在。
Sub wypelnijtabelkedatapivot()
Dim rng1 As Range, rng2 As Range
Dim cell1 As Range, cell2 As Range
Dim lookitem As String
Dim lookproduct As String
Dim sheetlookitem As String
Dim wb As Workbook
Dim ws As Worksheet
Dim lnRow, lncolumn As Long
Dim kollookitem As Long 'should that be integer?
Dim rowlookitem As Long 'should that be integer?
Dim rngFound As Range, rngFound2 As Range: Set rngFound = Nothing
Application.ScreenUpdating = False
Application.Calculation = xlManual
Columns(384).ClearContents
Set wb = ActiveWorkbook
Set ws = Sheets("datapivot")
With ws
last = .Range("NU:NU").Find("Grand Total").Row - 1
Set rng2 = .Range(ws.Cells(10, 384), ws.Cells(last, 384))
Set rng1 = .Range(ws.Cells(10, 1), ws.Cells(last, 383))
End With
lnRow = 31
lncolumn = 2
For Each cell2 In rng2
If Not Err <> 0 Then
cell2.Value = Left(Replace(Replace(Replace(cell2.Offset(0, 1), "PRE ", ""), "-", ""), " ", ""), 9)
Else
cell.Value = ""
End If
On Error GoTo 0
Next cell2
For Each cell1 In rng1
On Error Resume Next
lookitem = ws.Cells(9, cell1.Column)
lookproduct = Replace(ws.Cells(cell1.Row, 385), " ", "", 1, 1)
sheetlookitem = ws.Cells(cell1.Row, 384).Value
rowlookitem = Sheets(sheetlookitem).Range("B:B").Find(lookproduct).Row
kollookitem = Sheets(sheetlookitem).Range("31:31").Find(lookitem).Column
If Not Err <> 0 Then
cell1.Value = wb.Sheets(sheetlookitem).Cells(rowlookitem, kollookitem).Value
Else
cell1.Value = ""
End If
On Error GoTo 0
Next cell1
Application.Calculation = xlAutomatic
End Sub
我不能说我曾经见过或尝试过尝试使用您的代码。 本质上... Range1.Value = Range2.Find.Row
所以我不知道它是否真的应该工作。 但是我可以告诉您,如果“ Find
返回任何内容,则会出现错误。
不要将结果行分配给范围的默认属性,而应Set
范围并测试范围是否为空。
Set Range1 = Range2.Find
If Not Range1 Is Empty Then
Range1.Value = Range1.Row
Else
'no match
End If
这样可以防止错误并仍然返回该行。
另外,请注意合并的单元格,它们将使您的结果混乱。
您的代码有两个问题(除了没有格式化和缩进等):
A)Range.Row是一个Long,因此rowlookitem应该声明为Long
B)Range.Find显示一个错误消息框,因此您应在其运行期间关闭错误消息,但稍后再检查结果。
Dim rngFound As Range: Set rngFound = Nothing
On Error Resume Next
Set rngFound = wb.Sheets(sheetlookitem).Columns("B:B").Find(What:=lookproduct, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
On Error GoTo 0
If Not rngFound Is Nothing Then
rowlookitem = rngFound.Row
kollookitem = rngFound.Column
' Use these variables ...
Else
' Not found, don't use the variables
End If
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.