+ Reply to Thread
Results 1 to 17 of 17

Function returning error

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-03-2008
    Location
    Somerset, England
    MS-Off Ver
    XP, 2003, 2007 - depends on location
    Posts
    185

    Function returning error

    Hi all,

    I have a function that works out which is the last column containing data on a worksheet, but for some reason it is alwys retuning 0. Just wondering if anybody is able to spot where I am going wrong.

    Here is the function I am using -

    Public Function LastColumn(Optional WorksheetName As String) As Integer
    ' Last Column Function
    ' Function finds the last column in a worksheet with data in it
         
        If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name
        With Worksheets(WorksheetName)
            On Error Resume Next
            LastColumn = .Cells.Find("*", .Cells(1), xlFormulas, xlWhole, xlByColumns, xlPrevious).Column
            If Err <> 0 Then LastColumn = 0
        End With
         
    End Function
    And here are the ways I have tried retrieving data from the function, all of which return 0 -

    iColumn = LastColumn(wS22.Name)
    MsgBox iColumn
    MsgBox LastColumn(wS22.Name)
    Dim SheetToCheck As String
    SheetToCheck = wS22.Name
    iColumn = LastColumn(SheetToCheck)
    MsgBox iColumn
    When I take the Error handleing out I am getting the error 'Object Variable or With Block Variable not set'. All variables are correctly declered.

    Any help is greatly appriciated.
    Thanks.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Function returning error

    So you single-step through the code and what happens?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Contributor
    Join Date
    09-03-2008
    Location
    Somerset, England
    MS-Off Ver
    XP, 2003, 2007 - depends on location
    Posts
    185

    Re: Function returning error

    If I leave the error handlers in the Function, noting happens, but LastColumn is returned as 0 (as set if 'Error <> 0'). But if I remove that that I get an error (Run-time error '91';, which is the one mentioned above) the first time I go into the meat of the function.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: Function returning error

    Do you actually have any data in the sheets you are testing on?
    Remember what the dormouse said
    Feed your head

  5. #5
    Forum Contributor
    Join Date
    09-03-2008
    Location
    Somerset, England
    MS-Off Ver
    XP, 2003, 2007 - depends on location
    Posts
    185

    Re: Function returning error

    I do indeed have data (Far to much if you ask me!!), I have checked that 'wS22' is the currently selected sheet, and the the correct workbook is selected, but still no joy

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Function returning error

    ws22 as you're using it in the code (ws22.Name) is the CodeName of the sheet, not the sheet name (unless they're the same).

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: Function returning error

    I wanted to make sure it was in a normal module, not a class module.
    It appears the Find is not working - have you done any finds involving searching formats?

  8. #8
    Forum Contributor
    Join Date
    09-03-2008
    Location
    Somerset, England
    MS-Off Ver
    XP, 2003, 2007 - depends on location
    Posts
    185

    Re: Function returning error

    Ah, Ok. Do you mean have I used Find at any other point in the code? If yes, then no, this is the only occurance of it.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: Function returning error

    Doesn't have to be in code. If you have done a Find in the UI, those settings will be used unless your code overrides them specifically.

  10. #10
    Forum Contributor
    Join Date
    09-03-2008
    Location
    Somerset, England
    MS-Off Ver
    XP, 2003, 2007 - depends on location
    Posts
    185

    Re: Function returning error

    You'll have to excuse my noobness here, but what is 'UI', and how would I override the setting in the code?

  11. #11
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,298

    Re: Function returning error

    When using Find it is best to specify every parameter, including the optional ones, to ensure you are getting the Find you think you are!
    UI = User Interface.

+ 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