Results 1 to 3 of 3

Thread: Using the cell property inside the range

  1. #1

    Thread Starter
    Addicted Member epod69's Avatar
    Join Date
    Feb 2005
    Location
    Cambridge, WI - USA
    Posts
    239

    Exclamation Using the cell property inside the range

    Hi everyone.
    I will start with the code:
    VB Code:
    1. ItemCellDbl = CDbl(Worksheets("MEDIA").Range(ItemCell).Text)
    ItemCell is a string, and this code works fine if ItemCell Equals something like "E5".
    If ItemCell Equals "Cells(6, 30), Cells(6, 30)" I get an error:
    Run-time error '1004':
    Application-defined or object-defined error.

    What is strange though is that the following code works just fine:
    VB Code:
    1. Debug.Print CDbl(Worksheets("MEDIA").Range(Cells(6, 30), Cells(6, 30)).Text)

    Does maybe ItemCell being a string have anything to do with this error ?

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Using the cell property inside the range

    Of course. You can't pass a string variable and expect that to be interpreted into an actual reference.
    In the case of a LetterNumber reference, Excel recognises it because the name of the cell, E5, is defined in the Name box in the top corner. You can rename any cell or range to anything you like, or leave them in the A1 default style, but you're still referencing a cell or range by name. It is not interpreting your string and converting that into a cell reference. Hence you can't put in "Cell(6,1)" and expect it to find cell F1, it's looking for a cell or range named "Cell(6,1)".
    In your second example, you're not passing it a string, you're actually giving it cells to look at, so there is no problem.

    zaza
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  3. #3

    Thread Starter
    Addicted Member epod69's Avatar
    Join Date
    Feb 2005
    Location
    Cambridge, WI - USA
    Posts
    239

    Re: Using the cell property inside the range

    Thanks for the reply,
    Could I have ItemCell as something else beside string and still have the code work?

Posting Permissions

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



Click Here to Expand Forum to Full Width