+ Reply to Thread
Results 1 to 3 of 3

Thread: Set range still shows nothing

  1. #1
    Registered User
    Join Date
    08-12-2009
    Location
    Sacramento, CA
    MS-Off Ver
    Excel 2003
    Posts
    7

    Question Set range still shows nothing

    Hi All,

    I have the following code:

    Private Sub getrange(ByRef therange As Range, wsheet As Worksheet, strrange As String)
        Dim rowcount As Long
        Dim colindex As Integer
        Dim temprange As Range
        Dim rowstart As Integer
        rowstart = Sheet1.therowstart
        wsheet.Activate
        Debug.Print wsheet.Name
        colindex = getcolumn(strrange)
        rowcount = wsheet.Cells(65536, colindex).End(xlUp).Row
        If rowcount < Sheet1.therowstart Then
            rowcount = Sheet1.therowstart
        End If
        Debug.Print "sheet1.therowstart: " & Sheet1.therowstart
        Debug.Print "colindex: " & colindex
        Debug.Print "rowcount: " & rowcount
        Debug.Print "value:" & Cells(rowstart, colindex)
        Set therange = Range(Cells(rowstart, colindex), Cells(rowcount, colindex))
        Debug.Print therange(1, 1).Row
        Debug.Print therange(1, 1).column
        Debug.Print therange(1, 1).Text
    Here is my debug:

    Sheet1
    sheet1.therowstart: 3
    colindex: 17
    rowcount: 3
    value:
    3
    17
    My issue is that somehow, even though I'm telling Excel to go to cells(3,17) on the active sheet (wsheet), it's not seeing a value (or much less anything else). Consequently, I am unable to identify a range.

    I can't even add cells(1,1).select to my code without getting a 1004 error.

    Does anyone have any idea what might be causing this and how I can go about fixing it?

    Thanks.

    Regards,

    William

  2. #2
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    Re: Set range still shows nothing

    If your code is located in a worksheet code module rather than a normal module, then any reference to Cells or Range that is not explicitly qualified with a worksheet object will refer to the sheet containing the code. (Since that is not the active sheet, you cannot select a cell that way.) You need to use:
    Set therange = wsheet.Range(wsheet.Cells(rowstart, colindex), wsheet.Cells(rowcount, colindex))
    for example (or a With...End With block for neatness)

  3. #3
    Registered User
    Join Date
    08-12-2009
    Location
    Sacramento, CA
    MS-Off Ver
    Excel 2003
    Posts
    7

    Talking Re: Set range still shows nothing

    Perfect.

    Thank you.

    Regards,

    William

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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.2.0