xluser2007
07-19-2008, 02:38 AM
Hi All,
We conduct valuations every December and June.
The Dec07 valuation had a bunch of spreadsheets that had named ranges ending in "_Dec07".
We are in the Jun08 valuation. I have w orkbook that contains a list of the se relevant workbooks. I wish to write a macro that will:
Open up the list of workbooks.
For each workbook opened, search through each named range ending in "_Dec07" and replace it with "_Jun08".
Make sure the new named range for Jun08 has been substitutred over its Dec07 counterpart i.e. if a range was "data_Dec07". Step 2 should create and equivalent "data_Jun08". We need to substitute all "data_Dec07" range references with "data_Jun08".
Once all substitutions with "_Jun08" named ranges have been made, we can delete all the named ranges ending in "_Dec07".
Move onto the next workbook. and repeat steps 1-4.Now I have started with this code.
And for Step 1, I have:
Option Explicit
'-------------------------------------
' DEFINE any Private/ MODULE CONTANTS
'-------------------------------------
' "mclng_startrow" the starting ROW reference for the workbooks that the
' macro loops through on each worksheet
Private Const mclng_startrow As Long = 5
' "mclng_wbksCOLUMN" is the starting COLUMN reference for the workbooks list that
' the macro loops through and OPENS
Private Const mclng_wbksCOLUMN As String = "C"
Sub OPEN_workbooks(UpdateLinksSheetReference As String)
'------------------------------------------------------------------------
' STEP 1:
' Dimension all objects as we are using 'Option Explicit'
Dim UpdateSht As Worksheet
Dim linkwkbk As Workbook
Dim row As Long
'------------------------------------------------------------------------
' STEP 2:
' Toggle OFF screenupdating and automatic calculation
' This is for efficiency
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
'------------------------------------------------------------------------
' STEP 3: ERROR Handling
Set UpdateSht = ThisWorkbook.Sheets(UpdateLinksSheetReference)
If UpdateSht.Range("D1") <> 0 Then
' Display error message to user, and ask them to re-check link workbook
' strings and then Exit Sub
Call _
MsgBox("One of your workbooks links is incorrect - please correct it and re run?", _
vbCritical Or vbSystemModal, "ERROR")
Exit Sub
Else
'------------------------------------------------------------------------
' STEP 4: Renaming the relevant links
' mclng_startrow defined as a private CONSTANT above
row = mclng_startrow
Do While (UpdateSht.Range(mclng_wbksCOLUMN & row).Value <> "") Or _
(UpdateSht.Range(mclng_wbksCOLUMN & row).Value <> vbNullString)
Set linkwkbk = Workbooks.Open(UpdateSht.Range(mclng_wbksCOLUMN & row).Value, UpdateLinks:=0)
' INSERT Steps 2-4
row = row + 1
Loop
'------------------------------------------------------------------------
' STEP 5:
'Clear memory of stored objects by setting them to Nothing
Set linkwkbk = Nothing
'------------------------------------------------------------------------
End If
End Sub
Could anyone help me with Steps 2-4 as above please?
We conduct valuations every December and June.
The Dec07 valuation had a bunch of spreadsheets that had named ranges ending in "_Dec07".
We are in the Jun08 valuation. I have w orkbook that contains a list of the se relevant workbooks. I wish to write a macro that will:
Open up the list of workbooks.
For each workbook opened, search through each named range ending in "_Dec07" and replace it with "_Jun08".
Make sure the new named range for Jun08 has been substitutred over its Dec07 counterpart i.e. if a range was "data_Dec07". Step 2 should create and equivalent "data_Jun08". We need to substitute all "data_Dec07" range references with "data_Jun08".
Once all substitutions with "_Jun08" named ranges have been made, we can delete all the named ranges ending in "_Dec07".
Move onto the next workbook. and repeat steps 1-4.Now I have started with this code.
And for Step 1, I have:
Option Explicit
'-------------------------------------
' DEFINE any Private/ MODULE CONTANTS
'-------------------------------------
' "mclng_startrow" the starting ROW reference for the workbooks that the
' macro loops through on each worksheet
Private Const mclng_startrow As Long = 5
' "mclng_wbksCOLUMN" is the starting COLUMN reference for the workbooks list that
' the macro loops through and OPENS
Private Const mclng_wbksCOLUMN As String = "C"
Sub OPEN_workbooks(UpdateLinksSheetReference As String)
'------------------------------------------------------------------------
' STEP 1:
' Dimension all objects as we are using 'Option Explicit'
Dim UpdateSht As Worksheet
Dim linkwkbk As Workbook
Dim row As Long
'------------------------------------------------------------------------
' STEP 2:
' Toggle OFF screenupdating and automatic calculation
' This is for efficiency
With Application
.ScreenUpdating = False
.Calculation = xlCalculationManual
End With
'------------------------------------------------------------------------
' STEP 3: ERROR Handling
Set UpdateSht = ThisWorkbook.Sheets(UpdateLinksSheetReference)
If UpdateSht.Range("D1") <> 0 Then
' Display error message to user, and ask them to re-check link workbook
' strings and then Exit Sub
Call _
MsgBox("One of your workbooks links is incorrect - please correct it and re run?", _
vbCritical Or vbSystemModal, "ERROR")
Exit Sub
Else
'------------------------------------------------------------------------
' STEP 4: Renaming the relevant links
' mclng_startrow defined as a private CONSTANT above
row = mclng_startrow
Do While (UpdateSht.Range(mclng_wbksCOLUMN & row).Value <> "") Or _
(UpdateSht.Range(mclng_wbksCOLUMN & row).Value <> vbNullString)
Set linkwkbk = Workbooks.Open(UpdateSht.Range(mclng_wbksCOLUMN & row).Value, UpdateLinks:=0)
' INSERT Steps 2-4
row = row + 1
Loop
'------------------------------------------------------------------------
' STEP 5:
'Clear memory of stored objects by setting them to Nothing
Set linkwkbk = Nothing
'------------------------------------------------------------------------
End If
End Sub
Could anyone help me with Steps 2-4 as above please?