在执行代码时,出现以下错误“期望结尾为”。如果提供帮助,那就太好了。
我创建了下面的代码,用于比较两个电子表格。最初,它比较sheet1(“状态”)和sheet2(“ Interface”)的值范围。每当一个范围与另一个工作表中存在的范围匹配时,它都不执行任何操作。当该范围在另一个工作表中不匹配时,则将整个记录从sheet2复制到sheet1。两张工作表中都有大约1500行数据,每列15列。
Function UpdateNEW2() As Long
Const Delimiter As String = "|"
Dim dic As Object
Set dic = CreateObject("Scripting.Dictionary")
Dim newRows As Range
Dim vSteps, key
With Sheets("Steps")
vSteps = .Range("A2:C2", .Cells(.Rows.Count, 1).End(xlUp)).Value
End With
Dim r As Long, c As Long, n As Long
For r = 1 To UBound(vSteps)
key = vSteps(r, 2) & Delimiter & vSteps(r, 2)
If Not dic.Exists(key) Then dic.Add key, 0
Next
Dim vInterface, results
With Sheets("Interface")
vInterface = .Range("A2:O2", .Cells(.Rows.Count, "C").End(xlUp)).Value
End With
ReDim results(1 To UBound(vInterface), 1 To 15)
For r = 1 To UBound(vInterface)
key = vInterface(r, 5) & Delimiter & vInterface(r, 5)
If Not dic.Exists(key) Then
n = n + 1
For c = 3 To 15
results(n, c - 2) = vInterface(r, c)
Next
End If
Next
With Sheets("Steps")
With .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
.Resize(n, 15).Value = results
End With
UpdateNEW2 = n
End Function
答案 0 :(得分:1)
Option Explicit '<- Always use Option Explicit
Function UpdateNEW2() As Long
Const Delimiter As String = "|"
Dim dic As Object
Dim newRows As Range
Dim vSteps, key, vInterface, results '<- You could declare variables
Dim r As Long, c As Long, n As Long
Set dic = CreateObject("Scripting.Dictionary")
With ThisWorkbook '<- To avoid issues if two workbooks are open create a "With Statement" for this workbook
With .Sheets("Steps") '<- Use "." before Sheets to show that you will the work in the workbook of the "With Statement"
vSteps = .Range("A2:C2", .Cells(.Rows.Count, 1).End(xlUp)).Value
End With
For r = 1 To UBound(vSteps)
key = vSteps(r, 2) & Delimiter & vSteps(r, 2)
If Not dic.Exists(key) Then dic.Add key, 0
Next
With .Sheets("Interface") '<- Use "." before Sheets to show that you will the work in the workbook of the "With Statement"
vInterface = .Range("A2:O2", .Cells(.Rows.Count, "C").End(xlUp)).Value
End With
ReDim results(1 To UBound(vInterface), 1 To 15)
For r = 1 To UBound(vInterface)
key = vInterface(r, 5) & Delimiter & vInterface(r, 5)
If Not dic.Exists(key) Then
n = n + 1
For c = 3 To 15
results(n, c - 2) = vInterface(r, c)
Next
End If
Next
With .Sheets("Steps") '<- Use "." before Sheets to show that you will the work in the workbook of the "With Statement"
With .Cells(.Rows.Count, 1).End(xlUp).Offset(1)
.Resize(n, 15).Value = results
End With
UpdateNEW2 = n
End With
End With
End Function
答案 1 :(得分:0)
Function
如您所见,适当的缩进使得在需要时更易于阅读和更正。