Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 220
Default Check values before running macro

I'm using the following code to reformat dates in 3 different columns,
for example, Column T goes from 2070126 to 01/26/2007. What I'd like
to do is add something to it that will check each column first to make
sure it hasnt already been reformatted. Any ideas?


Sub CleanUp_Data_File()

Application.ScreenUpdating = False

With ActiveSheet

' ~~ Reformat Column P ~~
Columns("P:P").Insert
Range("P3:P" & Range("Q65536").End(xlUp).Row).FormulaR1C1 = _
"=MID(RC[1],5,2)&""-""&MID(RC[1],1,3)&""-""&MID(RC[1],8,4)"
Range("P3:P" & Range("P65536").End(xlUp).Row).Copy
Range("Q3").PasteSpecial xlPasteValues, , False, False
Columns("P:P").EntireColumn.Delete

' ~~ Reformat Column S ~~
Columns("S:S").Insert
Range("S3:S" & Range("T65536").End(xlUp).Row).FormulaR1C1 = _
"=MID(RC[1],4,2)&""/""&MID(RC[1],6,2)&""/""&""20""&MID(RC[1],
2,2)"
Range("S3:S" & Range("S65536").End(xlUp).Row).Copy
Range("T3").PasteSpecial xlPasteValues, , False, False
Columns("S:S").EntireColumn.Delete

' ~~ Reformat Column T ~~
Columns("T:T").Insert
Range("T3:T" & Range("U65536").End(xlUp).Row).FormulaR1C1 = _
"=MID(RC[1],4,2)&""/""&MID(RC[1],6,2)&""/""&""20""&MID(RC[1],
2,2)"
Range("T3:T" & Range("T65536").End(xlUp).Row).Copy
Range("U3").PasteSpecial xlPasteValues, , False, False
Columns("T:T").EntireColumn.Delete

End With

Application.ScreenUpdating = True

End Sub


Thanks,
-- Dan

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Check values before running macro

Sub macro1()
Dim iEnd As Long
Dim ws As Worksheet
Dim c As Range
Dim rng As Range
Dim iCt As Integer

Set ws = ActiveSheet
cols = Array("P", "S", "T")
For iCt = 0 To 2
iEnd = ws.Cells(65536, cols(iCt)).End(xlUp).Row
Set rng = ws.Range(ws.Cells(3, cols(iCt)), Cells(iEnd,
cols(iCt)))
For Each c In rng
If InStr(c, "/") = False Then
c = Mid(c, 4, 2) & "/" & Mid(c, 6, 2) & "/" & "20" &
Mid(c, 2, 2)
Next c
Next iCt
End Sub

Hth,
Merjet

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 220
Default Check values before running macro

Wow, I didn't know you could use Mid like that... It gave me a 'Next
without For' error and highlighted "Next c" on the 3rd row from the
bottom. Also, I hate to pry but the formatting for Column P goes from
Jan 26 2007 12:00AM to 26-Jan-2007... Do you mind showing me how to
write that more efficiently as well?

Thank You,
-- Dan

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Check values before running macro

I suspect the errors were because lines in my code were broken when
posted. 'cols(iCt)))' goes with the line above it. Same for 'Mid(c, 2,
2)'.

I hate to pry but the formatting for Column P
goes from Jan 26 2007 12:00AM to 26-Jan-2007...


Not clear enough. Is that before or after reformating? Please post a
complete example of a 'before' and an 'after'.

Merjet

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Check values before running macro

P.S. " c = Mid(c, 4, 2) ... " goes with Then, or put " _" after
Then.

Merjet



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 220
Default Check values before running macro

Yep, that was it...

For column P, here's the formula I'm using in the spreadsheet:
=MID(P3,5,2)&"-"&MID(P3,1,3)&"-"&MID(P3,8,4)

Befo
Jan 26 2007 12:00AM
Jan 3 2007 12:00AM
Jan 3 2007 12:00AM
Jan 26 2007 12:00AM

After: (time is excluded)
26-Jan-2007
3-Jan-2007
3-Jan-2007
26-Jan-2007

Thanks Merjet

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 812
Default Check values before running macro

I can't think of anything more efficient than what you have.

Merjet


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clearing Check Boxes & Running a diff Macro when unchecking the ch Vick Excel Discussion (Misc queries) 6 May 30th 07 08:44 PM
How to check a cell for content before running macro. Incoherent Excel Programming 1 October 7th 05 04:42 PM
How to Check if a Process is Running Chaplain Doug Excel Programming 0 May 24th 05 05:29 PM
check for running app L3Tech Excel Programming 6 February 20th 04 12:50 AM
Check for Excel version before running? Ed[_9_] Excel Programming 2 December 18th 03 01:56 PM


All times are GMT +1. The time now is 11:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"