Jan 28 2021 09:51 PM
After a recent windows update, a previous Excel vba script that was working, no longer functions correctly.
The macro operation, when working, opens a csv file that is defined in the active workbook as String aString. The csv file contains a list of variables and corresponding values for those variables. The macro returns the original active workbook and reads the defined named cells in the active workbook and updates those named cells with the values defined in the csv file.
The issue appears to be that despite returning to the original active workbook the command to generate the For loop to cycle through the named cells no longer returns a value for the variable name or which worksheet the variable lives in.
The macro works perfectly in Excel build Version 1902 (Build 11328.20318) but does not work in Version 2002 (Build 12527.21416).
The original code was originally written in Excel 2016 and has been functioning perfectly up until the latest company approved version of window 10 was updated.
The code is:
-----------------------------
Public Sub findNamedRange()
strWorkBook = ActiveWorkbook.Name
Workbooks(strWorkBook).Activate
Dim Nm As Name
Dim varName As String
Dim varSheet As String
Dim varCell As String
For Each Nm In Workbooks(strWorkBook).Names
With Nm
varName = Nm.Name
MsgBox "Named range """ & varName
varSheet = .RefersToRange.Parent.Name
varCell = Nm.RefersToRange.Address(0, 0)
End With
MsgBox "Named range """ & varName & """ refers to range" & vbCr & _
varCell & " on sheet """ & varSheet & """."
Next Nm
End Sub
---------------------------------
If I create a new workbook in Excel Version 2002 (Build... and copy this code into a new module it works perfectly, in terms of finding the named cells, their address and the sheet they are located on. However, If I try to run this module in the Excel workbook created pre- Build Version 2002, it fails with a warning message "Named range [ _xlfn.ANCHORARRAY ], which I presume means it cannot find any named ranges in the workbook.
Jan 31 2021 06:45 PM
SolutionIssue resolved after lots of searching on stackoverflow.
It seems that between Excel V1902 and V2002 a hidden variable _xlfn.SINGLE exists in the workbook. When the macro loops through, it sees the named range, cannot resolve its address or sheet location and stops. The hidden variable was only possible to see after running an additional macro to display hidden named ranges.
The fix was to include a loop to skip the named variable _xlfn* if found. Full detail and solution available on Stackoverflow
Jun 21 2022 03:30 PM
Jul 06 2022 08:00 AM