+ Reply to Thread
Results 1 to 7 of 7

emptyRow code giving Compile Error "Variable not defined"

Hybrid View

  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    emptyRow code giving Compile Error "Variable not defined"

    I am very new to VBA and occurred a problem that I recently received some great advice from an excel Forum user, unfortunately it has thrown up another problem that I cannot seem to get around!

    (I have posted a new thread rather than keep pestering my initial helper as it is not specific to my first posts title and would not help others if google-ing a similar problem.)

    My problem has come because I have now included ‘option explicit’ at the top of my code so I can debug in the IDA, which is helping me greatly define all my variables etc.
    I have a user form that once completed is submitted via a submit button and puts all the values on the user form into a sheet on my excel workbook. My code to submit to the correct empty rows is:


    Dim ws As Worksheet
    Set ws = Worksheets(“Sheet1”)
    
    emptyRow = WorksheetFunction.CountA(Range(“B:B”)) + 5
    
    ws.Cells(emptyRow, 2).Value = TextBox1.Value
    ws.Cells(emptyRow, 3).Value = TextBox2.Value
    ws.Cells(emptyRow, 4).Value = TextBox3.Value

    When I debug now it highlights the ‘emptyRow’ in the: emptyRow = WorksheetFunctions.CountA(Range(“B:B”)) + 5 line and gives the complie error: “Variable not defined”


    Can anyone help please…

    Many thanks

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: emptyRow code giving Compile Error "Variable not defined"

    Try this,

    Dim ws As Worksheet
    Set ws = Worksheets(“Sheet1”)
    
    emptyRow = Application.WorksheetFunction.CountA(Range(“B:B”)) + 5
    
    ws.Cells(emptyRow, 2).Value = TextBox1.Value
    ws.Cells(emptyRow, 3).Value = TextBox2.Value
    ws.Cells(emptyRow, 4).Value = TextBox3.Value
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  3. #3
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: emptyRow code giving Compile Error "Variable not defined"

    Hi JapanDave
    Thanks for the reply,
    When I put this code into my sub it says Compile Error: Expected list separator or ) while highlighting the : between the (Range("B:B"))

  4. #4
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: emptyRow code giving Compile Error "Variable not defined"

    Sorry JapanDave, when copying and pasting from your post I received that compile error but when I simply inserted .Application to my original line of code it did not produce the error but when I run the debugger again it reverts to my original problem of highlighting the ‘emptyRow’ in the: emptyRow = Application.WorksheetFunctions.CountA(Range(“B:B”)) + 5 line and gives the complie error: “Variable not defined”

  5. #5
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: emptyRow code giving Compile Error "Variable not defined"

    In that case try this.

    Are you using option explicit?

    Dim ws As Worksheet, emptyRow&
    Set ws = Worksheets(“Sheet1”)
    
    emptyRow = Application.WorksheetFunction.CountA(Range(“B:B”)) + 5
    
    ws.Cells(emptyRow, 2).Value = TextBox1.Value
    ws.Cells(emptyRow, 3).Value = TextBox2.Value
    ws.Cells(emptyRow, 4).Value = TextBox3.Value
    And if you are trying to find the first empty cell in row I would use this.

    Dim ws As Worksheet, emptyRow&
    Set ws = Worksheets(“Sheet1”)
    
    emptyRow = cells(Rows.Count, 2).end(xlup).row + 5
    
    ws.Cells(emptyRow, 2).Value = TextBox1.Value
    ws.Cells(emptyRow, 3).Value = TextBox2.Value
    ws.Cells(emptyRow, 4).Value = TextBox3.Value

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: emptyRow code giving Compile Error "Variable not defined"

    dim emptyrow as long
    since it matches the existing dim statements and type declaration characters are, imo, a mite old fashioned
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  7. #7
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: emptyRow code giving Compile Error "Variable not defined"

    Thank you very much JapanDave, i've applied the changes and works perfectly now

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1