Run time error - Adding Workshet

  • Thread starter Ashish Kanoongo
  • Start date
A

Ashish Kanoongo

I am using following procedure for adding worksheets in workbook, first time following function it works perfect and all the tab created alphabatically with the different name(abc, mno, stv, xys), but second time it give me following error

Method 'Move' of Object '_worksheet' failed;

If I comment following line, it wont give me error, it run perfectly except workbook tab name add in reverse chronolofical order like (xys, stv, mno, abc)

ExcelSht.Move before:=Worksheets(Worksheets.Count)

Lte me know whats wong I am doing? Or is there any way that I can sort worksheet alphabatically.

*--------------------------------------------------------
Dim ExcelApp As New Excel.Application
Dim ExcelSht As New Excel.Worksheet
Dim ExcelWkb As New Excel.Workbook
Dim rsManagersDis As New ADODB.Recordset

Set ExcelApp = CreateObject("Excel.Application")
MyExcel = True
ExcelApp.Visible = True

Set ExcelWkb = ExcelApp.Workbooks.Add

rsManagersDist.ActiveConnection = CurrentProject.Connection
rsManagersDist.CursorLocation = adUseClient
rsManagersDist.Open "Select distinct [Manager Name] rom tmpPMExcel order by [Manager Name] ) "

Do Until rsManagersDist.EOF
j2 = rsManagersDist("expr1")
Set ExcelSht = ExcelWkb.Worksheets.Add
ExcelWkb.Worksheets("Sheet1").Visible = xlSheetVeryHidden
ExcelSht.Visible = xlSheetVisible
ExcelSht.Move before:=Worksheets(Worksheets.Count)
ExcelSht.name = j2
ExcelSht.PageSetup.PrintGridlines = True
ExcelSht.PageSetup.Orientation = xlPortrait
rsManagersDist.MoveNext
Loop
*--------------------------------------------------------
 

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

Top