J
Jack Sons
Hi all,
I have a directory with some 500 files with names 8.xls 23.xls
163.xls 166.xls and so on to 1842.xls.
In each of those files I want to change all dates with 2004 into the
corresponding 2003 dates.
In my code (see below) I call and open a file, say 163.xls, search and
replace 2004 into 2003 and then save the file and close it.
Then I call and open the file with a file number that is one higher than the
previous one, so in my example 164.xls.
Because many file numbers do not exist, like 164.xls in my example, I have
in my code a line that reads:
On Error GoTo ErIsGeenBestand
Now the thing that I don't understand. This all works well, but only once.
In the case of trying to open the non-existing 164.xls file, the execution
of the code indeed jumps from
Workbooks.Open Filename:=k & ".xls"
to
ErIsGeenBestand:
but when after that in the next loop the next non-existing file (165.xls) is
called the code will not jump again. I get an errormassage saying that
165.xls can't be found and please check the spelling of its name.
Why will On Error GoTo not work twice in a row? What is wrong with my code?
Jack Sons
The Netherlands
----------------------------------------------------------------------------
-------------------------------------------------
Sub MacroVan2004Naar2003()
Application.ScreenUpdating = False
ChDir "C:\Documents and Settings\2\Mijn documenten\Excel\OverzichtUlt2003"
Application.ScreenUpdating = False
k = 163
Do Until k = 189
On Error GoTo ErIsGeenBestand
Workbooks.Open Filename:=k & ".xls"
Columns("A:A").Select
Selection.Replace What:="2004", Replacement:="2003", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Range("A1").Select
ActiveWorkbook.Save
ActiveWorkbook.Close
ErIsGeenBestand:
k = k + 1
Loop
Application.ScreenUpdating = True
End Sub
I have a directory with some 500 files with names 8.xls 23.xls
163.xls 166.xls and so on to 1842.xls.
In each of those files I want to change all dates with 2004 into the
corresponding 2003 dates.
In my code (see below) I call and open a file, say 163.xls, search and
replace 2004 into 2003 and then save the file and close it.
Then I call and open the file with a file number that is one higher than the
previous one, so in my example 164.xls.
Because many file numbers do not exist, like 164.xls in my example, I have
in my code a line that reads:
On Error GoTo ErIsGeenBestand
Now the thing that I don't understand. This all works well, but only once.
In the case of trying to open the non-existing 164.xls file, the execution
of the code indeed jumps from
Workbooks.Open Filename:=k & ".xls"
to
ErIsGeenBestand:
but when after that in the next loop the next non-existing file (165.xls) is
called the code will not jump again. I get an errormassage saying that
165.xls can't be found and please check the spelling of its name.
Why will On Error GoTo not work twice in a row? What is wrong with my code?
Jack Sons
The Netherlands
----------------------------------------------------------------------------
-------------------------------------------------
Sub MacroVan2004Naar2003()
Application.ScreenUpdating = False
ChDir "C:\Documents and Settings\2\Mijn documenten\Excel\OverzichtUlt2003"
Application.ScreenUpdating = False
k = 163
Do Until k = 189
On Error GoTo ErIsGeenBestand
Workbooks.Open Filename:=k & ".xls"
Columns("A:A").Select
Selection.Replace What:="2004", Replacement:="2003", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Range("A1").Select
ActiveWorkbook.Save
ActiveWorkbook.Close
ErIsGeenBestand:
k = k + 1
Loop
Application.ScreenUpdating = True
End Sub