Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating more than one excel

Tags:

excel

vbscript

I tried this code to create an excel sheet.

Set ExcelObject = CreateObject("Excel.Application")

ExcelObject.visible = True
ExcelObject.WorkBooks.Add       
ExcelObject.Sheets(1).Cells(1,1).value = "My first excel" 

But i want more than one excel to be generated So i tried this code-

Set ExcelObject = CreateObject("Excel.Application")

For x= 1 to 5
ExcelObject(x).visible = True
ExcelObject(x).WorkBooks.Add 'Adds a workbook to an excel object
x=x+1   
ExcelObject.Sheets(1).Cells(1,1).value = "My first excel"

But it is not working.Please help!

like image 903
Praveenks Avatar asked Jan 19 '26 20:01

Praveenks


1 Answers

Your code has some errors:

  1. You missed Next x

  2. You don't have to increment x since you use For..Next statement. Next x will do that for you.

Working code:

For VB.NET:

Dim ExcelObject() As Object
ExcelObject = New Object(5) {}
For x = 1 To 5
    ExcelObject(x) = CreateObject("Excel.Application")
    ExcelObject(x).visible = True
    ExcelObject(x).WorkBooks.Add() 'Adds a workbook to an excel object
    ExcelObject(x).Sheets(1).Cells(1, 1).value = "My first excel"
Next x

For VBScript:

Dim ExcelObject(5) 
For x = 1 To 5
    Set ExcelObject(x) = CreateObject("Excel.Application")
    ExcelObject(x).visible = True
    ExcelObject(x).WorkBooks.Add() 'Adds a workbook to an excel object
    ExcelObject(x).Sheets(1).Cells(1, 1).value = "My first excel"
Next
like image 150
Andrey Gordeev Avatar answered Jan 22 '26 11:01

Andrey Gordeev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!