Named Ranges Just Stopped Working Correctly

SewStage

Board Regular
Joined
Mar 16, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Unfortunately, I'm unable to download/use XL2BB anymore so only have text and screen shots.

The following macro all of a sudden started returning erroneous results without any changes to it, so I'm hoping someone can spot in the code where this might be occurring. This macro assigns a named range starting in Column D of Rows 5-64 within the Product Completion By Month tab. As you can see in the first screenshot, PCBM1 should refer to D5, but in reality when I select the D5 cell and open the Name Manager it has assigned G5 as the defined name (and E5 = H5, F5 = I5 etc. across the columns) per the second screenshot. It doesn't matter what cell my cursor is in when I run the macro it returns the same erroneous results. These errors ultimately impact (pass false data to) another tab in my workbook that's critical for my tax reporting. Any help is greatly appreciated.

VBA Code:
Public Sub subCreateNamedRanges()
' https://www.mrexcel.com/board/threads/autofill-naming-defined-names.1234019/
Dim Ws As Worksheet
Dim strMsg As String
Dim rngRangeList As Range
Dim Rng As Range
Dim s As String
Dim NamedRange As Name
Dim strName As String
Dim blnSheet As Boolean
Dim rngAddress  As Range
Dim intRow As Integer
Dim strColumns As String
Dim strCodes As String
Dim i As Integer
Dim arrColumns() As String
Dim arrCodes() As String
Dim WsList As Worksheet
Dim intCount As Integer

    ActiveWorkbook.Save
    
    strMsg = "Do you want to set the named ranges for the '" & ActiveSheet.Name & "' worksheet?"
    
    If MsgBox(strMsg, vbYesNo, "Security Question") = vbNo Then
        MsgBox "Activate the correct sheet before you run this code.", vbOKOnly, "Information"
        Exit Sub
    End If
    
    Set Ws = ActiveSheet
    
    On Error Resume Next
    Application.DisplayAlerts = False
    Worksheets("NamedRangeList1234019").Delete
    Application.DisplayAlerts = True
    On Error GoTo 0
    Worksheets.Add after:=Sheets(Sheets.Count)
    ActiveSheet.Name = "NamedRangeList1234019"
    Set WsList = ActiveSheet
        
    WsList.Range("A2:F20000").Cells.ClearContents
    
    Ws.Activate
        
    strColumns = "D"
    arrColumns = Split(strColumns, ",")
    
    strCodes = Replace("PCBM", " ", "", 1)
        
    arrCodes = Split(strCodes, ",")
    
    For i = LBound(arrColumns) To UBound(arrColumns)
                    
        For intRow = 1 To 60
        
            strName = arrCodes(i) & intRow
                    
            Set rngAddress = Ws.Cells(4 + intRow, Range(Trim(arrColumns(i)) & "1").Column)
                        
            With WsList
                .Range("A" & Rows.Count).End(xlUp)(2) = strName
                .Range("B" & Rows.Count).End(xlUp)(2) = "'" & Ws.Name & "!" & rngAddress.Address
            End With
            
            ThisWorkbook.Names.Add Name:=strName, RefersTo:="='" & rngAddress.Worksheet.Name & "'!" & rngAddress.Address(True, False)
            intCount = intCount + 1
            
        Next intRow
        
    Next i
    
    ActiveWorkbook.Save
    
    WsList.Activate
    
    With WsList.Range("A1").CurrentRegion
    
        .Font.Size = 16
        .Font.Name = "Arial"
        .EntireColumn.AutoFit
        .VerticalAlignment = xlCenter
        With .Rows(1)
            .Value = Array("Name", "Address")
            .Font.Bold = True
            .Interior.Color = RGB(219, 219, 219)
        End With
        .RowHeight = 28
        With .Borders
            .LineStyle = xlContinuous
            .Weight = xlThin
            .ColorIndex = vbBlack
        End With
        .IndentLevel = 1
        
    End With
    
    WsList.Range("A2").Select
    ActiveWindow.FreezePanes = True
    
    strMsg = intCount & " named ranges have been created."
    strMsg = strMsg & vbCrLf & "These have been listed in the " & WsList.Name & " worksheet."
    
    MsgBox strMsg, vbInformation, "Confirmation"
            
End Sub
 

Attachments

  • PCBM2.PNG
    PCBM2.PNG
    21.3 KB · Views: 4
  • PCBM1.PNG
    PCBM1.PNG
    66 KB · Views: 5

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Based on a quick look (I haven't tried to run your code) ....

Your reference is meant to be $D$5, but is actually G$5 - you've lost the absolute column reference $D.

In the code line below, if rngAddress is D5, then rngAddress.Address(True,False) will be D$5 so the code will create a name pointing to D$5.
VBA Code:
ThisWorkbook.Names.Add Name:=strName, RefersTo:="='" & rngAddress.Worksheet.Name & "'!" & rngAddress.Address(True, False)

When this range is created, the ActiveCell will be column A (in the new worksheet NamedRangeList1234019). So if you move the ActiveCell to column D (your second picture) the reference will move relatively to G$5.

Therefore I suggest you try:
Rich (BB code):
ThisWorkbook.Names.Add Name:=strName, RefersTo:="='" & rngAddress.Worksheet.Name & "'!" & rngAddress.Address(True, True)
 
Upvote 0
Based on a quick look (I haven't tried to run your code) ....

Your reference is meant to be $D$5, but is actually G$5 - you've lost the absolute column reference $D.

In the code line below, if rngAddress is D5, then rngAddress.Address(True,False) will be D$5 so the code will create a name pointing to D$5.
VBA Code:
ThisWorkbook.Names.Add Name:=strName, RefersTo:="='" & rngAddress.Worksheet.Name & "'!" & rngAddress.Address(True, False)

When this range is created, the ActiveCell will be column A (in the new worksheet NamedRangeList1234019). So if you move the ActiveCell to column D (your second picture) the reference will move relatively to G$5.

Therefore I suggest you try:
Rich (BB code):
ThisWorkbook.Names.Add Name:=strName, RefersTo:="='" & rngAddress.Worksheet.Name & "'!" & rngAddress.Address(True, True)
Thank you, Stephen. I did make the change you suggested and the defined name for D5 is now D5, but I don't want the absolute column reference $D; this needs to go across all columns within that 60-row range. Thoughts to make that happen then without the absolute column reference?
 
Upvote 0
Based on a quick look (I haven't tried to run your code) ....

Your reference is meant to be $D$5, but is actually G$5 - you've lost the absolute column reference $D.

In the code line below, if rngAddress is D5, then rngAddress.Address(True,False) will be D$5 so the code will create a name pointing to D$5.
VBA Code:
ThisWorkbook.Names.Add Name:=strName, RefersTo:="='" & rngAddress.Worksheet.Name & "'!" & rngAddress.Address(True, False)

When this range is created, the ActiveCell will be column A (in the new worksheet NamedRangeList1234019). So if you move the ActiveCell to column D (your second picture) the reference will move relatively to G$5.

Therefore I suggest you try:
Rich (BB code):
ThisWorkbook.Names.Add Name:=strName, RefersTo:="='" & rngAddress.Worksheet.Name & "'!" & rngAddress.Address(True, True)
Never mind my last reply - I found some previous code to remove the absolute reference. Again, thanks so much for your help!
 
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,736
Members
453,369
Latest member
juliewar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top