Excel “;对象变量或未设置块变量”;错误来临
我正在运行代码从工作簿中查找数据,并将其与另一个工作簿同步,在运行宏时,我收到此错误。有人能帮忙吗?谢谢 我正在运行代码从工作簿中查找数据,并将其与另一个工作簿同步,在运行宏时,我收到此错误。有人能帮忙吗?谢谢Excel “;对象变量或未设置块变量”;错误来临,excel,vba,Excel,Vba,我正在运行代码从工作簿中查找数据,并将其与另一个工作簿同步,在运行宏时,我收到此错误。有人能帮忙吗?谢谢 我正在运行代码从工作簿中查找数据,并将其与另一个工作簿同步,在运行宏时,我收到此错误。有人能帮忙吗?谢谢 Sub FindData(wbW As Workbook, WbD As Workbook, ByVal dCol As Long) Dim wSh As Long Dim dSh As Long Dim w As Long, d As Long, c As Lo
Sub FindData(wbW As Workbook, WbD As Workbook, ByVal dCol As Long)
Dim wSh As Long
Dim dSh As Long
Dim w As Long, d As Long, c As Long
Dim col As String
Dim co As String
Dim ws As Worksheet
Dim var As Range, coCl As Range
Dim lastColD As Long, lastColW As Long, lastRowW
Dim wsW As Worksheet, wsD As Worksheet
Dim dc As Long, wc As Long
Set ws = ThisWorkbook.Worksheets(1)
Debug.Print WbD.Name
Debug.Print wbW.Name
If wbW.Name = ws.Range("A2") & ".xlsx" And WbD.Name = ws.Range("A4") & ".xlsx" Then
col = "D"
Else
If wbW.Name = ws.Range("A2") & ".xlsx" And WbD.Name = ws.Range("A5") & ".xlsx" Then
col = "G"
Else
If wbW.Name = ws.Range("A2") & ".xlsx" And WbD.Name = ws.Range("A6") & ".xlsx" Then
col = "J"
Else
If wbW.Name = ws.Range("A3") & ".xlsx" And WbD.Name = ws.Range("A7") & ".xlsx" Then
col = "M"
Else
If wbW.Name = ws.Range("A3") & ".xlsx" And WbD.Name = ws.Range("A8") & ".xlsx" Then
col = "P"
End If
End If
End If
End If
End If
wSh = ws.Range(col & 1).End(xlDown).Row
dSh = WbD.Worksheets.Count
For w = 3 To wSh 'Working file sheets listed in macro workbook Sheet1
Set wsW = wbW.Worksheets(ws.Range(col & w).Value)
lastColW = wsW.Cells(2, wsW.Columns.Count).End(xlToLeft).Column
lastRowW = wsW.Cells(wsW.Rows.Count, 2).End(xlUp).Row
For c = 5 To lastRowW 'Companies in working file
co = wsW.Range("B" & c)
For d = 1 To dSh 'Data worksheet
Set coCl = Nothing
Set wsD = WbD.Worksheets(d)
If wsD.Range("A1") = co Then
Set coCl = wsD.Range("A1")
Else
If wsD.Range("A2") = co Then
Set coCl = wsD.Range("A2")
End If
End If
If Not coCl Is Nothing Then
lastColD = wsD.Cells(coCl.Offset(1, 0).Row, wsD.Columns.Count).End(xlToLeft).Column
' If WbD.Name = "2005-2010.xlsx" Then
' yr = "5-10"
' End If
If lastColD = 1 Then
lastColD = wsD.Cells(coCl.Offset(2, 0).Row, wsD.Columns.Count).End(xlToLeft).Column
Set coCl = coCl.Offset(1, 0)
End If
Set var = wsD.Range("A3").CurrentRegion.Columns(1).Find(ws.Range(col & w).Offset(0, 1), , xlValues, xlPart, , , False)
'Debug.Print wsD.Name
For dc = 2 To lastColD
For wc = 5 To lastColW
'Debug.Print wsD.Cells(coCl.Offset(1, 0).Row, dc).Value
'Debug.Print wsW.Cells(2, wc).Value
If wsD.Cells(coCl.Offset(1, 0).Row, dc).Value = wsW.Cells(2, wc).Value Then
'wsD.Range(wsD.Cells(var.Row, 2), wsD.Cells(var.Row, lastColD)).Copy Destination:=wsW.Cells(c, dCol)
' Debug.Print wsD.Name
' Debug.Print wsD.Cells(var.Row, dc).Value
wsW.Cells(c, wc).Value = wsD.Cells(var.Row, dc).Value
End If
Next
Next
Exit For
End If
Next
Next
Next
'Debug.Print WbD.Name
'Debug.Print wbW.Name
End Sub
我正在运行代码从工作簿中查找数据,并将其与另一个工作簿同步,在运行宏时,我收到此错误。有人能帮忙吗?谢谢
Sub FindData(wbW As Workbook, WbD As Workbook, ByVal dCol As Long)
Dim wSh As Long
Dim dSh As Long
Dim w As Long, d As Long, c As Long
Dim col As String
Dim co As String
Dim ws As Worksheet
Dim var As Range, coCl As Range
Dim lastColD As Long, lastColW As Long, lastRowW
Dim wsW As Worksheet, wsD As Worksheet
Dim dc As Long, wc As Long
Set ws = ThisWorkbook.Worksheets(1)
Debug.Print WbD.Name
Debug.Print wbW.Name
If wbW.Name = ws.Range("A2") & ".xlsx" And WbD.Name = ws.Range("A4") & ".xlsx" Then
col = "D"
Else
If wbW.Name = ws.Range("A2") & ".xlsx" And WbD.Name = ws.Range("A5") & ".xlsx" Then
col = "G"
Else
If wbW.Name = ws.Range("A2") & ".xlsx" And WbD.Name = ws.Range("A6") & ".xlsx" Then
col = "J"
Else
If wbW.Name = ws.Range("A3") & ".xlsx" And WbD.Name = ws.Range("A7") & ".xlsx" Then
col = "M"
Else
If wbW.Name = ws.Range("A3") & ".xlsx" And WbD.Name = ws.Range("A8") & ".xlsx" Then
col = "P"
End If
End If
End If
End If
End If
wSh = ws.Range(col & 1).End(xlDown).Row
dSh = WbD.Worksheets.Count
For w = 3 To wSh 'Working file sheets listed in macro workbook Sheet1
Set wsW = wbW.Worksheets(ws.Range(col & w).Value)
lastColW = wsW.Cells(2, wsW.Columns.Count).End(xlToLeft).Column
lastRowW = wsW.Cells(wsW.Rows.Count, 2).End(xlUp).Row
For c = 5 To lastRowW 'Companies in working file
co = wsW.Range("B" & c)
For d = 1 To dSh 'Data worksheet
Set coCl = Nothing
Set wsD = WbD.Worksheets(d)
If wsD.Range("A1") = co Then
Set coCl = wsD.Range("A1")
Else
If wsD.Range("A2") = co Then
Set coCl = wsD.Range("A2")
End If
End If
If Not coCl Is Nothing Then
lastColD = wsD.Cells(coCl.Offset(1, 0).Row, wsD.Columns.Count).End(xlToLeft).Column
' If WbD.Name = "2005-2010.xlsx" Then
' yr = "5-10"
' End If
If lastColD = 1 Then
lastColD = wsD.Cells(coCl.Offset(2, 0).Row, wsD.Columns.Count).End(xlToLeft).Column
Set coCl = coCl.Offset(1, 0)
End If
Set var = wsD.Range("A3").CurrentRegion.Columns(1).Find(ws.Range(col & w).Offset(0, 1), , xlValues, xlPart, , , False)
'Debug.Print wsD.Name
For dc = 2 To lastColD
For wc = 5 To lastColW
'Debug.Print wsD.Cells(coCl.Offset(1, 0).Row, dc).Value
'Debug.Print wsW.Cells(2, wc).Value
If wsD.Cells(coCl.Offset(1, 0).Row, dc).Value = wsW.Cells(2, wc).Value Then
'wsD.Range(wsD.Cells(var.Row, 2), wsD.Cells(var.Row, lastColD)).Copy Destination:=wsW.Cells(c, dCol)
' Debug.Print wsD.Name
' Debug.Print wsD.Cells(var.Row, dc).Value
wsW.Cells(c, wc).Value = wsD.Cells(var.Row, dc).Value
End If
Next
Next
Exit For
End If
Next
Next
Next
'Debug.Print WbD.Name
'Debug.Print wbW.Name
End Sub
@BruceWayne的建议是,在以后使用之前,您需要验证
Find
函数是否成功
Set Var = wsD.Range("A3").CurrentRegion.Columns(1).Find(ws.Range(col & w).Offset(0, 1), , xlValues, xlPart, , , False)
If Not Var Is Nothing Then ' <-- make sure Find function was able to find a match
' rest of your code goes here
Else
' raise an error message
MsgBox "Unable to find " & ws.Range(col & w).Offset(0, 1) & " in the range specified", vbCritical, "Error!"
End If
请只发布有错误的代码-不需要所有代码…哪一行有错误?@JohnyL这是第144行第1列第144行你想让我们数一数行吗?:)@约翰:对不起