簡體   English   中英

VBA Excel .Find函數查找變量

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM