+ Reply to Thread
Results 1 to 10 of 10

VBA. Determine last column used

  1. #1
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    VBA. Determine last column used

    Hi I have a macro code as below

    Please Login or Register  to view this content.
    Essentially what this code does is taking information across 10 sheets and then populates the information in a meaningful way. The code itself works just fine but its only very resently I realised it has a flaw to determine the last column used (lcol).
    Please Login or Register  to view this content.
    The section above only determines the last column as per row 2. I would need the code to check the last column usage as far as the data goes (e.i. across all rows as per column B). Or in simple terms, the code should understand which is the furthest column that has any sort of data in it.

    I have included an example workbook with the code above along with some comments.


    I would be extremely thankful if someone could help me to get it right.
    Cheers
    Rain
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: VBA. Determine last column used

    One way

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA. Determine last column used

    Try this
    Please Login or Register  to view this content.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  4. #4
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: VBA. Determine last column used

    Thank you Jason, Thank you Arlu1201

    Jason
    I used the line you provided with small modification

    Please Login or Register  to view this content.
    I think with the example workbook, the textboxes, arrows etc affected the UsedRange. Before I used the amended code it not include the information as per last column used (it excluded the last column). Plus 1 (+ 1) did the trick. Thank you for your help. Cheers

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA. Determine last column used

    The usedrange may not always give you the right answer.

  6. #6
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: VBA. Determine last column used

    Quote Originally Posted by arlu1201 View Post
    The usedrange may not always give you the right answer.
    I agree, having no data in column A was something that I didn't take into consideration with my suggestion.

    Please Login or Register  to view this content.
    as suggested by Arlu is the preferable option.

  7. #7
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: VBA. Determine last column used

    Hi arlu1201, jason.b75
    I get Run-time error '1004': Application-defined or object-defined error with the code below.

    Please Login or Register  to view this content.
    it stops at
    Please Login or Register  to view this content.
    What am I missing? I would appreciate any help.
    Cheers
    Rain

  8. #8
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: VBA. Determine last column used

    You're trying to use the value of j before it has been given a value.

  9. #9
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: VBA. Determine last column used

    Thanks for quick response Jason.

    I have to admit I'm still light years away of ever really understanding VBA. Just when I think I have reached some sort of beginner level the whole house comes down. I'm such a newb

    I moved "If Worksheets("Data_shift_HOLS").Cells(i, j).Value <> "" Then" down by two lines like this:
    Please Login or Register  to view this content.
    It now stops at
    Please Login or Register  to view this content.
    It almost feels like catch 22 where I can not figure out how to change one thing without affecting the other.
    Any chance you could correct my code?
    I would be very grateful. Sorry to be bit of pain.

  10. #10
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: VBA. Determine last column used

    You had 2 lines around the wrong way, see how this goes,

    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    01-29-2011
    Location
    Bristol, England
    MS-Off Ver
    Excel 2003
    Posts
    471

    Re: VBA. Determine last column used

    Looks to be working a treat.
    Many thanks

+ 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