Consulting

Results 1 to 12 of 12

Thread: Solved: Help ...Passing Arguments

  1. #1

    Solved: Help ...Passing Arguments

    I'd like to pass the argument: Workbooks(1).WorkSheets("Display #2"")

    I'd like to assign the argument as follows but I know that won't work.

    [VBA] ws = Workbooks(1).WorkSheets("Display #2"")

    MyLastCell = LastCell(ws).Address(False,False)
    [/VBA]
    This function returns the last cell on a worksheet that I have implemented.

    The function:

    [VBA] Function Lastcell(ws as ???) as Range
    Code
    With ws
    LastRow% = .cells.Find( more code
    more code
    End Function
    [/VBA]
    How do I define the argument so that I can Pass it???
    The reason is I would like to use the function over different workbooks and sheets.

    Any Help would be appreciated

    Thanks
    Last edited by Joker12; 11-09-2005 at 05:50 PM. Reason: Typo

  2. #2
    I think I have figured this out. It seems to work.
    Is it the correct way of doing it???


    [VBA]MyLastCell = LastCell(Workbooks(1).WorkSheets("Display#2") ).Address(False,False) [/VBA]


    [VBA]
    Function Lastcell(ws as VARIANT) as Range
    Code
    With ws
    LastRow% = .cells.Find( more code
    more code
    End Function
    [/VBA]

  3. #3
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Joker,

    I would think that your variant should be substituted with Worksheet.

    One problem I see, though, that I don't know what you've set MyLastCell to. If it's a string, then you'll need to return the address from the function. If it's a range, then you'll need to use the Set statment to use the function.

    Here's a reusable function that I've adapated from some I use frequently. It will get you the last cell on any worksheet you specify. I've also included an example of how to use it with your sheet name.

    [vba]Function GetLastCell(wks As Worksheet) As Range
    Set GetLastCell = Intersect(wks.Cells.Find(What:="*", After:=wks.Range("A1"), LookIn:=xlValues, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).EntireRow, _
    wks.Cells.Find(What:="*", After:=wks.Range("A1"), LookIn:=xlValues, LookAt:= _
    xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).EntireColumn)
    End Function
    Sub test()
    Dim rngLastCell As Range
    Set rngLastCell = GetLastCell(Workbooks(1).Worksheets("Display#2"))
    MsgBox rngLastCell.Address
    End Sub[/vba]

    Hope it helps,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  4. #4
    Thanks, I replaced "variant" with "worksheet" and my function seems to work the same way. Interesting that I can use either one. Learn something every day.

    Joker12

  5. #5
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Actually, that's totally expected.

    If you do not declare a type for a variable, it automatically sets it up as variant. Variants take the first data type assigned to them.

    So stepping through the following will give you a data type of Variant/String:

    [vba]Sub test()
    Dim testvar
    testvar = ActiveCell.Text

    End Sub[/vba]

    likewise, these are effectively the same
    [vba]Dim testvar
    Dim testvar as Variant[/vba]

    The danger comes when you pass the wrong thing to your variant data type. Say a worksheet to something that should be a string. It will take it, and error further along in the code. The issue there is that it can be very hard to figure out why it's erroring at that point. Variants also take more memory than other data types.

    For those reasons alone, and others as well, it's best to be as specific as possible.

    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I'm going to pick on Ken a little here .. ( )

    Quote Originally Posted by kpuls
    .. you pass the wrong thing to your variant data type.
    Thing? You mean Argument?

    .. a worksheet to something that should be a string. It will take it, and error further along in the code.
    Something like that will error when you try to set the variable, providing the data type does not accept the current argument status .. not further along in the code.

  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by firefytr
    I'm going to pick on Ken a little here .. ( )

    Thing? You mean Argument?
    Yep. You didn't get that?

    Quote Originally Posted by firefytr
    Something like that will error when you try to set the variable, providing the data type does not accept the current argument status .. not further along in the code.
    You misread what I posted, I think... Look at the following:

    [vba]Sub Bad()
    Dim wsName As Variant
    Set wsName = Worksheets(1)

    'Errors here as msgbox cannot take a worksheet
    MsgBox wsName
    End Sub

    Sub Bad2()
    Dim wsName As String

    'Errors here as string cannot take a worksheet
    Set wsName = Worksheets(1)
    MsgBox wsName
    End Sub

    Sub Good()
    Dim wsName As String
    wsName = Worksheets(1).Name
    MsgBox wsName
    End Sub[/vba]

    Obviously neither of the bad procedures will work, but I'd rather have mine error out as shown in Bad2. In Bad, the msgbox could be miles down the code, leaving you scratching your head as to what's going on. Everyone who has ever coded has run into a situation where everything looks right but doesn't work because of something dumb, we all know that.

    Personally, I'd rather have it gag on trying to assign the value or whatever to the variable, as that gives me a really good indication immediately of what went wrong. It also makes sure things will work better later.

    That make more sense?
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by kpuls
    If you do not declare a type for a variable, it automatically sets it up as variant. Variants take the first data type assigned to them.
    Not quite true, they take whatever data type is assigned to them. Consider this code

    [VBA]
    Dim myVar

    myVar = "abc"
    myVar = 17
    [/VBA]

    myVar starts as type Variant/Empty, it changes to Variant/String when the value "abc" is assigned to it, then becomes Variant/Integer when the value 17 is assigned to it. It is precisely because of the data typing looseness that it is a good idea to avoid variants wherever possible.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by firefytr
    Thing? You mean Argument?
    Technically, I believe, parameters are passed and arguments received - but the terminology is not consistently used anywhere.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    LOL!

    See Zack, look what you turned this into!

    Bob, good points. Avoiding Variants as much as possible, I actually did not think if that, but now that you mention it, yes that makes complete sense. I would say that it doesn't change the problems in the code I posted, though, but rather somewhat reinforces my point on scratching your head... especially for a newer coder.

    Tony, I've never been a terminology major. I think I'll stick with "Thing". It covers all the bases!

    Thanks guys! (yeah, you too, Zack )

    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  11. #11
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    I've never had so much fun stirring a pot before.

  12. #12
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    I just knew that you were going to want the last word, too!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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