Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 15th, 2013, 10:18 AM
martin28 martin28 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 martin28 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 32 m 20 sec
Reputation Power: 0
VB Script - Macro on multiple workshets, save to csv

Good Morning,

I am attempting to run what I thought what was some very basic code, and am running into a wall.

In a nutshell. Have a macro in one workbook. From that workbook I want to loop through worksheets in other workbooks (all in the same directory) then dump the contents of those worksheets into individual .csv files. The progam runs fine until it goes to save the .csv files.

The code is below. Any help would be greatly appreciated.

Option Explicit
Sub Open_Dir()

Dim FSO, FLD, FIL, XL
Option Explicit
Sub Open_Dir()

Dim FSO, FLD, FIL, XL
Dim strFolder, sFileName, sFull_File_Path

Dim newWks As Worksheet
Dim wks As Worksheet

'############ Set up the folder
strFolder = "C:\Backup2\"

'Create the filesystem object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FLD = FSO.GetFolder(strFolder)

' 'loop through the folder and get the file names
For Each FIL In FLD.Files

'Get the file name
sFileName = FIL.Name

'String together the path and file name
sFull_File_Path = strFolder & sFileName

'Set up the Excel file
Set XL = CreateObject("Excel.application")
XL.Application.Workbooks.Open sFull_File_Path
XL.Application.Visible = False

'Do your thing here
For Each wks In XL.ActiveWorkbook.Worksheets

Debug.Print wks.Name
'wks.Copy 'to a new workbook
'Set newWks = ActiveSheet
With wks
'Code stops here
.SaveAs FileName:=strFolder & wks.Name, FileFormat:=xlCSV
.Parent.Close savechanges:=False
End With
Next wks

MsgBox "done with: " & ActiveWorkbook.Name

Debug.Print sFileName


'End the Excel file
Set XL = Nothing

'Get the next file
Next

End Sub

Reply With Quote
  #2  
Old February 13th, 2013, 08:01 AM
GGriggs GGriggs is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 18 GGriggs User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 45 m 43 sec
Reputation Power: 0
You are trying to save a SHEET using SaveAs whereas you can only save a WORKBOOK. Change to thus:


Debug.Print wks.Name
'wks.Copy 'to a new workbook
'Set newWks = ActiveSheet
With wks
.Copy ' Copies the sheet to a newworkbook
'Code stops here
Activeworkbook.SaveAs FileName:=strFolder & wks.Name, FileFormat:=xlCSV
Activeworkbook.Close savechanges:=False
End With
Next wks

Just a thought, your "Save as" is only based on the sheet name, if you have e.g. a "Sheet1" in several of your workbooks then they will eachoverwrite each other except the last one

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > VB Script - Macro on multiple workshets, save to csv

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap