+ Reply to Thread
Results 1 to 5 of 5

Assign string as a range in VBA

  1. #1
    Registered User
    Join Date
    09-28-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    14

    Assign string as a range in VBA

    Hi:

    I have a table as follows on Sheet 1:

    Item No File Name Full Path Range Name Copy To Sheet CopyToLocation(Start Cell Only)
    1 Alpa.xlsx C:\users\Pravin\Documents\ "Name" MasterData $a$2
    2 Pravin.xlsx C:\users\Pravin\Documents\ "Name" MasterData $j$2
    3 Rohin.xlsx C:\users\Pravin\Documents\ "Name" MasterData $s$2
    4 Pravin.xlsx C:\users\Pravin\Documents\ "Name" Sheet1 $a$2

    which lists four files all with a range called 'Name' and written the following code to pull off info from here and copy the range to the the cells specified. However having problems getting it to recognise the range name input as string as a range - shown in red.

    Sub GetData()
    Dim strWhereToCopy As String, strStartCellColName As String
    Dim strListSheet As String
    Dim strCopyRange As String
    Dim CopyRange As Range
    Dim strCellToCopyStart As String
    strListSheet = "List"

    On Error GoTo ErrH
    Sheets(strListSheet).Select
    Range("B2").Select

    'this is the main loop, we will open the files one by one and copy their data into the masterdata sheet
    Set currentWB = ActiveWorkbook
    Do While ActiveCell.Value <> ""

    strFileName = ActiveCell.Offset(0, 1) & ActiveCell.Value
    strCopyRange = ActiveCell.Offset(0, 2).Value
    strWhereToCopy = ActiveCell.Offset(0, 3).Value
    strStartCellColName = Mid(ActiveCell.Offset(0, 4), 2, 1)
    strCellToCopyStart = ActiveCell.Offset(0, 4).Value

    Application.Workbooks.Open strFileName, UpdateLinks:=False, ReadOnly:=True
    Set dataWB = ActiveWorkbook
    Set CopyRange = Range(strCopyRange)

    Range(CopyRange).Select

    Selection.Copy

    currentWB.Activate
    Sheets(strWhereToCopy).Select
    Range(strCellToCopyStart).Select


    Selection.PasteSpecial xlPasteValues, xlPasteSpecialOperationNone
    Application.CutCopyMode = False
    dataWB.Close False
    Sheets(strListSheet).Select
    ActiveCell.Offset(1, 0).Select
    Loop
    Exit Sub

    ErrH:
    MsgBox "It seems some file was missing. The data copy operation is not complete."
    Exit Sub
    End Sub

  2. #2
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Assign string as a range in VBA

    Hi,

    CopyRange is a Range object and therefore does not require an additional Range() call. You only need:
    Please Login or Register  to view this content.
    rather than
    Please Login or Register  to view this content.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  3. #3
    Registered User
    Join Date
    09-28-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Assign string as a range in VBA

    Hi xlnitwit:

    Thanks for your help. I changed to your suggestion and still fails at that point. When I use Range("Name").select the code work perfectly but would like to assign a name on the table which a user can change and the vba to pick up the named range. I know I am nearly there so your help is really appreciated!

  4. #4
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Assign string as a range in VBA

    Which line produces the error and what is the error message? Does the cell contain simple Name or are the quotation marks actually present? If it contains "Name", you must remove the quotation marks.

  5. #5
    Registered User
    Join Date
    09-28-2012
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    14

    Re: Assign string as a range in VBA

    Thanks xlnitwit - your suggestion did work. Had to remove the "" from the range name in the table!

+ 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. [SOLVED] How can I assign the last 4 characters of a string to a string variable?
    By dschmitt in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-29-2015, 01:41 AM
  2. [SOLVED] Assign String
    By oglcngcmn in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-03-2015, 08:26 AM
  3. Assign a range through a string cell value
    By Aristizabal95 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2013, 11:33 PM
  4. How to Assign Value to a String & Replace String With Assigned Value?
    By xiaokang1982 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-23-2012, 06:01 PM
  5. Assign String to Number value
    By NTB in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-01-2009, 01:04 PM
  6. Assign value to string?
    By Macdave_19 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-27-2008, 07:51 AM
  7. [SOLVED] How can I assign a number to a string?
    By Jog Dial in forum Excel General
    Replies: 3
    Last Post: 01-14-2005, 12:06 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