+ Reply to Thread
Results 1 to 8 of 8

VBA code if cell is null - skip

  1. #1
    Registered User
    Join Date
    07-16-2011
    Location
    Bolton, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Smile VBA code if cell is null - skip

    Hi, I wonder if any-one could help. I have a script to save each row in an excel file as a text file. This works fine, but I now want it to ignore a column if there is no data in it, rather than show the heading and a blank field.
    So if cell a is blank, then go to cell b etc...

    The part of the script I want to edit is:

    FileNum = FreeFile
    With wks
    For iRow = 1 To .Cells(.Rows.Count, "e").End(xlUp).Row
    Close #FileNum
    Open myFolderName & .Cells(iRow, "e").Value For Output As FileNum
    Print #FileNum, myHeader, .Cells(iRow, "a").Value
    Print #FileNum, myHeader2, .Cells(iRow, "b").Value
    Print #FileNum, myHeader3, .Cells(iRow, "c").Value
    Print #FileNum, myHeader4, .Cells(iRow, "d").Value
    Print #FileNum, myHeader5, .Cells(iRow, "e").Value


    Next iRow
    End With

    Any help would be appreciated.

    Many Thanks.
    Louise
    Last edited by louisefleming; 07-17-2011 at 06:00 PM.

  2. #2
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: VBA code if cell is null - skip

    Welcome on forum ...
    Take few minutes to read forum rules (use "code" tags for show your implementation code)

    Try this code, I hope it will works as you want
    Please Login or Register  to view this content.
    Best Regards
    MaczaQ
    Last edited by MaczaQ; 07-16-2011 at 06:12 PM.

  3. #3
    Registered User
    Join Date
    07-16-2011
    Location
    Bolton, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA code if cell is null - skip

    Hi MacZaq, Many Thanks for that, however t did give me an error.
    It gave "Compile error, variable not defined at
    for i = 0 to ubound(myHeader)

    I tried changing to to iRow, but that then just gave me another error at the ubound part.

    As you can probably guess, I'm new to vb, so sorry if I appear a bit dense.

    Thanks again,
    Louise.

  4. #4
    Valued Forum Contributor MaczaQ's Avatar
    Join Date
    06-03-2011
    Location
    Poland
    MS-Off Ver
    Excel 2003 / XP
    Posts
    510

    Re: VBA code if cell is null - skip

    If you have "option explicit" you should declare all used variable

    Try this:

    Please Login or Register  to view this content.
    Best Regards
    MaczaQ
    Last edited by MaczaQ; 07-17-2011 at 04:36 PM.

  5. #5
    Registered User
    Join Date
    07-16-2011
    Location
    Bolton, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA code if cell is null - skip

    Hi, Thanks again but I'm still getting an error "Expected array". The full code I'm using now is:



    Please Login or Register  to view this content.
    Cheers,
    Louise

  6. #6
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: VBA code if cell is null - skip

    Hi Louise,

    MyHeader needs to be declared as a Variant rather than as a String.
    Hope that helps,

    Colin

    RAD Excel Blog

  7. #7
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: VBA code if cell is null - skip

    Also, you need to be careful with your variable declarations:
    Please Login or Register  to view this content.
    In VBA, this line of code will declare FileNum and i as Variants, and iRow as an Integer.

    To declare all three as Integers, you should use either of these:
    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    Also, in VBA, an Integer is 16-bit and can hold whole numbers from -32,768 to 32,767. The number of rows in an Excel worksheet can exceed 32,767, which means that the code could be prone to an overflow error. Whenever you're using variables which reflect row numbers, use Long data types.

  8. #8
    Registered User
    Join Date
    07-16-2011
    Location
    Bolton, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA code if cell is null - skip

    Thanks so much for your help Guys. The script now works, really appreciate you taking your time and effort to help me.

    Thanks again,
    Louise.

+ 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