EXCEL VBA reformat cell formular after importing worksheet from another workbook

ElPabloSS

New Member
Joined
Sep 12, 2018
Messages
10
Hi guys,

I stuck with a problem where I hope you can help.

Situation:
I added a macro that allows me to import excel worksheets from other workbooks.
The imported sheets have formulas in cells "B6:CX49" still referencing to another sheet in the workbook from where I have imported the sheets from.
Example from the imported sheets:
  • =SUMIFS('[AB_.xlsm]ABC'!$AE:$AE;'[AB_.xlsm]ABC'!$D:$D;$A3;'[AB_.xlsm]ABC'!$V:$V;$A8; '[AB_.xlsm]ABC'!$U:$U;AM4)
I would need to change the forumlars to reference to the new local sheet like:
  • =SUMIFS(ABC!$AE:$AE;ABC!$D:$D;$A3;ABC!$V:$V;$A8; ABC!$U:$U;AM4)

Below the code how i import the worksheets:
Dim sheet As String
Dim TabList As String
Dim i As Long
Dim ws As Worksheet
Dim lastsheet As String
Dim directory As String, fileName As String
Application.ScreenUpdating = False
Application.DisplayAlerts = False
TabList = ""
lastsheet = "template"
directory = ""
fileName = ""
i = 4
sheet = ActiveSheet.Name
Workbook = ActiveWorkbook.Name
For Each ws In Worksheets
TabList = TabList & ws.Name & vbCr
Next ws
lastRow = ThisWorkbook.Sheets(ActiveSheet.Name).Range("A" & Rows.Count).End(xlUp).Row
Set Rng = ThisWorkbook.Sheets(ActiveSheet.Name).Range("A" & "4:" & "A" & lastRow)
For Each cell In Rng
If cell.Value <> "" And InStr(1, TabList, cell.Value) = 0 Then
If directory <> ThisWorkbook.Sheets(sheet).Range("B" & i).Value Or fileName <> ThisWorkbook.Sheets(sheet).Range("C" & i).Value Then
directory = ThisWorkbook.Sheets(sheet).Range("B" & i).Value
fileName = ThisWorkbook.Sheets(sheet).Range("C" & i).Value
Workbooks.Open (directory & fileName)
Workbooks(fileName).Worksheets(cell.Value).Copy _
after:=Workbooks(Workbook).Worksheets(lastsheet)
lastsheet = cell.Value
Else
Workbooks(fileName).Worksheets(cell.Value).Copy _
after:=Workbooks(Workbook).Worksheets(lastsheet)
lastsheet = cell.Value
End If
End If
i = i + 1
Next cell
On Error Resume Next
Workbooks(fileName).Close
On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
ThisWorkbook.Sheets(sheet).Activate


Hope you can help,
Thanks
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi,

Unfortunately it is complicated to test but an ugly way would be
Code:
[LEFT][COLOR=#303336][FONT=Consolas]Cells[/FONT][/COLOR][COLOR=#303336][FONT=Consolas].[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]Replace What[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]"'["[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]&[/FONT][/COLOR][COLOR=#ff0000][FONT=Verdana][COLOR=#ff0000][FONT=Consolas]filename[/FONT][/COLOR][/FONT][/COLOR][COLOR=#303336][FONT=Consolas]&[/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]"]"[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] Replacement[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]"'"[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] LookAt[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]xlPart[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] SearchOrder[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]xlByRows[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] MatchCase[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] _
        [/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]False[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] SearchFormat[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]False[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] ReplaceFormat[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]False[/FONT][/COLOR][/LEFT]

where you set up filename as Initial workbook name...or more general principle but cleaner/quicker:

Code:
[LEFT][COLOR=#222222][FONT=Verdana]ActiveWorkbook.ChangeLink Name:="[/FONT][/COLOR][COLOR=#ff0000][FONT=Verdana]filename[/FONT][/COLOR][COLOR=#222222][FONT=Verdana].xlsm", NewName:="[/FONT][/COLOR][COLOR=#008000]Book2[/COLOR][COLOR=#222222][FONT=Verdana].xlsm", [/FONT][/COLOR][COLOR=#222222][FONT=Verdana]Type:=xlExcelLinks[/FONT][/COLOR][/LEFT]

where Book2 is the workbook where you want the part of the formula erased and launch macro from.
 
Last edited:
Upvote 0
Hi,

Unfortunately it is complicated to test but an ugly way would be
Code:
[LEFT][COLOR=#303336][FONT=Consolas]Cells[/FONT][/COLOR][COLOR=#303336][FONT=Consolas].[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]Replace What[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]"'["[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]&[/FONT][/COLOR][COLOR=#ff0000][FONT=Verdana][COLOR=#ff0000][FONT=Consolas]filename[/FONT][/COLOR][/FONT][/COLOR][COLOR=#303336][FONT=Consolas]&[/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]"]"[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] Replacement[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]"'"[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] LookAt[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]xlPart[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] SearchOrder[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]xlByRows[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] MatchCase[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] _
        [/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]False[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] SearchFormat[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]False[/FONT][/COLOR][COLOR=#303336][FONT=Consolas],[/FONT][/COLOR][COLOR=#303336][FONT=Consolas] ReplaceFormat[/FONT][/COLOR][COLOR=#303336][FONT=Consolas]:=[/FONT][/COLOR][COLOR=#7d2727][FONT=Consolas]False[/FONT][/COLOR][/LEFT]

where you set up filename as Initial workbook name...or more general principle but cleaner/quicker:

Code:
[LEFT][COLOR=#222222][FONT=Verdana]ActiveWorkbook.ChangeLink Name:="[/FONT][/COLOR][COLOR=#ff0000][FONT=Verdana]filename[/FONT][/COLOR][COLOR=#222222][FONT=Verdana].xlsm", NewName:="[/FONT][/COLOR][COLOR=#008000]Book2[/COLOR][COLOR=#222222][FONT=Verdana].xlsm", [/FONT][/COLOR][COLOR=#222222][FONT=Verdana]Type:=xlExcelLinks[/FONT][/COLOR][/LEFT]

where Book2 is the workbook where you want the part of the formula erased and launch macro from.



Hi Kamolga,
thanks for your help.

The cell.replace function is not working to change formulas, I think it's only changing the value of a cell.

"ActiveWorkbook.ChangeLink" works, thanks - I was just wondering if there's a similar option for worksheets, just in case you would like to keep a special link in one of your worksheets.
 
Upvote 0
The cell.replace function is not working to change formulas, I think it's only changing the value of a cell.

"ActiveWorkbook.ChangeLink" works, thanks - I was just wondering if there's a similar option for worksheets, just in case you would like to keep a special link in one of your worksheets.

Then indeed you would need to replace formula in specific ranges.

What about

Code:
[LEFT][COLOR=#252c2f][FONT=Courier]Cell.Formula = Replace(Cell.Formula,[COLOR=#7d2727][COLOR=#7d2727][FONT=monospace][I][FONT=Consolas]"'["[/FONT][/I][/FONT][/COLOR][/COLOR][COLOR=#303336][COLOR=#303336][FONT=monospace][I][FONT=Consolas]&[/FONT][/I][/FONT][/COLOR][/COLOR][COLOR=#ff0000][COLOR=#ff0000][FONT=monospace][I][FONT=Verdana][COLOR=#ff0000][FONT=Consolas]filename[/FONT][/COLOR][/FONT][/I][/FONT][/COLOR][/COLOR][COLOR=#303336][COLOR=#303336][FONT=monospace][I][FONT=Consolas]&[/FONT][/I][/FONT][/COLOR][/COLOR][COLOR=#7d2727][COLOR=#7d2727][FONT=monospace][I][FONT=Consolas]"]"[/FONT][/I][/FONT][/COLOR][/COLOR], [COLOR=#7d2727][COLOR=#7d2727][FONT=monospace][I][FONT=Consolas]"'"[/FONT][/I][/FONT][/COLOR][/COLOR])[/FONT][/COLOR][/LEFT]
then?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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