Hello All,
I am still pretty fresh when it comes to VBA so the answer to this may be simpler than I have made it so far;
Sheet3 contains all the ranges that I want to add as Named Ranges. Row 1 contains the range name and everything below is the range i.e. A2:A2000 (All to be the same size range!). There are many columns in Sheet3, all requiring named ranges. The range names may already exist in the workbook but they are to be superseded with these new ranges in Sheet3.
So far, I have written the below code but it doesn't work. Actually, it worked the first time I ran it but, now it stops at the RefersTo range. I can't work out what is wrong with the range, if that is actually the problem...
Any help will be much appreciated.
Sub create_named_ranges()
Dim ws3 As Worksheet
Dim Cols As Integer
Dim Col As Integer
Dim Row As Integer
Dim n As String
'Stop the stuff
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
'Give values
Set ws3 = ThisWorkbook.Worksheets(3)
Cols = ws3.UsedRange.Columns.Count
Row = ws3.UsedRange.Rows.Count
'Loop for each column
For Col = 1 To Cols
'Loop to delete existing Name first
For Each Name In Names
If Name = ws3.Cells(1, Col) Then
Name.Delete
End If
Next Name
'Add new Name and set range
wb.Names.Add Name:=ws3.Cells(1, Col), RefersTo:=ws3.Range(Cells(2, Col), Cells(Row, Col))
Next Col
'Start stuff back up
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
I am still pretty fresh when it comes to VBA so the answer to this may be simpler than I have made it so far;
Sheet3 contains all the ranges that I want to add as Named Ranges. Row 1 contains the range name and everything below is the range i.e. A2:A2000 (All to be the same size range!). There are many columns in Sheet3, all requiring named ranges. The range names may already exist in the workbook but they are to be superseded with these new ranges in Sheet3.
So far, I have written the below code but it doesn't work. Actually, it worked the first time I ran it but, now it stops at the RefersTo range. I can't work out what is wrong with the range, if that is actually the problem...
Any help will be much appreciated.
Sub create_named_ranges()
Dim ws3 As Worksheet
Dim Cols As Integer
Dim Col As Integer
Dim Row As Integer
Dim n As String
'Stop the stuff
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
'Give values
Set ws3 = ThisWorkbook.Worksheets(3)
Cols = ws3.UsedRange.Columns.Count
Row = ws3.UsedRange.Rows.Count
'Loop for each column
For Col = 1 To Cols
'Loop to delete existing Name first
For Each Name In Names
If Name = ws3.Cells(1, Col) Then
Name.Delete
End If
Next Name
'Add new Name and set range
wb.Names.Add Name:=ws3.Cells(1, Col), RefersTo:=ws3.Range(Cells(2, Col), Cells(Row, Col))
Next Col
'Start stuff back up
Application.CutCopyMode = False
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub