Hi Everyone,
I'm trying to create a macro that will run through a set of column headers and look for a specific header ("CMA - ERP Variance") and calculate a simple formula for all the cells within that column header. However, I'm running into an error with my code as I get the "Run-time error '1004': Unable to get the Match property of the WorksheetFunction class."
Can anyone help me debug this error and let me know what I'm doing wrong? My code is listed below. Thanks for all the help!
I'm trying to create a macro that will run through a set of column headers and look for a specific header ("CMA - ERP Variance") and calculate a simple formula for all the cells within that column header. However, I'm running into an error with my code as I get the "Run-time error '1004': Unable to get the Match property of the WorksheetFunction class."
Can anyone help me debug this error and let me know what I'm doing wrong? My code is listed below. Thanks for all the help!
VBA Code:
Sub Test_MatchReplace()
Dim TestHeader As Long
Dim TestCel As Range, TestCel2 As Range, TestCel3 As Range, TestHeader2 As Range, TestURange As Range
Dim ws As Worksheet
Set ws = ShTest
With Application
.StatusBar = "Your macro is running"
.ScreenUpdating = False
.DisplayAlerts = False
.Calculation = xlCalculationManual
End With
' To find and replace certain original headers on test data report
TestHeader = WorksheetFunction.Match("Amount", ShTest.Rows(1), 0)
Columns(TestHeader).Select
Selection.Replace What:="Amount", Replacement:="CMA Amount", lookat:=xlWhole _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
TestHeader = WorksheetFunction.Match("Balance", ShTest.Rows(1), 0)
Columns(TestHeader).Select
Selection.Replace What:="Balance", Replacement:="CMA Balance", lookat:=xlWhole _
, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
' To create a formula for the "CMA - ERP Variance" column
TestHeader2 = ShTest.Range(Cells(1, 1), Cells(1, ShTest.Cells(1, Columns.Count).End(xlToLeft).Column))
For Each TestCel In TestHeader2
If TestCel.Value = "CMA - ERP Variance" Then
For Each TestCel2 In ShTest.Range("M2:M" & Cells(Rows.Count, "M").End(xlUp).Row)
If Not IsEmpty(TestCel2.Value) Then
TestCel2.Offset(, 2).Formula = "=(RC[-2]-RC[-1])"
Else
TestCel2.Offset(, 2).Value = 0
End If
Next TestCel2
End If
Next TestCel
' To highlight any cells within the "CMA - ERP Variance" column that has a value not equal to 0
With ShTest
Set TestURange = .UsedRange
Set TestURange = .Range("A2").Resize(RowSize:=TestURange.Rows.Count - 1, ColumnSize:=TestURange.Columns.Count)
For Each TestCel3 In TestURange
If TestCel3.Column = 15 And TestCel3.Value <> 0 Then
TestCel3.Interior.ColorIndex = 6
End If
Next TestCel3
End With
With Application
.StatusBar = ""
.ScreenUpdating = True
.DisplayAlerts = True
.Calculation = xlCalculationAutomatic
.CutCopyMode = False
End With
End Sub