Consulting

Results 1 to 6 of 6

Thread: Storing range object temporarily in code not possible?

  1. #1
    VBAX Regular
    Joined
    Oct 2008
    Posts
    31
    Location

    Storing range object temporarily in code not possible?

    I am looping through some cells. I want to save the formats and values temporarily into some variable but I can't seem to do it. If I uncomment this line:
    d.value = c.value
    I get error: Object variable or with block variable not set.

    I don't want to store the cells in excel sheet but just temporarily in my code. Can I achieve this??

    Here's my code, I also attached a sample fiel with the code and some data.

    [vba]Sub macro()
    Dim c As Range
    Set c = ActiveCell
    Dim d As Range

    Do While (IsEmpty(c) = False)
    ' Why I can't do this?
    'd.Value = c.Value
    MsgBox c.Value
    Set c = c.Offset(0, 1)
    Loop
    End Sub[/vba]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Your sample is not clear as to how the saved value is used. You can use a Global Variable somthing like this

    [vba]Option Explicit

    Dim d As Range

    Sub macro1()
    Dim c As Range
    Set c = ActiveCell
    Set d = c
    End Sub


    Sub macro2()
    MsgBox d.Value
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3

  4. #4
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Are you trying to store the values and formulas as an array
    [VBA]Dim c As Range
    Dim imageOfC As Variant

    Set c = Range("A1:B2")

    imageOfC = c.Formula
    MsgBox imageOfC(2, 1) & ", " & imageOfC(2, 2): Rem 2, =A2+1[/VBA]

  5. #5
    VBAX Regular
    Joined
    Oct 2008
    Posts
    31
    Location
    Hmmm.. Let me try to refrase the question:

    If I have declared:
    dim c as Range
    Can I use
    c.value = 10
    if c has not been set to refer to any existing cell in worksheet? Just these two lines produce error, but is there a way I can create instances of the Range class without instances pointing to some specific cells?

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Maybe I'll get in over my head here, but here goes nuthin...

    A Range is an Object, thus, while you have declared or dimensioned the variable's type as Range, it does not refer to anything, until you Set a reference to a range.

    So... you would be attempting to set the value of a property (in this case the .Value property), wherein the property belongs to an Object that does not yet exist.

Posting Permissions

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