How to do something in an VBS script for all worksheets of an Excel file?

  • Thread starter Thread starter Claudia d'Amato
  • Start date Start date
C

Claudia d'Amato

I would like to do something in a *.vbs script and all the operations should be applied
on each worksheet within an Excel file.

How do I do this?

It must be something like:

for i in (1 .. lastworksheetnumber) do
...operations
end


Claudia
 
For i = 1 To Activeworkbook.Worksheets.Count

... operations on Activeworkbook.Worksheets(i)

Next i
 
Claudia said:
I would like to do something in a *.vbs script and all the operations should be applied
on each worksheet within an Excel file.

How do I do this?

It must be something like:

for i in (1 .. lastworksheetnumber) do
...operations
end


Claudia
Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim oExcel : Set oExcel = CreateObject( "Excel.Application" )
Dim sFSpec : sFSpec = oFS.GetAbsolutePathName( "<yourxlsfile" )
Dim oWBook : Set oWBook = oExcel.Workbooks.Open( sFSpec )
Dim oSheet
For Each oSheet In oWBook.WorkSheets
WScript.Echo oSheet.Name
...
Next
oWBook.Close True ' Save SaveAs ...
oExcel.Quit
 
hi
something like this might work...
Sub claudia()
For i = 1 To Worksheets.Count
Worksheets(i).Range("A1").Interior.ColorIndex = 6
'above is for test only
Next i
End Sub

regards
FSt1
 
Hi

Sub sonic()

for x=1 to worksheets.count
worksheets(x).select
'do your stuff
next

end sub


depemding on what you are doing you may not (probably won't) need to select

Mike
 
This should be nice and easy for you.

Dim sh As Worksheet

For Each sh In Worksheets
'your code here
Next sh

Hope this helps!
 
Do you really mean .VBS?

If yes:

=================

Dim xlApp
Dim xlWkbk
Dim xlWks

Set xlApp = CreateObject("Excel.application")
'xlApp.Visible = True
Set xlWkbk = xlApp.Workbooks.Open("C:\book1.xls")

For Each xlWks In xlWkbk.worksheets
xlWks.Columns.AutoFit
Next xlWks

xlWkbk.Close True

xlApp.Quit

Set xlWks = Nothing
Set xlWkbk = Nothing
Set xlApp = Nothing

=================
It's nice to see what's happening when you're debugging. That's why I kept the
..visible line in the pasted code.
 
Hi guy/girls

It seems that there is somebody active in the newsgroups who is trying to
disturp a lot, by creating a special kind of follow up.

Will you be so kind to stop replying to this person, speaking here from the
dotnet language newsgroup.

We are very interested in scripting, excel or whatever.

But when we want that information we visit those news groups.

Thanks in advance

Cor
 
I would like to do something in a *.vbs script and all the operations should
be applied
on each worksheet within an Excel file.

As you can tell me what this has to do with VB for Net, then I agree with
you.

Cor
 
Well seeing as I am answering it in an Excel group, it seems totally
relevant.

Yes, but the person who posted the question, cross posted it to
the .Net group where it is off topic. I think Cor was requesting that
you remove the .Net group from the follow up.

Cheers,

Chris
 
Il giorno Tue, 24 Jun 2008 14:15:04 -0700, =?Utf-8?B?TWlrZSBI?=
Sub sonic()
for x=1 to worksheets.count
worksheets(x).select
'do your stuff
next
end sub

I'm not sure if this works with already opened worksheets. If it's not, you have to open
every single xls before working on it.

Set xl=CreateObject("excel.application")
xl.Visible=True

'xl.Workbooks.Open Filename

for x=1 to xl.worksheets.count
xl.worksheets(x).select
'do your stuff
next


If you want to run an existing xls macro from a vbs:

filePath = "c:\Test.xls"
Set oExcel = CreateObject("Excel.Application")
oExcel.Workbooks.Open(filepath)
oExcel.Run "macro1"
oExcel.ActiveWorkbook.Save
oExcel.ActiveWorkbook.Close
oExcel.Quit
Set oExcel = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top