Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Macro to Find and Replace

I have recorded a macro to find and replace all the dates but when I run it
it doesn't actually change them. I want to change all the dates so that they
are all the 1st of the the month instead of specifying the day. I can't think
of an easier way of doing it so if anyone has any better ideas it'll be very
appreciated!!!

Here's the code:

Columns("U:U").Select
Selection.Replace What:="??/12/2005", Replacement:="01/12/2005",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/01/2006", Replacement:="01/01/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/02/2006", Replacement:="01/02/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/03/2006", Replacement:="01/03/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

It doesn't come up with an error, it just doesn't do it!

Thanks in advance!!!!
  #2   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 5,441
Default Macro to Find and Replace

Insert a new column, and use a formula like

=DATE(YEAR(U2),MONTH(U2),1)

Copy that formula down to match your dates (you may need some error checking with that), then copy
and pastespecial values over your original dates, then delete your new column. Can be done with a
macro as well...

HTH,
Bernie
MS Excel MVP


"R Storey" wrote in message
...
I have recorded a macro to find and replace all the dates but when I run it
it doesn't actually change them. I want to change all the dates so that they
are all the 1st of the the month instead of specifying the day. I can't think
of an easier way of doing it so if anyone has any better ideas it'll be very
appreciated!!!

Here's the code:

Columns("U:U").Select
Selection.Replace What:="??/12/2005", Replacement:="01/12/2005",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/01/2006", Replacement:="01/01/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/02/2006", Replacement:="01/02/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/03/2006", Replacement:="01/03/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

It doesn't come up with an error, it just doesn't do it!

Thanks in advance!!!!



  #3   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Macro to Find and Replace

I would insert a helper column and use a formula like:

=U1-DAY(U1)+1
drag it down the column
Copy and paste special|values over column U and delete that helper column.

R Storey wrote:

I have recorded a macro to find and replace all the dates but when I run it
it doesn't actually change them. I want to change all the dates so that they
are all the 1st of the the month instead of specifying the day. I can't think
of an easier way of doing it so if anyone has any better ideas it'll be very
appreciated!!!

Here's the code:

Columns("U:U").Select
Selection.Replace What:="??/12/2005", Replacement:="01/12/2005",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/01/2006", Replacement:="01/01/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/02/2006", Replacement:="01/02/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/03/2006", Replacement:="01/03/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

It doesn't come up with an error, it just doesn't do it!

Thanks in advance!!!!


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 2
Default Macro to Find and Replace

Thanks to both of you, both options work fantastically!!!

"Dave Peterson" wrote:

I would insert a helper column and use a formula like:

=U1-DAY(U1)+1
drag it down the column
Copy and paste special|values over column U and delete that helper column.

R Storey wrote:

I have recorded a macro to find and replace all the dates but when I run it
it doesn't actually change them. I want to change all the dates so that they
are all the 1st of the the month instead of specifying the day. I can't think
of an easier way of doing it so if anyone has any better ideas it'll be very
appreciated!!!

Here's the code:

Columns("U:U").Select
Selection.Replace What:="??/12/2005", Replacement:="01/12/2005",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/01/2006", Replacement:="01/01/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/02/2006", Replacement:="01/02/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/03/2006", Replacement:="01/03/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

It doesn't come up with an error, it just doesn't do it!

Thanks in advance!!!!


--

Dave Peterson

  #5   Report Post  
Posted to microsoft.public.excel.misc
CLR CLR is offline
external usenet poster
 
Posts: 1,998
Default Macro to Find and Replace

A Macro approach, assuming your dates in column A..........

Sub ChangeDayTo1stOfMonth()
'======================================
'Changes all dates in column A to the first day of the month
'======================================
Dim lastrow As Long, r As Long
Dim num1
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
On Error Resume Next
For r = lastrow To 1 Step -1
If Cells(r, "A") 0 Then
Cells(r, "A").Select
num1 = Selection.Value
End If
num1 = Format(num1, "m/1/yyyy")
With ActiveCell
.Value = num1
End With
Next r
End Sub

Vaya con Dios,
Chuck, CABGx3




"R Storey" wrote:

I have recorded a macro to find and replace all the dates but when I run it
it doesn't actually change them. I want to change all the dates so that they
are all the 1st of the the month instead of specifying the day. I can't think
of an easier way of doing it so if anyone has any better ideas it'll be very
appreciated!!!

Here's the code:

Columns("U:U").Select
Selection.Replace What:="??/12/2005", Replacement:="01/12/2005",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/01/2006", Replacement:="01/01/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/02/2006", Replacement:="01/02/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/03/2006", Replacement:="01/03/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

It doesn't come up with an error, it just doesn't do it!

Thanks in advance!!!!



  #6   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Macro to Find and Replace

I think that this could be dangerous.

num1 = Format(num1, "m/1/yyyy")

It relies on excel parsing that entry. If your date settings are mdy, then no
problem. But if your settings are dmy, you may not get the date you want--and
if your settings are ymd, you may not even get a date.

I think I'd use:
num1 = dateserial(year(num1),month(num1),1)

And apply a nice unambiguous .numberformat to that range so that I could verify.



CLR wrote:

A Macro approach, assuming your dates in column A..........

Sub ChangeDayTo1stOfMonth()
'======================================
'Changes all dates in column A to the first day of the month
'======================================
Dim lastrow As Long, r As Long
Dim num1
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
On Error Resume Next
For r = lastrow To 1 Step -1
If Cells(r, "A") 0 Then
Cells(r, "A").Select
num1 = Selection.Value
End If
num1 = Format(num1, "m/1/yyyy")
With ActiveCell
.Value = num1
End With
Next r
End Sub

Vaya con Dios,
Chuck, CABGx3

"R Storey" wrote:

I have recorded a macro to find and replace all the dates but when I run it
it doesn't actually change them. I want to change all the dates so that they
are all the 1st of the the month instead of specifying the day. I can't think
of an easier way of doing it so if anyone has any better ideas it'll be very
appreciated!!!

Here's the code:

Columns("U:U").Select
Selection.Replace What:="??/12/2005", Replacement:="01/12/2005",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/01/2006", Replacement:="01/01/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/02/2006", Replacement:="01/02/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/03/2006", Replacement:="01/03/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

It doesn't come up with an error, it just doesn't do it!

Thanks in advance!!!!


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.misc
CLR CLR is offline
external usenet poster
 
Posts: 1,998
Default Macro to Find and Replace

Point well taken Dave.........I just used MY format (figuring everybody else
does the same) and failed to observe the OP's format........

thanks for the head's up

Vaya con Dios,
Chuck, CABGx3



"Dave Peterson" wrote:

I think that this could be dangerous.

num1 = Format(num1, "m/1/yyyy")

It relies on excel parsing that entry. If your date settings are mdy, then no
problem. But if your settings are dmy, you may not get the date you want--and
if your settings are ymd, you may not even get a date.

I think I'd use:
num1 = dateserial(year(num1),month(num1),1)

And apply a nice unambiguous .numberformat to that range so that I could verify.



CLR wrote:

A Macro approach, assuming your dates in column A..........

Sub ChangeDayTo1stOfMonth()
'======================================
'Changes all dates in column A to the first day of the month
'======================================
Dim lastrow As Long, r As Long
Dim num1
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
On Error Resume Next
For r = lastrow To 1 Step -1
If Cells(r, "A") 0 Then
Cells(r, "A").Select
num1 = Selection.Value
End If
num1 = Format(num1, "m/1/yyyy")
With ActiveCell
.Value = num1
End With
Next r
End Sub

Vaya con Dios,
Chuck, CABGx3

"R Storey" wrote:

I have recorded a macro to find and replace all the dates but when I run it
it doesn't actually change them. I want to change all the dates so that they
are all the 1st of the the month instead of specifying the day. I can't think
of an easier way of doing it so if anyone has any better ideas it'll be very
appreciated!!!

Here's the code:

Columns("U:U").Select
Selection.Replace What:="??/12/2005", Replacement:="01/12/2005",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/01/2006", Replacement:="01/01/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/02/2006", Replacement:="01/02/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="??/03/2006", Replacement:="01/03/2006",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

It doesn't come up with an error, it just doesn't do it!

Thanks in advance!!!!


--

Dave Peterson

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
Macro to Find & Replace [email protected] Excel Worksheet Functions 2 September 14th 06 07:17 PM
find and replace macro strange behaviour Nicawette Excel Discussion (Misc queries) 3 June 13th 06 08:49 PM
Macro: Find and replace Bertie Excel Discussion (Misc queries) 1 May 29th 06 02:01 PM
Find & Replace in VB macro JackC Excel Discussion (Misc queries) 1 August 24th 05 09:22 PM
macro to Find Replace in Excel Nurddin Excel Discussion (Misc queries) 7 January 3rd 05 04:29 AM


All times are GMT +1. The time now is 02:21 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"