Forum Discussion

NisHera's avatar
NisHera
Valued Contributor
11 years ago

Create new excel from data

Hi

I was trying to create an XL using some data in script 

but failed following are my coding please help me correcting it




Sub Main


 Dim fileName, sheetName


 fileName ="C:\N2014_1\TaxCalculation\logs26.xlsx"


 sheetName ="Logs1"


 


 Call WriteLogs(fileName,sheetName)


End Sub


 


Sub WriteLogs(fname,sname)


Dim app,book,sste, row, col


 


Set app = CreateObject("Excel.Application")


Set book = app.Workbooks.Add()


  book.saveAs fname


  


  Set app = Sys.OleObject("Excel.Application")


  Set book = app.Workbooks.Open(fname)


  Set sste = book.Sheets(sname) '------hear it says Subscript out of range ????-----------


 


  app.Application.Visible = True


  app.DisplayAlerts = False


  


  For row=0 to 5


for col = 0 to 4


app.book.sheet.Cells(row,col) =row&","& col


Next


Next


 


   Book.SaveAs fname


  .Book.Close


  


app.Appliaction.Quit


End Sub

1 Reply

  • murugans1011's avatar
    murugans1011
    Regular Contributor
    you are creating a new excel by using 'save as' method....but u r opening non existent sheet ....... by default excel will create a sheet name as 'sheet1'. so try



                sheetName ="sheet1"

            

                function

                Set sste = book.Sheets(sname)


               End function