All2Cheesy
Board Regular
- Joined
- Mar 4, 2015
- Messages
- 127
Hello all,
I'm attempting to run a macro over multiple workbooks which clean up, and add additional data into each workbook.
For the most part, this macro runs correctly, however, one section will only run properly if stepped through in debug mode. If run normally it will have no effect.
What I am trying to do is replace any instance of the date 01/01/2020, with '1-1. Below is the section of code which is not running correctly.
Is there a way to force excel to execute this section of the macro? Any assistance is greatly appreciated.
For reference, here is the full macro.
I'm attempting to run a macro over multiple workbooks which clean up, and add additional data into each workbook.
For the most part, this macro runs correctly, however, one section will only run properly if stepped through in debug mode. If run normally it will have no effect.
What I am trying to do is replace any instance of the date 01/01/2020, with '1-1. Below is the section of code which is not running correctly.
VBA Code:
Rows("1:1").Select
Cells.Replace What:=CDate("1/01/2020"), Replacement:="'1-1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Is there a way to force excel to execute this section of the macro? Any assistance is greatly appreciated.
For reference, here is the full macro.
VBA Code:
Sub ProcessFiles()
'Disable screen updating
With Application
.ScreenUpdating = False
.Calculation = xlManual
.EnableEvents = False
.DisplayAlerts = False
End With
'***********************************************************************Overwrite Rate Files***********************************************************************
'Declare variables
Dim Filename, Pathname As String
Dim wb As Workbook
'Locate file path to be changed
Pathname = ActiveWorkbook.Path & "C:\Test\"
Filename = Dir(Pathname & "*.CSV")
'Run DoWork sub - loop for all files
Do While Filename <> ""
Set wb = Workbooks.Open(Pathname & Filename)
DoWork wb
wb.Close SaveChanges:=True
Filename = Dir()
Loop
'Enable screen updating
With Application
.ScreenUpdating = True
.Calculation = xlAutomatic
.EnableEvents = True
.DisplayAlerts = True
End With
End Sub
Sub DoWork(wb As Workbook)
With wb
'***********************************************************************Create Melbourne 2 In Rate Files***********************************************************************
'Replaces incorrect header formats
'Replace 1-1 date error
Rows("1:1").Select
Cells.Replace What:=CDate("1/01/2020"), Replacement:="'1-1", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Replace blanks
Selection.Replace What:="", Replacement:="0-0", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Applies a filter to data to only show cons receiveing into Melbourne
ActiveSheet.Range("$A:$AS").AutoFilter Field:=3, Criteria1:= _
"Melbourne"
'Applies a filter to data to only show a kilo charge
ActiveSheet.Range("$A:$AS").AutoFilter Field:=8, Criteria1:= _
"Kilogram"
'Selects first cell after header row
Range("A2").Select
'Selects all used cells underneath cell A2
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
'Copies selected cells
Selection.Copy
'Selects first available blank cell
Columns("A:A").Select
Selection.Find(What:="", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Select
'Pastes copied cells into selected cell
ActiveSheet.Paste
'Replaces any instances of Melbourne with Melbourne 2 in selected range
Selection.Replace What:="Melbourne", Replacement:="Melbourne 2", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Removes filters
ActiveSheet.ShowAllData
End With
End Sub