Consulting

Results 1 to 20 of 20

Thread: I can never get this simple code working

  1. #1
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location

    I can never get this simple code working

    I can never get this line of code to work.

    [VBA]FinalRow = Cells(65536, 1).End(x1up).Row[/VBA]

    I declare it publicly at the beginning as

    [VBA]Public FinalRow as Range[/VBA]

    What am I doing wrong?

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    [vba]
    FinalRow = Cells(A65536, 1).End(x1up).Row
    [/vba]
    Lastrow = ActSh.Cells(Rows.Count, 1).End(xlUp).Row
    Explain each part of this code please. I have a general idea of what it is doing, selecting the last row of code w/ data and selecting moving up to avoid missing data in non-contiguous cells.

    Answer:from Jake
    Lastrow is a variable that will hold this value.
    ActSH is an object that was (presumably) set to a certain worksheet. Like this:
    Set ActSh = Sheets("PCCombined_FF")
    Cells is a way to refer to a specific cell on the worksheet by refering to the cell row, then the cell column. In this case, the row will be 65536 (since that is the current count of rows for an Excel worksheet), and 1 referd to column A. So this is the cell A65536.
    End(xlUp) will move the reference up. Assuming that A65536 is blank, then it will move up, skipping all blank cells and come to the first non-blank cell encountered.
    .Row will return the row of the cell we get after the End(xlUp).
    Basically what this does is get the last row used in a certain column.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Its always something simple
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  4. #4
    VBAX Contributor compariniaa's Avatar
    Joined
    Jun 2006
    Location
    Santa Clarita, CA
    Posts
    117
    Location
    i think you may want to double-check that....he doesn't need the "A" in front of 65536, because he's declaring the column with the 1 in the cells statement

    the problem is how he's declaring the variable. if he wants a range variable, he needs to drop the ".row"
    if he wants a long variable, he needs to change "as Range" to "as Long"

  5. #5
    VBAX Contributor compariniaa's Avatar
    Joined
    Jun 2006
    Location
    Santa Clarita, CA
    Posts
    117
    Location
    oops...forgot to post some code. here are some options:

    [VBA]Public FinalRow as Range
    'then, instead of FinalRow=.....
    set FinalRow=cells(65536,1).end(xlup)

    'or if you just want the row number
    Public FinalRow as Long
    FinalRow=cells(65536,1).end(xlup).row[/VBA]

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Sorry, I was in kind of a hurry and I wanted Daniel to read the quote. Maybe this will work:
    [VBA]
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    lol I just want the row number

    So I declared it As long like you said and I am still getting an error message saying variable not defined and it highlights x1up

  8. #8
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Option Explicit
    [VBA]
    Sub x()
    Dim FinalRow As Long
    FinalRow = Cells(65536, 1).End(xlUp).Row
    MsgBox FinalRow
    End Sub

    [/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  9. #9
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    [VBA]Sub y()
    Dim FinalRow As Long
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    MsgBox FinalRow
    End Sub[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  10. #10
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Lucas I tried both of them and neither work and I have option explicit at the top. Also what does Option explicit mean and why do I need it? I am sorry

  11. #11
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    They both work for me.....see attached
    you must have data in col A for this to work
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  12. #12
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    If you need to check a different column change the 1 to match your column
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  13. #13
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    I know I need the column number to be correct. I do have info in the 1st column and that is what I am using. However, it gives me an error saying variable not defined and it hightlights x1up

  14. #14
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    It should be xlUp. ie lower case L instead of 1.

  15. #15
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Daniel, its xlUp not x1up it is an L not a 1
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  16. #16
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    no wonder it wouldn't work. Like I said its usually something small
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  17. #17
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    thank you in my book it looks like a 1

  18. #18
    VBAX Master
    Joined
    Jun 2006
    Posts
    1,091
    Location
    Now, I am trying to learn my mistakes; This code is also giving me an error:

    [VBA]For i = 2 To FinalRow
    Select Case Cells(i, 1).Value
    Case "Customer"
    CustomerTab
    Case "Product:"
    ProductTab1
    Case "ShipTo"
    ShipTo2
    Next i[/VBA]

    It is saying next without for. Witch makes no sense since I can see the For right there.

  19. #19
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    You need an End Select statement at the bottom[vba]
    For i = 2 To FinalRow


    Select Case Cells(i, 1).Value
    Case "Customer"
    CustomerTab
    Case "Product:"
    ProductTab1
    Case "ShipTo"
    ShipTo2
    End Select
    Next i
    [/vba]

  20. #20
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Can you start a new thread?

    By the way do you have Intellisense turned on? (Tools>Option>Auto List Members)

Posting Permissions

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