Results 1 to 3 of 3

Thread: VBA Pasting values to different cells in the same named range

  1. #1
    VBAX Newbie
    Feb 2020

    VBA Pasting values to different cells in the same named range

    Hi all,

    I don't have a ton of VBA experience, so I was hoping I could get some ideas from others. I have a text document resulting from print statements in the following format:

    Set CurrentRng = Range("NamedRange")
    Print #1, .Name & Chr(9) & "NamedRange" & Chr(9) & CurrentRng

    So the data might look like this in the text file:

    Sheet Name Named Rng Value
    Sheet 1 Name 1 2
    Sheet 1 Name 2 3
    Sheet 1 Name 2 4

    Notice that there are 2 values for the the cell range "Name 2" (Named range encompasses two cells).

    I am trying to take the values from the text document and paste them into the correct cells (i.e. if values "3" and "4" were in cells A4 and A5, respectively, in the original document, I want to be able to return them to those positions but using the named range so the results are unaffected by any edits to the excel document)

    Here's my code:

    Sub Test_Input()
    Dim InData As String
    Dim SheetName As String
    Dim NamedRng As String
    Dim NewVal As String
    Dim WS As Worksheet
    Dim Target As String
    Open "TESTFILE" For Input As #1
    Do Until EOF(1)
        Line Input #1, InData
        SheetName = GetSubString(InData, Chr(9), 1)
        NamedRng = GetSubString(InData, Chr(9), 2)
        NewVal = GetSubString(InData, Chr(9), 3)
        Set WS = ThisWorkbook.Sheets(SheetName)
        Target = NamedRng
        WS.Range(Target) = NewVal
    Close #1
    End Sub
    Where the GetSubString function is just pulling the 3 fields in the text document.

    I'm running into a problem when two entries have the same named range, as in the example above. So all the cells in the range "Name 2" will populate as 4, instead of the first populating as 3 and the second populating as 4. I know my code currently does nothing to separate these out, I'm having difficulty thinking of a way to get my desired result. Any ideas?

    Thanks in advance!

  2. #2
    VBAX Wizard
    Apr 2007
    Code fragment to put data into named range's cells

    Option Explicit
    Sub demo()
        ActiveSheet.Range("A1:B1").Name = "MyName"
        Range("MyName").Cells(1, 1).Value = 123
        Range("MyName").Cells(1, 2).Value = 456
        [MyName].Cells(1, 1).Value = 321
        [MyName].Cells(1, 2).Value = 654
    End Sub

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s)
    (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums

  3. #3
    Knowledge Base Approver VBAX Guru
    Oct 2005
    Surrey UK
    I think you might be able to set up a dictionary object to hold the last used cell for every SheetName/NamedRange combination.
    If you set up a workbook with named ranges etc. in and attach it here along with a text file, I'll give it a go.
    p45cal - - - - -This is my signature, it appears after all my posts. Below is not directed at anyone in particular - so don't take offence! - (You might guess why it's there though)
    If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

Tags for this Thread

Posting Permissions

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