1

I'm quite a newbie to VBA macros. I created a Userform to save some time with copy and pasting Excel content.

Now I have the following issue: I get a string output from a Userform - Textbox (txtHRS_ANW). I created exactly the same names as named ranges. Now I want to use the string output as a reference for the named range. I am not able to add the double quotes in the brackets - I tried using """" and chr(34).

My code is as follows:

Private Sub cmdHRSLoading_Click()
Dim NameRange As String
If chkANW = True Then
    NameRange = Me.txtHRS_ANW.Value 'gives me following string HRS_ANW_CORP01 (until CORP10 depending on user entry)
    Sheet5.Range(NameRange).Select 'here I would like to use the string as a reference for the range
    Selection.Copy
    Sheet9.Range("A" & Rows.count).End(xlUp).Offset(1).Select
    Selection.Paste
End If

End Sub
1
  • do you get any error? what is its number? try to activate your sheet first with Sheet5.activate and next call .select line. Please remember that you don't need to select to trigger .copy >> .paste methods Commented Dec 8, 2016 at 8:09

2 Answers 2

1

Use the following procedure:

Private Sub cmdHRSLoading_Click()
Dim MyNameRange As String
        If chkANW = True Then
            MyNameRange = Me.txtHRS_ANW 'gives me following string HRS_ANW_CORP01 (until CORP10 depending on user entry)
            Application.Goto (MyNameRange) 'here I would like to use the string as a reference for the range
            Selection.Copy
            Sheet9.Activate
            Range("A" & Rows.count).End(xlUp).Offset(1).Select
            ActiveSheet.Paste
        End If
End Sub

Use Application.GoTo method. Also you do not need to use .value after Me.txtHRS_ANW

--- Use ActiveSheet.Paste not Selection.Paste

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Harun - Thanks for the fast and accurate answer! You saved my day - Spot on!
It might be worth explaining why you don't need to use .Value for other people that come to view this post in the future, and possibly the OP.
1

Can also be written as:

Private Sub cmdHRSLoading_Click()

    If chkANW Then
        With Sheet9
            With .Cells(.Rows.Count, 1).End(xlUp).Offset(0, 1)
                .Resize(Range(Me.txtHRS_ANW.Text).Rows.Count, Range(Me.txtHRS_ANW.Text).Columns.Count).Value = Range(Me.txtHRS_ANW.Text).Value
            End With
        End With
    End If

End Sub

Negating the need to use the clipboard.

2 Comments

Thank you! Just one more question - I wrote the macro in Excel 2016 - How can I make it work in Excel 2010?
There should be no difference in the code really - I'd suggest posting a new question of something isn't working on a specific version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.