+ Reply to Thread
Results 1 to 6 of 6

write selected range to variable and then write the range saved in variable in to a cell

Hybrid View

ExcelBobb write selected range to... 02-20-2020, 04:32 AM
bakerman2 Re: write selected range to... 02-20-2020, 04:46 AM
nigelog Re: write selected range to... 02-20-2020, 04:52 AM
bakerman2 Re: write selected range to... 02-20-2020, 04:56 AM
ExcelBobb Re: write selected range to... 02-20-2020, 07:14 AM
bakerman2 Re: write selected range to... 02-20-2020, 08:22 AM
  1. #1
    Registered User
    Join Date
    01-29-2020
    Location
    Berlin, Germany
    MS-Off Ver
    2016
    Posts
    5

    write selected range to variable and then write the range saved in variable in to a cell

    Hi everybody,

    How can I write the saved cell range of a variable in a cell?
    For example:

    Dim rng As Range 'this works fine

    Set rng = Selection 'this works fine

    Range("O1").Value = rng 'this does not work, the cell is still blank after that


    Also if I use a msgBox to see if the range is getting stored in my rng var, its shown.
    So it seems to be an issue with the last line, is it even possible to write a range var in to a cell?

    I somehow need a solution to save the range of already selected cells in a worksheet to a variable.
    Then I want to write the content of the variable (for example: A1:B5) in to a cell in the same worksheet.
    Background: I want to use the cell range later on for a range selection, so after the steps above, I want to save the cell range(example: A1:B5) again in a var to use for selection.
    The problem is that, the workbook is getting closed in between saving and using the content of variable, so because of that I want to go for this workaround.
    Last edited by ExcelBobb; 02-20-2020 at 04:35 AM.

  2. #2
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,303

    Re: write selected range to variable and then write the range saved in variable in to a ce

    How about this.

    Dim rng As Range 'this works fine
    
    Set rng = Selection 'this works fine
    
    Range("O1").Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  3. #3
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: write selected range to variable and then write the range saved in variable in to a ce

    edit; posted in error

  4. #4
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,303

    Re: write selected range to variable and then write the range saved in variable in to a ce

    On second thought, if it's the address of the selection you're after.

    Range("O1").Value = rng.Address

  5. #5
    Registered User
    Join Date
    01-29-2020
    Location
    Berlin, Germany
    MS-Off Ver
    2016
    Posts
    5

    Re: write selected range to variable and then write the range saved in variable in to a ce

    Quote Originally Posted by bakerman2 View Post
    On second thought, if it's the address of the selection you're after.

    Range("O1").Value = rng.Address
    Thats it, it worked!

    Thank you

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,303

    Re: write selected range to variable and then write the range saved in variable in to a ce

    You're welcome and thanks for rep+.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

    Also, as a relatively new member of the forum, you may not be aware that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of those who helped.
    Last edited by bakerman2; 02-20-2020 at 11:07 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Method `Range of object_'Worksheet' failed - Trying to write variable to cell
    By stevemcd1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-17-2013, 04:52 PM
  2. Get multiple user selected cell range addresses and write to dictionary
    By aznenginerd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-28-2012, 05:49 PM
  3. Write variable to cell in excel
    By iisjman07 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-27-2010, 07:22 PM
  4. Trying to 'write' a formula in a cell with a variable reference
    By stumpy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-12-2007, 09:33 PM
  5. [SOLVED] using code variable to write cell formula
    By rpw in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-05-2006, 02:00 PM

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