I'm using the code that is column letter agnostic. It searches for the column header by name:
Dim ws As Worksheet
Dim column1, column2 As String
Dim lr, lc As Long
Dim col1, col2 As Range
Set ws = ThisWorkbook.Sheets("Sheet1")
lc = ws.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
With ws
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lr = .Cells.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lr = 1
End If
End With
column1 = "Column 1"
column2 = "Column 2"
Set col1 = ws.Rows(1).Find(What:=column1, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
MsgBox(col1.Column)
Set col2 = ws.Rows(1).Find(What:=column2, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows,
SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Column
MsgBox(col2.Column)
With ws.Range(.Cells(2, 1), .Cells(lr, lc))
' The line below this generates a Run-time error 1004:
' Application-defined or Object-defined error with or without
' the ws.Range(ws.Cells(#, #), ws.Cells(#, #) qualifier.
.Range(.Cells(2, col1.Column), .Cells(lr,
col1.Column)).FormulaR1C1 =
"=IF(LEN(RC[col2.Column])>1,RC[col2.Column]*1,"""")"
End With
Column 2 has dates, some with leading zeros, that I want to remove. I plan on placing these dates in Column 1 after applying the above formula. Then:
With ws.Range(.Cells(2, 1), .Cells(lr, lc))
.Range(.Cells(2, col1.Column), .Cells(lr, col1.Column)).Copy
.Range(.Cells(2, col1.Column), .Cells(lr,
col1.Column)).PasteSpecial xlValues
.Range(.Cells(2, col2.Column), .Cells(lr,
col2.Column)).FormulaR1C1 = "=RC[col1.Column]"
End With
^ I'm guessing that this will also generate a Run-time error 1004: Application-defined or Object-defined error.
Any ideas to correct this will be greatly appreciated! Thanks in advance! Mods - PLEASE note that this question is NOT answered anywhere on StackOverflow. Linked answers have been applied, and they fail to solve this issue.