+ Reply to Thread
Results 1 to 7 of 7

Get cell value and use as vba variable

Hybrid View

  1. #1
    Registered User
    Join Date
    08-02-2010
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2013
    Posts
    26

    Get cell value and use as vba variable

    Hi,

    I am trying to find a way where I can use a worksheet cell value in vba code. ie.. I have vba code
    Sheets("School Term").Range(B4:B201, B436:B572").Copy
    Sometimes these values will change and I would like to just change them in a cell in a worksheet and have the code updated from the cell values. is this possible please.

    Regards
    David

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Get cell value and use as vba variable

    Hi David,

    Try this where the range is in cell A2 (change to suit):

    Option Explicit
    Sub Macro1()
        
        Dim strMyRange As String
        
        strMyRange = Sheets("School Term").Range("A2").Value
    
        Sheets("School Term").Range(CStr(strMyRange)).Copy
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Registered User
    Join Date
    08-02-2010
    Location
    Canberra, Australia
    MS-Off Ver
    Excel 2013
    Posts
    26

    Re: Get cell value and use as vba variable

    Hi Robert,

    Thanks for your reply,

    I seem to have the values I want now in the variables but copying those values in the worksheet don't seem to work well. ie.. if I use
    Sheets("School Term").Range("B4:B201, B436:B572").Copy
    this will copy 2 different sections ie.. copies from B4 to B572 without the middle rows B202 to B435. if is use the strMyRange it copies all from B4 to B572?

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,643

    Re: Get cell value and use as vba variable

    deleted....
    Ben Van Johnson

  5. #5
    Forum Contributor banaanas's Avatar
    Join Date
    08-26-2014
    Location
    Finland
    MS-Off Ver
    2016
    Posts
    199

    Re: Get cell value and use as vba variable

    What excactly are you trying to achieve? Why do you need the values in a variable?

    You can also store the range itself in a range variable.
    
    Option Explicit
    Sub Macro1()
        
        Dim MyRange As Range
    
        MyRange = Sheets("School Term").Range("B4:B201", "B436:B572")
        MyRange .Copy
    
    End Sub
    Tuomas "Banaanas" Savonius
    Trying to give back now when I actually can do some VBA

  6. #6
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,643

    Re: Get cell value and use as vba variable

    Cell Z1 --> B4:B201, B436:B572

    Range(range("Z1").value).Copy Dest

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Get cell value and use as vba variable

    worksheet don't seem to work well
    That's odd as it worked for me

    If the non adjacent range is entered correctly (as protonLeah has done) into the cell your code references it should work.

+ 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. Replies: 5
    Last Post: 06-18-2015, 09:10 AM
  2. Replies: 3
    Last Post: 02-17-2015, 11:53 PM
  3. Replies: 4
    Last Post: 05-07-2014, 03:08 AM
  4. [SOLVED] Assigning A Cell Address to A Variable, Using A Variable - Error '1003'
    By ch_abs in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-28-2014, 01:36 PM
  5. Replies: 4
    Last Post: 07-12-2013, 12:14 PM
  6. Replies: 3
    Last Post: 11-21-2012, 03:28 PM
  7. Replies: 2
    Last Post: 07-24-2012, 11:47 AM

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