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

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
929
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
 
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.


-----
I apologize, I thought the issue was with the other. I did not write this one originally and still do not fully understand it.

Thank you so much for all of your help! Works perfect now 😊
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,224,816
Messages
6,181,141
Members
453,021
Latest member
Justyna P

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