以下是我到目前为止的代码。我正在尝试执行以下操作:
将“ICT编号”与工作表名称进行比较,如果该工作表名称包含ICT编号,即使它与其他字符串值混合在一起,那么我想查看该工作表中的某个单元格并比较在我的清单工作表中包含单元格的单元格中的值。
如果这些值相同,那么我想在该行的相应单元格中显示一条消息,说明两个来源已协调。
然后我想要循环检查清单工作表中的所有行以及工作簿中的所有工作表。
Dim ICT_Number As Range
Dim statmentdata As Range
Dim checklistdata As Range
Dim Worksheet As Variant
Dim reconcile As Range
For Each cell In Range("d6:d236")
Set ICT_Number = ActiveCell
Set statementdata = Worksheets("m0017 v p0903").Range("H2016")
Set checklistdata = ActiveCell.Offset(0, 5)
Set currsheet = Worksheets("m0017 v p0903")
Set reconcile = ActiveCell.Offset(0, 11)
If InStr(1, cell, ICT_Number, 1) Then
If statmentdata = checklistdata Then
reconclie.Value = "this line reconiles"
Else
reconcile.Value = "this line does not reconclie"
End If
Next cell
End Sub
答案 0 :(得分:0)
您的代码中有几个问题:
请不要这样做:
Dim Worksheet As Variant
更好:
Dim ws as Worksheet
Set
这不起作用:
currsheet = Worksheets("statement n0246 v ab119")
相反,请执行:
Set currsheet = Worksheets("statement n0246 v ab119")
Sub test()
Dim ICT_Number As Range
Dim statmentdata As Range
Dim checklistdata As Range
Dim Worksheet As Variant
Dim reconcile As Range
Set ICT_Number = Worksheets("checklist").Range("D79")
Set statementdata = Worksheets("n0246 v ab119").Range("H2016")
Set checklistdata = Worksheets("checklist").Range("H79")
Set currsheet = Worksheets("statement n0246 v ab119")
Set reconcile = Worksheets("checklist").Range("N79")
If currsheet.Name = ICT_Number Then
If statmentdata = checklistdata Then
reconclie.Value = "this line reconiles"
Else
reconcile.Value = "this line does not reconclie"
End If
End If
End Sub
请查看VBA教程学习语法:VBA: Basic Syntax and Examples Tutorial