我自己负责构建一个小型QA工具,但问题是我对VBA或编程并不十分熟悉。我希望能够遍历三个不同的.csv文件中的单个列。这些列中的单元格应该匹配,并且工具的整个点是识别那些"奇怪的人"。
目前我有一些意大利面条代码给我一个错误" false.xlsx"没找到......请温柔......
Sub CompareLists()
Dim count As Integer
Dim kRange As Range
Dim LastRow As Long
Dim iRow As Long
Dim varSheetA As Variant
Dim varSheetB As Variant
Dim varSheetC As Variant
LastRow = Range("A" & Rows.count).End(xlUp).Row
Set kRange = ActiveSheet.Range("A3:A" & LastRow)
Set File_Path1 = Workbooks.Open(Filename = "C:\Test Files\Test_File1.csv")
Set varSheetA = File_Path1.Worksheets("Sheet1").Range(kRange)
Set File_Path2 = Workbooks.Open(Filename = "C:\Test Files\Test_File2.csv")
Set varSheetB = File_Path2.Worksheets("Sheet1").Range(kRange)
Set File_Path3 = Workbooks.Open(Filename = "C:\Test Files\Test_File3.csv")
Set varSheetC = File_Path3.Worksheets("Sheet1").Range(kRange)
For iRow = LBound(varSheetA) To UBound(varSheetA)
count = 1
If varSheetA(iRow) = varSheetB(iRow) Then
count = count + 1
Else
If varSheetA(iRow) = varSheetC(iRow) Then
count = count + 1
Else
If count < 3 Then
ActiveCell.Interior.ColorIndex = 3
End If
End If
End If
Next
End Sub
答案 0 :(得分:3)
你的专栏:
Set File_Path1 = Workbooks.Open(Filename = "C:\Test Files\Test_File1.csv")
将内部计算为布尔检查,检查一些不存在的Filename
变量是否具有值&#34; C:\ Test Files \ Test_File1.csv&#34;。由于新变量Filename
没有值,因此比较返回false。您可能正在尝试:=
而不是=
。 :=
运算符分配给方法参数,而=
执行布尔评估或变量赋值。以下四行中的任何一行都应该修复错误,因为第一行使用:=
运算符分配给Filename参数,后两行只知道Filename是默认的第一个参数。
Set File_Path1 = Workbooks.Open(Filename:="C:\Test Files\Test_File1.csv")
Set File_Path1 = Workbooks.Open Filename:="C:\Test Files\Test_File1.csv"
Set File_Path1 = Workbooks.Open("C:\Test Files\Test_File1.csv")
Set File_Path1 = Workbooks.Open "C:\Test Files\Test_File1.csv"
作为旁注,这在VBA中要非常小心。将Option Explicit
放在每个模块的顶部,这样就会强制您定义变量。如果你这样做,你的错误输出就会抱怨变量&#39;文件名&#39;没有定义。
就你的下一行而言:
Set varSheetA = File_Path1.Worksheets("Sheet1").Range(kRange)
你有两个问题。首先,打开csv文件时,选项卡名称始终与文件名相同,而不是Sheet1。其次,kRange
是您开始使用的工作表的一部分,以便在新工作表中获得可比范围{/ 1}}。这些修正将上述内容改为:
kRange.Address
你的循环处理数组之类的范围。解决这个问题的最直接方法是通过更改Set varSheetA = File_Path1.Worksheets("Test_File1").Range(kRange.Address)
语句(添加括号)和范围变量的赋值语句(删除Dim
并添加Set
)来强制它们成为数组
.Value
最好的替代方法是永远不要创建范围变量,只需按行和列索引获取单元格。
Dim varSheetA() As Variant
...
varSheetA = File_Path1.Worksheets("Test_File1").Range(kRange.Address).Value
...
If varSheetA(iRow, 1) = varSheetB(iRow, 1) Then
当然,据我所知,你的布尔逻辑简化为:
Sub CompareLists_2()
Dim count As Integer
Dim LastRow As Long, iRow As Long
Dim MainSht As Worksheet, ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
Set MainSht = ActiveSheet
LastRow = MainSht.Range("A" & Rows.count).End(xlUp).Row
Set ws1 = Workbooks.Open(Filename:="C:\Test Files\Test_File1.csv").Worksheets("Test_File1")
Set ws2 = Workbooks.Open(Filename:="C:\Test Files\Test_File2.csv").Worksheets("Test_File2")
Set ws3 = Workbooks.Open(Filename:="C:\Test Files\Test_File3.csv").Worksheets("Test_File3")
For iRow = 3 To LastRow
count = 1
If ws1.Cells(iRow, 1).Value = ws2.Cells(iRow, 1).Value Then
count = count + 1
Else
If ws1.Cells(iRow, 1).Value = ws3.Cells(iRow, 1).Value Then
count = count + 1
Else
If count < 3 Then
MainSht.Cells(iRow, 1).Interior.ColorIndex = 3
End If
End If
End If
Next
End Sub
每条评论修改布尔逻辑(虽然我对逻辑略有不同):
If ws1.Cells(iRow, 1).Value <> ws2.Cells(iRow, 1).Value And ws1.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value And ws2.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value Then
MainSht.Cells(iRow, 1).Interior.ColorIndex = 3
End If
答案 1 :(得分:0)
Sub CompareLists()
Dim count As Integer
Dim LastRow As Long, iRow As Long
Dim MainSht As Worksheet, ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, ws4 As Worksheet
Dim ws5 As Workbook
Dim var1 As Variant
Set ws1 = Workbooks.Open(Filename:="C:\Test Files\Test_File1.csv").Worksheets("Test_File1")
Set ws2 = Workbooks.Open(Filename:="C:\Test Files\Test_File2.csv").Worksheets("Test_File2")
Set ws3 = Workbooks.Open(Filename:="C:\Test Files\Test_File3.csv").Worksheets("Test_File3")
Set MainSht = ws1
LastRow = MainSht.Range("A" & Rows.count).End(xlUp).Row
For iRow = 3 To LastRow
count = 1
If ws1.Cells(iRow, 1).Value <> ws2.Cells(iRow, 1).Value And ws1.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value Then
ws1.Cells(iRow, 1).Interior.ColorIndex = 3
ElseIf ws1.Cells(iRow, 1).Value = ws2.Cells(iRow, 1).Value And ws1.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value Then
ws1.Cells(iRow, 1).Interior.ColorIndex = 6
ws2.Cells(iRow, 1).Interior.ColorIndex = 6
ElseIf ws1.Cells(iRow, 1).Value = ws3.Cells(iRow, 1).Value And ws1.Cells(iRow, 1).Value <> ws2.Cells(iRow, 1).Value Then
ws1.Cells(iRow, 1).Interior.ColorIndex = 6
ws3.Cells(iRow, 1).Interior.ColorIndex = 6
End If
If ws3.Cells(iRow, 1).Value <> ws2.Cells(iRow, 1).Value And ws3.Cells(iRow, 1).Value <> ws1.Cells(iRow, 1).Value Then
ws3.Cells(iRow, 1).Interior.ColorIndex = 3
ElseIf ws3.Cells(iRow, 1).Value = ws2.Cells(iRow, 1).Value And ws3.Cells(iRow, 1).Value <> ws1.Cells(iRow, 1).Value Then
ws3.Cells(iRow, 1).Interior.ColorIndex = 6
ws2.Cells(iRow, 1).Interior.ColorIndex = 6
End If
If ws2.Cells(iRow, 1).Value <> ws3.Cells(iRow, 1).Value And ws2.Cells(iRow, 1).Value <> ws1.Cells(iRow, 1).Value Then
ws2.Cells(iRow, 1).Interior.ColorIndex = 3
End If
Next
End Sub