0

I am in the process of creating a VBA code that will allow users to enter information into a form and then move that data into a database for storage. During the migration of the data, I am attempting to usea worksheetfunction.countif to prevent duplicate "Case ID's" from being entered into the database. When I try to run my code with a known duplicate, it is still allowing the data to be written and does not seem to be counting over the entire column.

I am aware that this question relates to several others already asked but I have reviewed every question I could find and it appears to me that I am doing things according to the already provided answers.

Can someone please tell me why my code will not detect duplicates in column 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
dwirony
  • 5,487
  • 3
  • 21
  • 43
ekoenig
  • 3
  • 4
  • Where is CaseID defined and assigned a value? What does the message box return? – QHarr Aug 16 '18 at 16:34
  • All the variables are defined at the very beginning of the code. I only posted the related area. Would it help to post the entire code? The message box is returning "0" even though its a known duplicate. – ekoenig Aug 16 '18 at 16:36
  • `Queries` seems not assigned, hence it-d be `0` and the loop would be skipped – DisplayName Aug 16 '18 at 16:38
  • Only that your syntax is correct so trying to pinpoint other possible sources of error. – QHarr Aug 16 '18 at 16:40
  • Queries is also defined in the same location as the rest of the variables. I will edit the question and provide the entire code, I really appreciate the help on this. – ekoenig Aug 16 '18 at 16:44
  • What happens when CaseId in countif is replaced with CaseReportData(2)? –  Aug 16 '18 at 16:45
  • Is `MsgBox CaseCount` reporting `0` when it should be `1`? –  Aug 16 '18 at 16:47
  • use `Dim CaseCount As long` a string should not be compared to `<1`. –  Aug 16 '18 at 16:50
  • The message box is reporting 0 in all instances when it should be reporting the number of times that Case ID has previously appeared. Which technically should be 1 if its a duplicate. I tried to replaces CaseID with CaseReportData(2) and it is still returning 0. – ekoenig Aug 16 '18 at 16:51
  • and what's the returned value in Immediate Window of `?Worksheets("Case Reporting").Cells(2, 4).Value` – DisplayName Aug 16 '18 at 16:53
  • The Immediate Window is returning a values of 10002, which is the current Case ID. – ekoenig Aug 16 '18 at 16:57
  • it could be the format of column "A", make sure `CaseID` and column A have the same format, for example they are both `TEXT`, you may see a numeric value which is not interpreted ad a number – Ibo Aug 16 '18 at 17:20
  • I have both the cells and the column formatted as General and then I tried to format them both as Text but I am still running in to the same issue. – ekoenig Aug 16 '18 at 17:33

1 Answers1

2

We actually don't need to see all of this code, it was better with the original post, most of it is just noise.

Anyway, the current problem is probably the fact that you are using an implicit reference for Column("A"). Which means that it may not be looking Worksheets("Case Data"), but instead the current sheet in the active workbook.

In fact, the Active Workbook changes when you open the "Database" file, which I'm guessing also has a Worksheet called "Case Data" otherwise the next line would error

You can test this by adding the following line:

Debug.Print Columns("A").Parent.Name 

or even

Debug.Print Worksheets("Case Data").Parent.Name

Originally I said that it should be:

CaseCount = Application.WorksheetFunction.CountIf(Worksheets("Case Data").Columns("A"), CaseID)

but that is wrong, because again I'm using an implicit reference to the Current Workbook

If I were you I'd clean up the code by removing the Select statements and using Explicit references instead of implicit ones (i.e. Sheet.Columns vs 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
Profex
  • 1,370
  • 8
  • 20
  • Thank you for the help. Your solution worked for me and I was able to add in a second criteria to my CountIf, now a CountIfs. I appreciate your help. – ekoenig Aug 16 '18 at 18:02
  • np, that darn implicit reference will get you every time. I even had to update my answer, because it still had an implicit reference. It's always best to setup variables for the Workbooks/Worksheets at the start, that way nothing will change half way through the code. – Profex Aug 16 '18 at 18:13
  • @QHarr, I have Implicit references everywhere up until the last block of code; then I used Explicit references. Mainly because of my calls to `Worksheets("Case Data")` without the Workbook object in front. – Profex Aug 16 '18 at 20:31