Assistance with VBA error handling - a cell not matching data set

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
915
Office Version
  1. 365
Platform
  1. Windows
I was given the below VBA code and do not fully understand it. It runs when I enter a year in cell A1 on sheet "Score Card".

However if I enter in a year that has no data on sheet "Log" I get a Run-time error 1004: Application-defined or object-defined error.

I would like it if the year entered has no data on sheet "Log" that a Msg box comes up and states "No Data" and exits.

Any help would be appreciated! Thank you

Injury Data All Years.xlsm
ABCDEFGHIJKLMNOPQRSTUV
12023
2Safety (Employees)
3Key Performance IndicatorOperationsArea
4TargetActualCellParking Lot
5Reportable000000000000000000000
6Non-Reportable021100000000000000000
7Near Miss000000000000000000000
Score Card
Cell Formulas
RangeFormula
C5:C7C5=SUM(D5:X5)
D5:V5D5=COUNTIFS(Log[Area],'Score Card'!D4,Log[Year],'Score Card'!$A$1,Log[Reportable/ non-reportable],'Score Card'!$A$5)
D6:V6D6=COUNTIFS(Log[Area],'Score Card'!D4,Log[Year],'Score Card'!$A$1,Log[Reportable/ non-reportable],'Score Card'!$A$6)
D7:V7D7=COUNTIFS(Log[Area],'Score Card'!D4,Log[Year],'Score Card'!$A$1,Log[Reportable/ non-reportable],'Score Card'!$A$7)
Named Ranges
NameRefers ToCells
'Score Card'!Print_Area='Score Card'!$A$1:$S$51D5:V7


Injury Data All Years.xlsm
ABCDEFGHI
1First NameLast NameYearDateBuildingDivisionReportable/ non-reportableAreaLost Time
2MD20232023-01-06UnNon-ReportableCellN
3TW20232023-01-05UnNon-ReportableParking LotN
Log
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G2:G3Cell Value="Near Miss"textNO
G2:G3Cell Value="Non-Reportable"textNO
G:GCell Value="Near Miss"textNO
G2:G99860Cell Value="Non-Reportable"textNO
Cells with Data Validation
CellAllowCriteria
E2:E1048576List='Graph Data'!$T$2:$T$7
F2:F1048576List='Graph Data'!$U$2:$U$13
G2:G1048576List='Graph Data'!$W$2:$W$4
H2:H3List='Graph Data'!$V$2:$V$40
I2:I1048576List='Graph Data'!$X$2:$X$3


VBA Code:
Sub SC()
   Dim Cl As Range
   Dim Dic As Object
   Dim Yr As Long
 
   Set Dic = CreateObject("scripting.dictionary")
   Dic.comparemode = 1
   Yr = Sheets("Score Card").Range("A1").Value
   With Sheets("Log")
      For Each Cl In .Range("H2", .Range("H" & Rows.Count).End(xlUp))
         If Cl.Offset(, -5).Value = Yr Then Dic(Cl.Value) = Empty
      Next Cl
   End With
   With Sheets("Score Card")
      .Range("D4:AA4").ClearContents
      .Range("D4").Resize(, Dic.Count).Value = Dic.Keys
   End With
   Columns("D:AA").EntireColumn.AutoFit
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi @willow1985 and happy new year!!!

Try this:

VBA Code:
Sub SC()
   Dim Cl As Range
   Dim Dic As Object
   Dim Yr As Long
   Dim lr As Long
 
   Set Dic = CreateObject("scripting.dictionary")
   Dic.comparemode = 1
   Yr = Sheets("Score Card").Range("A1").Value
   With Sheets("Log")
      lr = .Range("H" & Rows.count).End(xlUp).Row
      If lr = 1 Then
        MsgBox "No Data"
        Exit Sub
      End If
      For Each Cl In .Range("H2:H" & lr)
         If Cl.Offset(, -5).Value = Yr Then Dic(Cl.Value) = Empty
      Next Cl
   End With
   With Sheets("Score Card")
      .Range("D4:AA4").ClearContents
      .Range("D4").Resize(, Dic.count).Value = Dic.Keys
   End With
   Columns("D:AA").EntireColumn.AutoFit
End Sub
 
Upvote 0
Hummm...
I guess the OP was asking for checking that column H does contain any date with the selected year.
For this, I should modify the output portion of the code; for example:
VBA Code:
Sub SC()
Dim Cl As Range
Dim Dic As Object
Dim Yr As Long

Set Dic = CreateObject("scripting.dictionary")
Dic.comparemode = 1
Yr = Sheets("Score Card").Range("A1").Value
With Sheets("Log")
   For Each Cl In .Range("H2", .Range("H" & Rows.Count).End(xlUp))
      If Cl.Offset(, -5).Value = Yr Then Dic(Cl.Value) = Empty
   Next Cl
End With
With Sheets("Score Card")
    If Dic.Count > 0 Then
        .Range("D4:AA4").ClearContents
        .Range("D4").Resize(, Dic.Count).Value = Dic.Keys
    Else
        MsgBox ("No data for year=" & Yr)
    End If
End With
Columns("D:AA").EntireColumn.AutoFit
End Sub
 
Upvote 0
I guess the OP was asking for checking that column H does contain any date with the selected year.
Good point. In that case, the macro could check the year from the start of the code:
VBA Code:
Sub SC()
   Dim Cl As Range
   Dim Dic As Object
   Dim Yr As Long
   Dim f As Range
 
   Set Dic = CreateObject("scripting.dictionary")
   Dic.comparemode = 1
   Yr = Sheets("Score Card").Range("A1").Value
   With Sheets("Log")
      Set f = .Range("C:C").Find(Yr, , xlValues, xlWhole)
      If f Is Nothing Then
        MsgBox "No Data"
        Exit Sub
      End If
      For Each Cl In .Range("H2", .Range("H" & Rows.count).End(xlUp))
         If Cl.Offset(, -5).Value = Yr Then Dic(Cl.Value) = Empty
      Next Cl
   End With
   With Sheets("Score Card")
      .Range("D4:AA4").ClearContents
      .Range("D4").Resize(, Dic.count).Value = Dic.Keys
   End With
   Columns("D:AA").EntireColumn.AutoFit
End Sub
 
Upvote 0
Hi @willow1985 and happy new year!!!

Try this:

VBA Code:
Sub SC()
   Dim Cl As Range
   Dim Dic As Object
   Dim Yr As Long
   Dim lr As Long
 
   Set Dic = CreateObject("scripting.dictionary")
   Dic.comparemode = 1
   Yr = Sheets("Score Card").Range("A1").Value
   With Sheets("Log")
      lr = .Range("H" & Rows.count).End(xlUp).Row
      If lr = 1 Then
        MsgBox "No Data"
        Exit Sub
      End If
      For Each Cl In .Range("H2:H" & lr)
         If Cl.Offset(, -5).Value = Yr Then Dic(Cl.Value) = Empty
      Next Cl
   End With
   With Sheets("Score Card")
      .Range("D4:AA4").ClearContents
      .Range("D4").Resize(, Dic.count).Value = Dic.Keys
   End With
   Columns("D:AA").EntireColumn.AutoFit
End Sub
I am getting an error with your code on line:
Me.Range("D4").Resize(, Dic.Count).Value = Dic.Keys

1673445897935.png
 
Upvote 0
That's weird.
In my tests if there is no year 2023, then send the message, we will have to try the option that @Anthony47 put.
Try like this, with double validation:

VBA Code:
Sub SC()
   Dim Cl As Range
   Dim dic As Object
   Dim Yr As Long
   Dim f As Range
 
   Set dic = CreateObject("scripting.dictionary")
   dic.comparemode = 1
   Yr = Sheets("Score Card").Range("A1").Value
   With Sheets("Log")
      Set f = .Range("C:C").Find(Yr, , xlValues, xlWhole)
      If f Is Nothing Then
        MsgBox "No Data"
        Exit Sub
      End If
      For Each Cl In .Range("H2", .Range("H" & Rows.count).End(xlUp))
         If Cl.Offset(, -5).Value = Yr Then dic(Cl.Value) = Empty
      Next Cl
   End With
   With Sheets("Score Card")
    If dic.count > 0 Then
      .Range("D4:AA4").ClearContents
      .Range("D4").Resize(, dic.count).Value = dic.Keys
    Else
      MsgBox "No Data, dic empty"
    End If
   End With
   Columns("D:AA").EntireColumn.AutoFit
End Sub
 
Upvote 0
That's weird.
In my tests if there is no year 2023, then send the message, we will have to try the option that @Anthony47 put.
Try like this, with double validation:

VBA Code:
Sub SC()
   Dim Cl As Range
   Dim dic As Object
   Dim Yr As Long
   Dim f As Range
 
   Set dic = CreateObject("scripting.dictionary")
   dic.comparemode = 1
   Yr = Sheets("Score Card").Range("A1").Value
   With Sheets("Log")
      Set f = .Range("C:C").Find(Yr, , xlValues, xlWhole)
      If f Is Nothing Then
        MsgBox "No Data"
        Exit Sub
      End If
      For Each Cl In .Range("H2", .Range("H" & Rows.count).End(xlUp))
         If Cl.Offset(, -5).Value = Yr Then dic(Cl.Value) = Empty
      Next Cl
   End With
   With Sheets("Score Card")
    If dic.count > 0 Then
      .Range("D4:AA4").ClearContents
      .Range("D4").Resize(, dic.count).Value = dic.Keys
    Else
      MsgBox "No Data, dic empty"
    End If
   End With
   Columns("D:AA").EntireColumn.AutoFit
End Sub
Still get the error unfortunately. If you are willing to look at a copy of the document with sensitive information removed go to link: Data All Years.xlsm

I am not sure why I keep getting the same error.
 
Upvote 0
The problem is not in the macro.
The macro works fine. The problem is in the code that you have in the sheet that is activated when you modify cell A1 😅
I also corrected that code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Cl As Range
  Dim dic As Object
  Dim f As Range
 
  If Target.CountLarge > 1 Then Exit Sub
  If Target.Address(0, 0) = "A1" Then
 
    Set dic = CreateObject("scripting.dictionary")
    dic.comparemode = 1
    With Sheets("Log")
      Set f = .Range("C:C").Find(Target.Value, , xlValues, xlWhole)
      If f Is Nothing Then
        MsgBox "No Data"
        Target.Select
        Exit Sub
      End If
      For Each Cl In .Range("H2", .Range("H" & Rows.Count).End(xlUp))
        If Cl.Offset(, -5).Value = Target.Value Then dic(Cl.Value) = Empty
      Next Cl
    End With
   
    Me.Range("D4:AA4").ClearContents
    Me.Range("D4").Resize(, dic.Count).Value = dic.Keys
    ActiveWorkbook.Worksheets("Score Card").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Score Card").Sort.SortFields.Add Key:=Range( _
      "D4:T4"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
      xlSortNormal
    With ActiveWorkbook.Worksheets("Score Card").Sort
      .SetRange Range("D4:Z4")
      .Header = xlGuess
      .MatchCase = False
      .Orientation = xlLeftToRight
      .SortMethod = xlPinYin
      .Apply
    End With
    Columns("D:AA").EntireColumn.AutoFit
  End If
End Sub

I return the updated file.


-----
 
Upvote 0
Solution

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top