Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

excel macro programming....

Status
Not open for further replies.

rpatel18

IS-IT--Management
Jun 15, 2007
32
0
0
US
I have the following macro in excel..
Sub replace()
'
' replace Macro
' Macro recorded 6/18/2007 by LISC USER
'

Range("B1:CM4000").Select
Selection.replace What:="** ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False

Range("B1:CM4000").Select
Selection.replace What:="* ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False

Range("B1:CM4000").Select
Selection.replace What:="--", Replacement:="0", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False

Range("B1:CM4000").Select
Selection.replace What:="* ", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
ActiveWorkbook.Save
End Sub

the macro works great but I have to do it for 50 files all in the same folder(C:\dbase). I know there is a way to loop it and tell it to open, run the macro, save and close, but dont quite remember how to do it.
 




Hi,

Use a FileScripting Object to define the folder and loop thru the Files collection.

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 




sorry...

a Scripting.FileSystemObject

Skip,
[sub]
[glasses] [red][/red]
[tongue][/sub]
 
I used this macro:
Sub RunCodeOnAllXLSFiles()
Dim i As Integer
Dim wbResults As Workbook
Dim wbCodeBook As Workbook

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False

On Error Resume Next

Set wbCodeBook = ThisWorkbook

With Application.FileSearch
.NewSearch
.LookIn = "C:\MyDocuments\TestResults"
.FileType = msoFileTypeExcelWorkbooks

If .Execute > 0 Then
For i = 1 To .FoundFiles.Count

Set wbResults = Workbooks.Open(.FoundFiles(i))

'my codes here

Next i
End If
End With

On Error GoTo 0
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub

but wat if all the files are csv files and not xls?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top