LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Batching files through a macro

I got rid of the Select/Selection stuff (but I think I did the same as your
code):

Option Explicit
Sub testme01()

Dim tempWkbk As Workbook

Dim myNames() As String
Dim fCtr As Long
Dim myFile As String
Dim myPath As String

Dim newName As String

'change to point at the folder to check
myPath = "C:\Documents and Settings\mpethel\My Documents\Excel"
If Right(myPath, 1) < "\" Then
myPath = myPath & "\"
End If

myFile = Dir(myPath & "*.xls")
If myFile = "" Then
MsgBox "no files found"
Exit Sub
End If

'get the list of files
fCtr = 0
Do While myFile < ""
fCtr = fCtr + 1
ReDim Preserve myNames(1 To fCtr)
myNames(fCtr) = myFile
myFile = Dir()
Loop

If fCtr 0 Then

For fCtr = LBound(myNames) To UBound(myNames)
Set tempWkbk = Workbooks.Open(Filename:=myPath & myNames(fCtr))

With ActiveSheet
.Columns("A:F").Delete Shift:=xlToLeft
.Columns("C:C").Cut
.Columns("A:A").Insert Shift:=xlToRight
.Columns("C:C").Cut
.Columns("B:B").Insert Shift:=xlToRight
.Rows("1:1").Delete Shift:=xlUp
End With

'get rid of the .xls extension
newName = Left(tempWkbk.FullName, Len(tempWkbk.FullName) - 4)

'save it as .csv
tempWkbk.SaveAs Filename:=newName, FileFormat:=xlCSV

'close it without saving
tempWkbk.Close savechanges:=False

Next fCtr

End If

End Sub


emt29165 wrote:

Thanks Dave,
I added my code and it works great.
One more question...how can I get them to save in .csv rather than back to
xls?
Here is the complete code as I have it (with mine inserted into yours)....
[thanks again!]

Sub testme01()

Dim tempWkbk As Workbook

Dim myNames() As String
Dim fCtr As Long
Dim myFile As String
Dim myPath As String

'change to point at the folder to check
myPath = "C:\Documents and Settings\mpethel\My Documents\Excel"
If Right(myPath, 1) < "\" Then
myPath = myPath & "\"
End If

myFile = Dir(myPath & "*.xls")
If myFile = "" Then
MsgBox "no files found"
Exit Sub
End If

'get the list of files
fCtr = 0
Do While myFile < ""
fCtr = fCtr + 1
ReDim Preserve myNames(1 To fCtr)
myNames(fCtr) = myFile
myFile = Dir()
Loop

If fCtr 0 Then

For fCtr = LBound(myNames) To UBound(myNames)
Set tempWkbk = Workbooks.Open(Filename:=myPath & myNames(fCtr))

Columns("A:F").Select
Selection.ClearContents
Selection.Delete Shift:=xlToLeft
Columns("C:C").Select
Selection.Cut
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
Columns("C:C").Select
Selection.Cut
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Rows("1:1").Select
Selection.Delete Shift:=xlUp
Cells.Select

tempWkbk.Close savechanges:=True

Next fCtr

End If

End Sub


--

Dave Peterson
 
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
importing files with a macro JT[_2_] Excel Programming 2 August 9th 04 10:14 PM
Combining Macro files CPower[_21_] Excel Programming 0 July 8th 04 05:17 PM
Same macro in two files David Miller Excel Programming 2 January 5th 04 08:01 AM
Import multiple files macro can't find files Steven Rosenberg Excel Programming 1 August 7th 03 01:47 AM
Run Macro on many files at once steve Excel Programming 0 July 23rd 03 11:52 PM


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