My data provider has changed several things in the .XLSX file I get. I have added a new sub to fix the data according to the model this application expects:
Sub Feb27FixModel() ActiveSheet.Range("H2").End(xlDown).NumberFormat = "dd-mmm-yyyy" 'change format of Processed Date Dim colNum As Integer colNum = ActiveSheet.Rows(1).Find(what:="Legacy code", lookat:=xlWhole).Column ' Done twice to insert 2 new cols ActiveSheet.Columns(colNum + 1).Insert ActiveSheet.Columns(colNum + 1).Insert ' New col headings ActiveSheet.Cells(1, colNum + 1).Value = "Origin Code" ActiveSheet.Cells(1, colNum + 2).Value = "Jurisdiction Code" Range("A1").Select ActiveCell.FormulaR1C1 = "County Name" Range("B1").Select ActiveCell.FormulaR1C1 = "State Abbreviation" ActiveWorkbook.Save End Sub
Everything works EXCEPT the result of the 1st line above. In the active worksheet, column H has a header row value of “Processed Date” and the H2 cell and cells below it are stored as General with values like 11/15/2016. I need to change all the dates to a custom date as dd-mmm-yyyy. The statement below fails to get this done for me:
ActiveSheet.Range("H2").End(xlDown).NumberFormat = "dd-mmm-yyyy"
Thanks to the answer from @Jeeped below, I added another statement and this is the code that was the solution:
With ActiveSheet .Range(.Cells(2, "H"), .Cells(.Rows.Count, "H").End(xlUp)).NumberFormat = "dd-mmm-yyyy" .Range(.Cells(2, "H"), .Cells(.Rows.Count, "H").End(xlUp)).Value = ActiveCell.Text End With
I made a mistake yesterday. In debugging, I must have had a good cell selected in the worksheet at a breakpoint; hence, refering to ActiveCell.Text “sort of worked”. It had the effect of replicating the Text to be “14-Oct-2016” in EVERY row for column H (except the first row). This was minimally acceptable.
What I really need is a statement that will take the Text of all rows of column H and change the stored value from displaying as 10/14/2016 to 14-Oct-2016, etc. It is not enough to just change the format to custom. I need to change the stored values too. I don’t know how to do that.
You are only operating on the last cell with a value in column H. Span from H2 to the last cell to set the format of all of the cells.
with ActiveSheet .Range(.cells(2, "H"), .cells(.rows.count, "H").End(xlUp)).NumberFormat = "dd-mmm-yyyy" end with
This four-line-loop should do the trick. Tested it. 😉
For i = 2 To Columns("H").End(xlDown).Row 'from row nr 2 to the rownr of last cell of column H Set currentCell = Cells(i, "h") currentCell.Value = Format(currentCell.Value, "dd-mm-yyyy") Next i
It strolls though each cell and applies the Format() function on them to convert the text format. Let me know if that helps.
Just solved this for the same issue you have. Tested your case as well. The key is the TextToColumns command. With your code, give this a try:
With ActiveSheet.Range("H:H") .NumberFormat = "dd-mmm-yyyy" .TextToColumns End With