I am trying to copy a number of columns of data from one worksheet to another. I turned the data from row 2 down in each column into a named range using the following code:
Sub CreateRangeNamesPosExport()
Dim C As Long
Dim Header As String
Dim LastCol As Long
Dim RefStr As String
Dim Rng As Range
Dim RngEnd As Range
Dim Wks As Worksheet
Set Wks = ActiveSheet
LastCol = Wks.Cells(1, Columns.Count).End(xlToLeft).Column
For C = 1 To LastCol
Set Rng = Cells(2, C)
Set RngEnd = Wks.Cells(Rows.Count, C).End(xlUp)
Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
Header = CStr(Wks.Cells(1, C))
Rng.Name = Header
On Error Resume Next
Wks.Parent.Names(Header).Delete
Err.Clear
On Error GoTo 0
Wks.Parent.Names.Add Header, RefStr
Next C
End Sub
This seems to have done the job; i.e. I can "goto" the named ranges using the dropdown menu manually. However, when I try to use vba to copy and paste the data in these ranges to another sheet, I don't get any error message, but nothing happens at all. I also tried to just create a "goto" macro to see if I could get excel to select the range based on my command, but I can't get that to work either. What am I doing wrong? Thanks much in advance for your help!!!
Sub CopyNamedRange()
With Sheets("Position Export")
.Range("PortfolioAccountNumber").Copy Destination:=Sheets("Rebalance").Range("A2")
End With
End Sub
Bookmarks