Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 338841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:28:01+00:00 2026-05-12T10:28:01+00:00

I’m having trouble with the way I designed this little report I’m making. Is

  • 0

I’m having trouble with the way I designed this little report I’m making. Is it possible to create a variable for a Range object in Excel VBA, for the purposes of applying the formatting to another Range? Here is my example:

I’m creating a dictionary from the Microsoft Scripting Runtime library:

Dim d as Scripting.Dictionary

With this I’m adding labels, values, and (trying to add) Ranges.

Dim rng as Range    

rng.Font.Bold = True
d.Add 1, Field("test1", 12345, rng)
rng.Font.Bold = False
d.Add 2, Field("TestTwo", "Testing field", rng)
rng.HorizontalAlignment = xlCenter
d.Add 3, Field("threeeee", 128937912, rng)

Dim key As Variant

For Each key In d.keys
    Range("A" & key).value = d(key).Label
    Set Range("B" & key).value = d(key).rng

Next key

Here is my Field function:

Private Function Field(Label As String, val As Variant, rng As Range) As cField

    Dim f As New cField
    f.Label = Label
    f.val = val
    Set f.rng = rng
    Set Field = f

End Function

And here is my cField class:

Option Explicit

Dim mVarValue As Variant
Dim mStrLabel As String
Dim mRng As Range

Property Let val(ByVal val As Variant)
    mVarValue = val
End Property

Property Get val() As Variant
    val = mVarValue
End Property

Property Let Label(ByVal val As String)
    mStrLabel = val
End Property

Property Get Label() As String
    Label = mStrLabel
End Property

Property Let rng(ByVal val As Range)
    Set mRng = val
End Property

Property Get rng() As Range
    Dim a As Range
    a.value = mVarValue
    Set rng = a
End Property

The idea is that the key in the dictionary is going to be the row location for the field. This way if changes need to be made to the report I’m making, the only thing that needs to be changed is the key for that particular value in the dictionary. I have been successful storing the label for the value, and the value itself, but I also want to store the formatting for that Range (bold, justification, borders, etc…).

I get a ‘Run-time error ’91’: Object variable or With block variable not set’ error on the line immediately following the rng declaration. I’m wondering if its not possible to have a generic Range that doesn’t have a location on a sheet, or if somehow my syntax is off.

Any help would be greatly appreciated! 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:28:01+00:00Added an answer on May 12, 2026 at 10:28 am

    Is it possible to create a variable for a Range object in Excel VBA, for
    the purposes of applying the
    formatting to another Range?

    I’m wondering if its not possible to have a generic Range that doesn’t have
    a location on a sheet…

    The short answer is no.

    The quick answer is…I suggest creating a “format” worksheet, which can be hidden or very hidden, that contains ranges, or Named Ranges, with the formatting you need. This allows you to range.Copy the “formatted” range then use range.PasteSpecial xlPasteFormats.

    I dislike overwriting the user’s clipboard, but it is difficult to programmatically copy the formatting of one range to another. I use this method in numerous solutions because it is flexible, maintainable, reusable, and does not rely on complex code. Moreover, I can visually change formatting without touching code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.