View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
news.sf.sbcglobal.net news.sf.sbcglobal.net is offline
external usenet poster
 
Posts: 1
Default Formatting and sorting cells with VBA

I need to apply date formatting to column A and then sort the entire
worksheet by column A ascending. I don't know how many rows/columns there
will be, but row 1 is always the header row.

I calculate the last row and column like this:

lc = .Range("A1").SpecialCells(xlCellTypeLastCell).Colu mn
lr = .Range("A1").SpecialCells(xlCellTypeLastCell).Row
(no problem using SpecialCells here)

I've tried the following VBA code, but no luck:

With xlapp.Workbooks(strXlsFile).Worksheets(sn(i))
.Range("A2:A" & lr).NumberFormat = "mm/dd/yyyy hh:mm;@"
End with

With xlapp.Workbooks(strXlsFile).Worksheets(sn(i))
.Range(.Cells(2, 1), .Cells(lr, lc)).NumberFormat = "mm/dd/yyyy hh:mm;@"
End with

Why is this not working? The macro recorder returns this:

Range("A2:A106").Select
Selection.NumberFormat = "mm/dd/yyyy hh:mm;@"

Do I need to select the range in VBA??

Thanks in advance.