I want to copy several named ranges. The ranges have a set naming convention with a short string, then number (eg, Body1, Body2, etc).
My workaround loops through each sheet name, and copies range B2:C50 instead of each named range, which is what I`d prefer.
Code:
Dim Bnum As Integer 'Bnum is Loop Counter
Dim Nbodies As Integer 'Nbodies is how many loops should go
Dim BodyNum As String
'BodyNum is the sheet name where the named range is (and also the named range I`d like to copy). The named ranges are workbook wide in this case
Nbodies = Range("N.Bodies") 'Called from elsewhere in the workbook
Bnum = 1 'Initial counter set
BodyNum = "Body" & Bnum 'Name of worksheet+named range which I want to copy
Do Until Bnum = Nbodies + 1
Workbooks(Wkbk).Activate
Worksheets(BodyNum).Range("B2:C50").Copy
'Copies range manually definied in vba B2:C50 in this case. Want to copy named range with the same name as Bodynum, varying the digit for each loop
Workbooks(Inputbk).Activate 'The paste portion works fine
Selection.PasteSpecial Paste:=xlPasteValues
Call Cursor.Cursor
Workbooks(Wkbk).Activate
Bnum = Bnum + 1 'Iterates loop counter
BodyNum = "Body" & Bnum
Loop
My understanding is that you have a set of worksheets named "Body1".."BodyN", each having a named range bearing the name of the worksheet itself.
You mention that you Want to copy named range with the same name as Bodynum
.
Then all you should have to do is turn this line:
Worksheets(BodyNum).Range("B2:C50").Copy
into this:
Worksheets(BodyNum).Range(BodyNum).Copy
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.