I receive a report that has row 1 headings that include "date" with cell text strings of "yyyy-mm-dd." What I produce is a series of date based windows. So the need is to look in row 1 for all occurrences of the string "date", select the column, find "-" & replace with "/" then format the column as Date. But nothing I've tried to simplify the recorded macro code, remove the recorded column numbers, etc. works.
So I'm asking for help . . . I've left in everything I've tried as remarks to assist in pointing out the errors of my thoughts.
Bottom Line Up Front:
3 problems
1. The error message is that I'm not correctly setting the Object
2. Next isn't working. Column 1 is selected then the macro stops.
3. I can't figure out how to change "ReplaceFormat:=False" as seen below . . .
As my guidance for setting the range, I'm attempting to use a posting I found at StackOverFlow https://stackoverflow.com/questions...ce-between-dim-and-set-in-vba/3884929#3884929
TIA
Ron
So I'm asking for help . . . I've left in everything I've tried as remarks to assist in pointing out the errors of my thoughts.
Bottom Line Up Front:
3 problems
1. The error message is that I'm not correctly setting the Object
2. Next isn't working. Column 1 is selected then the macro stops.
3. I can't figure out how to change "ReplaceFormat:=False" as seen below . . .
As my guidance for setting the range, I'm attempting to use a posting I found at StackOverFlow https://stackoverflow.com/questions...ce-between-dim-and-set-in-vba/3884929#3884929
Set statement is only used for object variables (like Range, Cell or Worksheet in Excel)
The simple equal sign '=' is used for elementary datatypes like Integer.
Dim declares the variable.
Dim r As Range
Set sets the variable to an object reference.
Set r = Range("A1")
TIA
Ron
Code:
Sub tm_Find_Change_Date_Format()
'test macro to fix text dates
Dim LastCol As Integer
Dim LastRow As Long
Dim hRow As Range
Dim thiswksht As Worksheet
'
Set thiswksht = ActiveSheet
Set thiswb = ActiveWorkbook
'
With thiswksht
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
End With
'
' ---------------- Failed -----------------------------
'Set hRow = Range(Cells(1, LastCol))
'Cells(1,1).Activate
'Set hRow = Range(ActiveCell.EntireRow)
'Range(Cells(1, LastCol)).Name = "hRow"
'Range(Selection, Selection.End(xlToRight)).Select
'Selection.Name = "hRow"
'Range("hRow").Select
'Rows("1").Select
'Range("1:1").Select
' ---------------------------------------------------
For Each cell In Selection
If InStr(cell.value, "date") > 0 Then
ActiveCell.EntireColumn.Select
Selection.Replace What:="-", Replacement:="/", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.NumberFormat = "mm/dd/yyyy"
Cells(1, ActiveCell.Column).Select
End If
Next cell
'
Cells(1, 1).Activate
End Sub
Last edited by a moderator: