CheekyDevil

New Member
Joined
Apr 15, 2018
Messages
20
Hi experts,

I was trawling through countless pages of Google seeking an answer to this, with no success.

I have a VBA project throwing a "Runtime91" error advising a variable has not been declared, but doesn't tell me what variable. In Debug, it refers me to a single line of code that shows my userform on a command button press.

The only other indication I've had is the following codes "NumberFormat" but this should not be a variable, simply how the date and time is to be formatted when entered.

I've got OE at the top and still no joy.

Is there any code I can implement or any error checking options in VBA that will point me to the variable not declared?

Here's the code piece I'm referring to above:

Code:
Option Explicit
Dim ws As Worksheet
Dim LastRow As Long


Private Sub UserForm_Initialize()


  Set ws = Sheet9

'Focus Log
    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Adds Date
    ws.Range("A" & LastRow).Value = Date
    NumberFormat = "dd/mm/yyyy"
    LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1 'Adds Time
    ws.Range("B" & LastRow).Value = Time
    NumberFormat = "hh:mm:ss"
    LastRow = ws.Range("C" & Rows.Count).End(xlUp).Row + 1 'Adds Username
    ws.Range("C" & LastRow).Value = Environ("username")
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
You haven't declared where the number format will be applied !
Also, I'd probably use
Code:
Set ws=sheets("Sheet9")
or
Code:
Set ws=sheet(9)

Code:
With ws
    LastRow = .Range("A" & Rows.Count).End(xlUp).Row + 1 'Adds Date
    .Range("A" & LastRow).Value = Date
    .Range("A" & LastRow).NumberFormat = "dd/mm/yyyy"
    LastRow = .Range("B" & Rows.Count).End(xlUp).Row + 1 'Adds Time
    .Range("B" & LastRow).Value = Time
    .Range("B" & LastRow).NumberFormat = "hh:mm:ss"
    LastRow = .Range("C" & Rows.Count).End(xlUp).Row + 1 'Adds Username
    .Range("C" & LastRow).Value = Environ("username")
    End With
 
Last edited:
Upvote 0
:eek: WOW was I THAT blind! Thank you SO much Michael! You have saved me hours of frustration in one swoop, and so quickly! Had not even seen this line was written incorrectly.

Forever grateful for your help and I'll now go back to VBA school to learn the basics again :P

Thanks again!
 
Upvote 0
Happens to us all...in my case it's called seniors moments !!...(y)
 
Upvote 0
Also, in the VB Editor, if you select Tools > Options > General > & select "Break in Class Module"
Rather than showing you the Userform.show line, it will highlight the problem within the initialise event.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,692
Members
448,979
Latest member
DET4492

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