Relative Column Absolute Row Code

SewStage

Board Regular
Joined
Mar 16, 2021
Messages
70
Office Version
  1. 365
Platform
  1. Windows
Hopefully a quick and easy solution: I have the following code that automatically creates defined names within a certain tab. It works great, but Name Manager continues to show it as $D$56 for instance and I need it to be D$56. I've searched Mr Excel and and the internet but am not a VBA expert at all, so I'm not sure of the correct syntax or where to put it within this code...?

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("PCBMO", " ", "", 1)
        
    arrCodes = Split(strCodes, ",")
    
    For i = LBound(arrColumns) To UBound(arrColumns)
                    
        For intRow = 1 To 25
        
            strName = arrCodes(i) & intRow
                    
            Set rngAddress = Ws.Cells(55 + 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
            
            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
 
? - The red line is the one I gave yo
Would this help?

2023 Inventory Management.xlsm
ABCDEFGHIJKLMNO
1Product IDItem DescriptionBeginning QtyJanFebMarAprMayJunJulAugSepOctNovDec
561OBW quilt1
572Halloween gnome quilt1
583Boxy Tote02
594Flip Clutch Wallet - all cotton03
605Flip Clutch Wallet - all cork031
616Flip Clutch Wallet - all vinyl013
627NCW - cork02
638NCW - vinyl02
649Miranda Bag01
6510Vinyl Stadium Bag077
6611Itty Bitty Box Bag0867
6712Vinyl Drawstring Bag0117
6813H2O2GO Bag013211
6914Boxy Bag01910
7015Purse Pal03
7116Hinterland 18W WPC with cotton02
7217Notions pouch - clear010
7318Notions pouch - TPU09
7419Retro sling WPC exterior011
7520 0
7621 0
7722 0
7823 0
7924 0
8025 0
Product Completion by Mo.
Cell Formulas
RangeFormula
B56:B61,A63:B80,A56:A62B56=IF(ISBLANK('Material Usage'!B49),"",'Material Usage'!B49)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B55:B80,B83:B98,B100:B254Expression=MATCH(B55,KITNAME,0)textNO
D1:O1Expression=MONTH(D1)=MONTH(TODAY())textNO

? - The red line is the one I gave you
 

Attachments

  • 1696976851685.png
    1696976851685.png
    25.9 KB · Views: 8
Upvote 0

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
When I paste, it tries to open a file I don't have
 
Upvote 0
I was able to define a range with a column relative, row absolute address (i.e. D$56), but Excel seems to apply that relative value to jump the column value to something else, depending on what the activecell is, though I don't understand the relationship. It behaves the same way if you edit the name manually. Compare columns B to D on the NamedRangeList1234019 sheet to see what I mean.

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 S As String, SA
    Dim strName As String
    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
    Dim TestN As Name
  
    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("PCBMO", " ", "", 1)

    arrCodes = Split(strCodes, ",")

    Dim Refstr As String
    For I = LBound(arrColumns) To UBound(arrColumns)

        For intRow = 1 To 25

            strName = arrCodes(I) & intRow

            Set rngAddress = Ws.Cells(55 + intRow, Range(Trim(arrColumns(I)) & "1").Column)

            S = Replace(rngAddress.Address(1, 0, , 1), "[", "]")
            SA = Split(S, "]")
            Refstr = "=" & SA(0) & SA(UBound(SA))

            Set TestN = ThisWorkbook.Names.Add(Name:=strName, RefersTo:=Refstr)
          
            With WsList
                .Range("A" & Rows.Count).End(xlUp)(2) = strName
                .Range("B" & Rows.Count).End(xlUp)(2) = "''" & Ws.Name & "'!" & rngAddress.Address
                .Range("C" & Rows.Count).End(xlUp)(2) = "'" & TestN.RefersTo
                .Range("D" & Rows.Count).End(xlUp)(2) = "'" & TestN.RefersToRange.Address(, , , 1)
            End With

            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", "RefersTo", "RefersToRange")
            .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
    ActiveSheet.UsedRange.Columns.AutoFit
    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
 
Upvote 0
I was able to define a range with a column relative, row absolute address (i.e. D$56), but Excel seems to apply that relative value to jump the column value to something else, depending on what the activecell is, though I don't understand the relationship. It behaves the same way if you edit the name manually. Compare columns B to D on the NamedRangeList1234019 sheet to see what I mean.

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 S As String, SA
    Dim strName As String
    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
    Dim TestN As Name
 
    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("PCBMO", " ", "", 1)

    arrCodes = Split(strCodes, ",")

    Dim Refstr As String
    For I = LBound(arrColumns) To UBound(arrColumns)

        For intRow = 1 To 25

            strName = arrCodes(I) & intRow

            Set rngAddress = Ws.Cells(55 + intRow, Range(Trim(arrColumns(I)) & "1").Column)

            S = Replace(rngAddress.Address(1, 0, , 1), "[", "]")
            SA = Split(S, "]")
            Refstr = "=" & SA(0) & SA(UBound(SA))

            Set TestN = ThisWorkbook.Names.Add(Name:=strName, RefersTo:=Refstr)
         
            With WsList
                .Range("A" & Rows.Count).End(xlUp)(2) = strName
                .Range("B" & Rows.Count).End(xlUp)(2) = "''" & Ws.Name & "'!" & rngAddress.Address
                .Range("C" & Rows.Count).End(xlUp)(2) = "'" & TestN.RefersTo
                .Range("D" & Rows.Count).End(xlUp)(2) = "'" & TestN.RefersToRange.Address(, , , 1)
            End With

            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", "RefersTo", "RefersToRange")
            .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
    ActiveSheet.UsedRange.Columns.AutoFit
    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
Strange - it's actually applying the defined names to the cells on the last tab of the workbook! The tab I need them on is the 3rd (4th if you count one hidden tab).
 
Upvote 0
Strange - it's actually applying the defined names to the cells on the last tab of the workbook! The tab I need them on is the 3rd (4th if you count one hidden tab).

It did not do that for me. It created the names in the specified tab (worksheet) as per the code.
 
Upvote 0
It did not do that for me. It created the names in the specified tab (worksheet) as per the code.
Could that be because you only had one tab to work with (if you're referring to the sample I sent you)?
 
Upvote 0
No. My test workbook has multiple tabs.
I don't know then. I was hoping it would be a straightforward solution but I guess not. Going back to the original code, I saw something online that had "...address.address(true, false) - and that actually worked on the new sheet the macro creates, but not in the Name Manager.
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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