Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Find and Replace on all worksheets on all files in a folder

I am trying to find and replace 3 things on all worksheets on all excel
files in a folder.

I have a macro that executes a few API Calls for me to select the
folder for the problematic macro to run in.

here is the code:

Sub TestFile6()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
Dim wks As Worksheet
SaveDriveDir = CurDir
MyPath = GetDirectory(MyPath)
ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xnv")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If

Application.ScreenUpdating = False
Set basebook = ThisWorkbook
Do While FNames < ""
Set mybook = Workbooks.Open(FNames)

For Each wks In Worksheets
With wks
Range("A1").Select

Cells.Replace What:="ALF_STATE", Replacement:="CHARTFIELD1",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

Cells.Replace What:="ALF_POOL_INDICATOR",
Replacement:="CHARTFIELD2", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

Cells.Replace What:="ALF_REINSURANCE_CD",
Replacement:="CHARTFIELD3", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
End With
Next wks
ActiveWorkbook.Save
On Error Resume Next
On Error GoTo 0
mybook.Close False
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub


This is not finding and replacing and I do not know why, I do know it
is opening and closing each file, I can see that in the taskbar as the
macro runs.

Any ideas?

Rob Slagle

robslagleATyahooDOTcom

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 983
Default Find and Replace on all worksheets on all files in a folder

As a guess try being more explicit with your refernces to the workbook...
Specifically

For Each wks In mybook.Worksheets

HTH

"Rob Slagle" wrote:

I am trying to find and replace 3 things on all worksheets on all excel
files in a folder.

I have a macro that executes a few API Calls for me to select the
folder for the problematic macro to run in.

here is the code:

Sub TestFile6()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
Dim wks As Worksheet
SaveDriveDir = CurDir
MyPath = GetDirectory(MyPath)
ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xnv")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If

Application.ScreenUpdating = False
Set basebook = ThisWorkbook
Do While FNames < ""
Set mybook = Workbooks.Open(FNames)

For Each wks In Worksheets
With wks
Range("A1").Select

Cells.Replace What:="ALF_STATE", Replacement:="CHARTFIELD1",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

Cells.Replace What:="ALF_POOL_INDICATOR",
Replacement:="CHARTFIELD2", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

Cells.Replace What:="ALF_REINSURANCE_CD",
Replacement:="CHARTFIELD3", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
End With
Next wks
ActiveWorkbook.Save
On Error Resume Next
On Error GoTo 0
mybook.Close False
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub


This is not finding and replacing and I do not know why, I do know it
is opening and closing each file, I can see that in the taskbar as the
macro runs.

Any ideas?

Rob Slagle

robslagleATyahooDOTcom


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Find and Replace on all worksheets on all files in a folder

I have done this with no success.

Rob Slagle



Jim Thomlinson wrote:
As a guess try being more explicit with your refernces to the

workbook...
Specifically

For Each wks In mybook.Worksheets

HTH

"Rob Slagle" wrote:

I am trying to find and replace 3 things on all worksheets on all

excel
files in a folder.

I have a macro that executes a few API Calls for me to select the
folder for the problematic macro to run in.

here is the code:

Sub TestFile6()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
Dim wks As Worksheet
SaveDriveDir = CurDir
MyPath = GetDirectory(MyPath)
ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xnv")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If

Application.ScreenUpdating = False
Set basebook = ThisWorkbook
Do While FNames < ""
Set mybook = Workbooks.Open(FNames)

For Each wks In Worksheets
With wks
Range("A1").Select

Cells.Replace What:="ALF_STATE",

Replacement:="CHARTFIELD1",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

Cells.Replace What:="ALF_POOL_INDICATOR",
Replacement:="CHARTFIELD2", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

Cells.Replace What:="ALF_REINSURANCE_CD",
Replacement:="CHARTFIELD3", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
End With
Next wks
ActiveWorkbook.Save
On Error Resume Next
On Error GoTo 0
mybook.Close False
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub


This is not finding and replacing and I do not know why, I do know

it
is opening and closing each file, I can see that in the taskbar as

the
macro runs.

Any ideas?

Rob Slagle

robslagleATyahooDOTcom



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Find and Replace on all worksheets on all files in a folder

See it this works:

Sub TestFile6()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
Dim wks As Worksheet
SaveDriveDir = CurDir
MyPath = GetDirectory(MyPath)
ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xnv")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If

Application.ScreenUpdating = False
Set basebook = ThisWorkbook
Do While FNames < ""
Set mybook = Workbooks.Open(FNames)

For Each wks In MyBook.Worksheets
With wks
' Range("A1").Select

wks.Cells.Replace What:="ALF_STATE", _
Replacement:="CHARTFIELD1", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False

wks.Cells.Replace What:="ALF_POOL_INDICATOR", _
Replacement:="CHARTFIELD2", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False

wks.Cells.Replace What:="ALF_REINSURANCE_CD", _
Replacement:="CHARTFIELD3", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
End With
Next wks
Mybook.Save
On Error Resume Next
On Error GoTo 0
mybook.Close False
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub

I believe will help.

--
Regards,
Tom Ogilvy

"Rob Slagle" wrote in message
ups.com...
I have done this with no success.

Rob Slagle



Jim Thomlinson wrote:
As a guess try being more explicit with your refernces to the

workbook...
Specifically

For Each wks In mybook.Worksheets

HTH

"Rob Slagle" wrote:

I am trying to find and replace 3 things on all worksheets on all

excel
files in a folder.

I have a macro that executes a few API Calls for me to select the
folder for the problematic macro to run in.

here is the code:

Sub TestFile6()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim FNames As String
Dim MyPath As String
Dim SaveDriveDir As String
Dim wks As Worksheet
SaveDriveDir = CurDir
MyPath = GetDirectory(MyPath)
ChDrive MyPath
ChDir MyPath
FNames = Dir("*.xnv")
If Len(FNames) = 0 Then
MsgBox "No files in the Directory"
ChDrive SaveDriveDir
ChDir SaveDriveDir
Exit Sub
End If

Application.ScreenUpdating = False
Set basebook = ThisWorkbook
Do While FNames < ""
Set mybook = Workbooks.Open(FNames)

For Each wks In Worksheets
With wks
Range("A1").Select

Cells.Replace What:="ALF_STATE",

Replacement:="CHARTFIELD1",
LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

Cells.Replace What:="ALF_POOL_INDICATOR",
Replacement:="CHARTFIELD2", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False

Cells.Replace What:="ALF_REINSURANCE_CD",
Replacement:="CHARTFIELD3", LookAt:= _
xlPart, SearchOrder:=xlByRows, MatchCase:=False,
SearchFormat:=False, _
ReplaceFormat:=False
End With
Next wks
ActiveWorkbook.Save
On Error Resume Next
On Error GoTo 0
mybook.Close False
FNames = Dir()
Loop
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub


This is not finding and replacing and I do not know why, I do know

it
is opening and closing each file, I can see that in the taskbar as

the
macro runs.

Any ideas?

Rob Slagle

robslagleATyahooDOTcom





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
How to find all folder and/or files in a specific location Mr. GetRight Excel Discussion (Misc queries) 0 July 2nd 08 06:51 PM
Macro Involving Find & Replace In Two Different Files K8_Dog[_2_] Excel Discussion (Misc queries) 4 May 2nd 08 07:44 PM
VBA to find Cell Range in Files in Folder, return value Tom Ogilvy Excel Programming 1 August 26th 04 07:00 PM
Copy several range from all files in folder into several worksheets Adri[_2_] Excel Programming 13 June 27th 04 03:52 PM
how to step thru only one subfolder of a folder to find like files foamfollower Excel Programming 1 November 11th 03 08:40 PM


All times are GMT +1. The time now is 09:20 PM.

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

About Us

"It's about Microsoft Excel"