VBA CountIF 不会计算整个列
VBA CountIF will not count over entire column
我正在创建一个 VBA 代码,允许用户将信息输入表单,然后将该数据移动到数据库中进行存储。在数据迁移期间,我试图使用 worksheetfunction.countif 来防止将重复的 "Case ID's" 输入到数据库中。当我尝试 运行 我的代码与已知的重复时,它仍然允许写入数据并且似乎没有计算整个列。
我知道这个问题与其他几个已经问过的问题有关,但我已经审查了我能找到的每个问题,在我看来,我正在根据已经提供的答案做事。
有人能告诉我为什么我的代码无法检测到 A 列中的重复项吗?
Sub DatabaseBuilder()
Dim CaseID As String
Dim CaseLevel As String
Dim Queries As String
Dim CaseReportData(2 To 8) As String
Dim UniqueIdentifier As String
Dim CaseCount As String
Worksheets("Case Reporting").Select
CaseID = Worksheets("Case Reporting").Cells(2, "D").Value
CaseLevel = Worksheets("Case Reporting").Cells(4, "D").Value
Queries = Worksheets("Case Reporting").Cells(1, "A").Value
For i = 2 To 4
CaseReportData(i) = Worksheets("Case Reporting").Cells(i, 4).Value
Next i
For i = 5 To 7
CaseReportData(i) = Worksheets("Case Reporting").Cells(i - 3, 8).Value
Next i
CaseReportData(i) = Worksheets("Case Reporting").Cells(34, 3).Value
ReDim QueryData(1 To Queries) As String
Dim Count As Integer
Count = 1
For i = 1 To Queries
UniqueIdentifier = Worksheets("Case Reporting").Cells(Count + 6, 3).Value
If UniqueIdentifier = i Then
QueryData(i) = Worksheets("Case Reporting").Cells(Count + 6, 6).Value
Count = Count + 1
End If
Next i
Set myData = Workbooks.Open....
Worksheets("Case Data").Select
Worksheets("Case Data").Range("A1").Select
RowCount = Worksheets("Case Data").Range("A1").CurrentRegion.Rows.Count
CaseCount = Application.WorksheetFunction.CountIf(Columns("A"), CaseID)
MsgBox CaseCount
If CaseCount < 1 Then
For i = 2 To 8
With Worksheets("Case Data").Range("A1")
.Offset(RowCount, i - 2) = CaseReportData(i)
End With
Next i
Worksheets("Case Data").Select
Worksheets("Case Data").Range("H1").Select
For i = 1 To Queries
With Worksheets("Case Data").Range("H1")
.Offset(RowCount, i - 1) = QueryData(i)
End With
Next i
End If
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub
我们实际上不需要看到所有这些代码,原始代码更好 post,其中大部分只是噪音。
无论如何,当前的问题可能是您正在使用 Column("A")
的隐式引用。这意味着它可能不会查找 Worksheets("Case Data")
,而是查找活动工作簿中的当前 sheet。
事实上,当您打开 "Database" 文件时,Active Workbook 会发生变化,我猜它还有一个名为 "Case Data"否则下一行会报错
您可以通过添加以下行来测试它:
Debug.Print Columns("A").Parent.Name
甚至
Debug.Print Worksheets("Case Data").Parent.Name
本来我说应该是:
CaseCount = Application.WorksheetFunction.CountIf(Worksheets("Case Data").Columns("A"), CaseID)
但这是错误的,因为我再次使用了对当前工作簿的隐式引用
如果我是你,我会通过 removing the Select statements 清理代码并使用 Explicit 引用而不是 implicit 引用(即 Sheet.Columns 与列)。
Dim Book as Workbook
Dim Sheet as Worksheet
Set Book = ThisWorkbook ' Or ActiveWorkbook or Workbooks("File ABC.xlsx")
Set Sheet = Book.Worksheets("Case Data")
With Sheet
CaseID = .Cells(2, "D").Value
'''
CaseCount = Application.WorksheetFunction.CountIf(.Columns("A"), CaseID)
End With
我正在创建一个 VBA 代码,允许用户将信息输入表单,然后将该数据移动到数据库中进行存储。在数据迁移期间,我试图使用 worksheetfunction.countif 来防止将重复的 "Case ID's" 输入到数据库中。当我尝试 运行 我的代码与已知的重复时,它仍然允许写入数据并且似乎没有计算整个列。
我知道这个问题与其他几个已经问过的问题有关,但我已经审查了我能找到的每个问题,在我看来,我正在根据已经提供的答案做事。
有人能告诉我为什么我的代码无法检测到 A 列中的重复项吗?
Sub DatabaseBuilder()
Dim CaseID As String
Dim CaseLevel As String
Dim Queries As String
Dim CaseReportData(2 To 8) As String
Dim UniqueIdentifier As String
Dim CaseCount As String
Worksheets("Case Reporting").Select
CaseID = Worksheets("Case Reporting").Cells(2, "D").Value
CaseLevel = Worksheets("Case Reporting").Cells(4, "D").Value
Queries = Worksheets("Case Reporting").Cells(1, "A").Value
For i = 2 To 4
CaseReportData(i) = Worksheets("Case Reporting").Cells(i, 4).Value
Next i
For i = 5 To 7
CaseReportData(i) = Worksheets("Case Reporting").Cells(i - 3, 8).Value
Next i
CaseReportData(i) = Worksheets("Case Reporting").Cells(34, 3).Value
ReDim QueryData(1 To Queries) As String
Dim Count As Integer
Count = 1
For i = 1 To Queries
UniqueIdentifier = Worksheets("Case Reporting").Cells(Count + 6, 3).Value
If UniqueIdentifier = i Then
QueryData(i) = Worksheets("Case Reporting").Cells(Count + 6, 6).Value
Count = Count + 1
End If
Next i
Set myData = Workbooks.Open....
Worksheets("Case Data").Select
Worksheets("Case Data").Range("A1").Select
RowCount = Worksheets("Case Data").Range("A1").CurrentRegion.Rows.Count
CaseCount = Application.WorksheetFunction.CountIf(Columns("A"), CaseID)
MsgBox CaseCount
If CaseCount < 1 Then
For i = 2 To 8
With Worksheets("Case Data").Range("A1")
.Offset(RowCount, i - 2) = CaseReportData(i)
End With
Next i
Worksheets("Case Data").Select
Worksheets("Case Data").Range("H1").Select
For i = 1 To Queries
With Worksheets("Case Data").Range("H1")
.Offset(RowCount, i - 1) = QueryData(i)
End With
Next i
End If
ActiveWorkbook.Save
ActiveWorkbook.Close
End Sub
我们实际上不需要看到所有这些代码,原始代码更好 post,其中大部分只是噪音。
无论如何,当前的问题可能是您正在使用 Column("A")
的隐式引用。这意味着它可能不会查找 Worksheets("Case Data")
,而是查找活动工作簿中的当前 sheet。
事实上,当您打开 "Database" 文件时,Active Workbook 会发生变化,我猜它还有一个名为 "Case Data"否则下一行会报错
您可以通过添加以下行来测试它:
Debug.Print Columns("A").Parent.Name
甚至
Debug.Print Worksheets("Case Data").Parent.Name
本来我说应该是:
CaseCount = Application.WorksheetFunction.CountIf(Worksheets("Case Data").Columns("A"), CaseID)
但这是错误的,因为我再次使用了对当前工作簿的隐式引用
如果我是你,我会通过 removing the Select statements 清理代码并使用 Explicit 引用而不是 implicit 引用(即 Sheet.Columns 与列)。
Dim Book as Workbook
Dim Sheet as Worksheet
Set Book = ThisWorkbook ' Or ActiveWorkbook or Workbooks("File ABC.xlsx")
Set Sheet = Book.Worksheets("Case Data")
With Sheet
CaseID = .Cells(2, "D").Value
'''
CaseCount = Application.WorksheetFunction.CountIf(.Columns("A"), CaseID)
End With