我已經完成了我非常粗略的代碼,但我正在優化它。VBA錯誤91 - 無法設置我的範圍
我已經定義了我的工作表,但現在我需要定義我的2個範圍rngTo和rngFrom。 rngTo設置沒有問題,但rngFrom拋出一個錯誤91,我不明白爲什麼。它應該與另一個不同?下面是我認爲這是相關代碼的一部分,但也有可能是未在此快照使用的一些昏暗的:
Sub Worksheet_UpdateAllItemCostData()
Dim material As Variant
Dim fndEntry, rngTo, rngFrom As Range
Dim wb1 As Workbook, wb2 As Workbook
Dim wsTo, wsFrom As Worksheet
Dim lr As Long, I As Long, J As Long
Const sPOS As String = "Pos. "
Application.Calculation = xlCalculationManual ' Disable
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Set wb1 = ThisWorkbook
Set wsTo = wb1.Sheets("Sagsnr.")
wsTo.Rows("1:1").Hidden = False
lr = wb1.Sheets("Sagsnr.").Cells(Rows.Count, "C").End(xlUp).Row
If lr < 21 Then
Exit Sub
End If
Set wb2 = Workbooks.Open(Filename:="G:\Backoffice\Tilbudsteam\Kostdatabase\Matcost.xls", ReadOnly:=True)
Set wsFrom = wb2.Sheets("Matcost")
For I = 21 To lr
material = wsTo.Range("C" & I).Value
Set fndEntry = wsFrom.UsedRange.Columns(4).Find(What:=material)
Set rngTo = wsTo.Range("A" & I)
Set rngFrom = wsFrom.Range("A" & fndEntry.Row)
If Not fndEntry Is Nothing Then
rngTo(, "B") = rngFrom(, "H")
End If
Next I
wb2.Close
wsTo.Rows("1:1").Hidden = True
Application.Calculation = xlCalculationAutomatic 'Enable
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
在'Dim fndEntry,rngTo,rngFrom As Range'行中,只有rngFrom被定義爲Range,fndEntry和rngTo的類型是Variant。 –
我不覺得自己像個傻瓜,thx一堆! – Smeltet