Home » excel » excel – Why am I getting this error: Object Variable or With block variable not set?

excel – Why am I getting this error: Object Variable or With block variable not set?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have found out what causes this error but I cannot pinpoint where it is coming from in my code. I even tested this macro in a separate worksheet in excel and it worked but in this worksheet it is not working. Can anyone guess as to why or offer any workarounds? I put a comment on the line with the error. Assume data is put in correctly.

Private Sub CommandButton1_Click()
    Dim startDate As Date, endDate As Date, reason As String, name As String
    name = InputBox("Please enter the name of the SLG as appears in column 1 on the worksheet:")
    startDate = InputBox("Please enter the start date in MM/DD/YYYY format:")
    endDate = InputBox("Please enter the end date in MM/DD/YYYY format:")
    reason = InputBox("Please short description for the absence:")

    Dim rng1 As Range, columnNumberStart As Integer, rowNumber As Integer, columnNumberEnd As Integer, test1 As String, test2 As String

    Worksheets("FY-15 Schedule").Activate

    Set rng1 = ActiveSheet.UsedRange.Find(name)
    rowNumber = rng1.Row

    Set rng1 = ActiveSheet.UsedRange.Find(startDate) 
    columnNumberStart = rng1.Column 'Says Error is on this line

    Set rng1 = ActiveSheet.UsedRange.Find(endDate)
    columnNumberEnd = ActiveSheet.UsedRange.Find(endDate).Column

    test1 = Cells(rowNumber, columnNumberStart).Address
    test2 = Cells(rowNumber, columnNumberEnd).Address

    Dim rng2 As Range
    Set rng2 = Range(test1, test2)


    rng2.Value = reason

End Sub
How to&Answers:

First, explicitly coerce the date from the inputbox input:

startDate = CDate(InputBox("Please enter the start date in MM/DD/YYYY format:"))

And do the same for the other date field.

Then, there is still possible source of this same error: when the .Find method has returned Nothing to the range object, (e.g., the date is not found/doesn’t exist in the sheet) then you are essentially doing Nothing.Column, which is an error.

If the date does not exist in the sheet, this will always raise an error, which you could trap like this, or use GoTo statements to return to the inputbox, etc…

Set rng3 = ActiveSheet.UsedRange.Find(startDate) 
If rng3 Is Nothing Then 
    MsgBox "Start date" & Cstr(startDate) & " not found!", vbInformation
    Exit Sub
End If
columnNumberStart = rng1.Column 

Answer:

I figured it out! The problem was this: It was not finding the dates because the dates were being generated by a formula!! My first date was manually typed in but the rest of them were a fill series from the second cell which was A2=A1+1 and all the way down the line. I wanted it that way so the start date could be changed but for some reason excel was not recognizing those as dates!?! How weird?!?!

Problem solved though. What you said was correct about it not finding the date but appearance wise when looking at the spreadsheet it is there in plain sight but when looking at the cells their values were formulas.