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!
Your code has some errors:
You missed Next x
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With