Hi All!
I'm writing a sub to restore the named ranges in a workbook back to what I originally intended them to be (to compensate for users deleting data rows, etc). My basic approach is just to use the Worksheet.Names.Add method. If the name already exists (it should), Excel just seems to overwrite the current reference with the correct reference. This is good.
The problem is that when I run it, strange things happen. For a while it was giving me weird errors, but I added a few lines at the beginning to select cell "A1" of each sheet (to avoid relative reference issues), and that seemed to fix that. Now it creates the Named ranges, but they arent visible to the formulas on my worksheet, or to the named range drop-down box in the menu bar. They ARE visible and correctly set if I go to Insert>Name>Define. What am I doing wrong? Here's the code:
Thanks for your help!
VBAjedi![[swords] [swords] [swords]](/data/assets/smilies/swords.gif)
I'm writing a sub to restore the named ranges in a workbook back to what I originally intended them to be (to compensate for users deleting data rows, etc). My basic approach is just to use the Worksheet.Names.Add method. If the name already exists (it should), Excel just seems to overwrite the current reference with the correct reference. This is good.
The problem is that when I run it, strange things happen. For a while it was giving me weird errors, but I added a few lines at the beginning to select cell "A1" of each sheet (to avoid relative reference issues), and that seemed to fix that. Now it creates the Named ranges, but they arent visible to the formulas on my worksheet, or to the named range drop-down box in the menu bar. They ARE visible and correctly set if I go to Insert>Name>Define. What am I doing wrong? Here's the code:
Code:
Sub RefreshNamedRanges()
' Call this sub anytime you want to refresh the ranges
Dim TargetWorkbook As Workbook, mSheet As Worksheet, mName As Name
Dim x
Set TargetWorkbook = ActiveWorkbook
For Each mSheet In TargetWorkbook.Worksheets
x = mSheet.Name
mSheet.Activate
Range("A1").Select ' avoid relative range issues
Next mSheet
AssignSheets ' creates Sheet variables named shSS, shDa, and ShLB that refer to the three sheets with named ranges
TargetWorkbook.Names.Add Name:="DAData", RefersTo:="=" & ShDa.Name & "!$A$1:$U$50000"
TargetWorkbook.Names.Add Name:="LBCanLatestBal", RefersTo:="=" & ShLB.Name & "!$F$7"
TargetWorkbook.Names.Add Name:="LBUSLatestBal", RefersTo:="=" & ShLB.Name & "!$A$7"
TargetWorkbook.Names.Add Name:="SSAgingBuckets", RefersTo:="=" & ShSS.Name & "!$L$13:$L$50000"
TargetWorkbook.Names.Add Name:="SSAgingDate", RefersTo:="=" & ShSS.Name & "!$K$1"
TargetWorkbook.Names.Add Name:="SSAllData", RefersTo:="=" & ShSS.Name & "!$A$13:$AN$50000"
TargetWorkbook.Names.Add Name:="SSCurDocAmts", RefersTo:="=" & ShSS.Name & "!$N$13:$N$50000"
TargetWorkbook.Names.Add Name:="SSDataAndHeaders", RefersTo:="=" & ShSS.Name & "!$A$12:$AN$50000"
TargetWorkbook.Names.Add Name:="SSDataDate", RefersTo:="=" & ShSS.Name & "!$I$1"
TargetWorkbook.Names.Add Name:="SSDocDates", RefersTo:="=" & ShSS.Name & "!$K$13:$K$50000"
TargetWorkbook.Names.Add Name:="SSDocNums", RefersTo:="=" & ShSS.Name & "!$I$13:$I$50000"
TargetWorkbook.Names.Add Name:="SSLastWkData", RefersTo:="=" & ShSS.Name & "!$R$13:$W$50000"
TargetWorkbook.Names.Add Name:="SSOpenCRAmts", RefersTo:="=" & ShSS.Name & "!$O$13:$O$50000"
TargetWorkbook.Names.Add Name:="SSOrigDocAmts", RefersTo:="=" & ShSS.Name & "!$M$13:$M$50000"
TargetWorkbook.Names.Add Name:="SSThisWkData", RefersTo:="=" & ShSS.Name & "!$L$13:$Q$50000"
TargetWorkbook.Names.Add Name:="SSUpdatedAmts", RefersTo:="=" & ShSS.Name & "!$AD$13:$AD$50000"
TargetWorkbook.Names.Add Name:="SSUpdateDate", RefersTo:="=" & ShSS.Name & "!$AD$10"
TargetWorkbook.Names.Add Name:="SSWkBeforeData", RefersTo:="=" & ShSS.Name & "!$X$13:$AC$50000"
End Sub
Thanks for your help!
VBAjedi
![[swords] [swords] [swords]](/data/assets/smilies/swords.gif)