VBA corruption

  • Thread starter Thread starter HSalim[MVP]
  • Start date Start date
H

HSalim[MVP]

Hi,
I was working on some vba code - nothing complicated - adding a workbook,
then adding sheets and so on.
I could not tell why I kept getting errors adding a worksheet.
for example, Workbook.Worksheet.Add would work but .add(After:= I) would
result in an error.
would , eventually abandoned the file and started afresh and found that the
code works flawlessly.
That file was created in Xl2007 and saved as an xl2003 file. It is running
"in compatibility mode."

Is this a common occurrence? Any fixes?

HS
 
I'm surprised that code ever worked!

Your "I" should be a sheet (or worksheet/chart) object


With ActiveWorkbook
..Worksheets.Add after:=.Sheets(.Sheets.Count)
End With

or
Set oSht = ActiveWorkbook.Sheets.Count
ActiveWorkbook.Worksheets.Add after:=oSht

Both the above add a new worksheet after all sheets (incl chart sheets). You
could of course change .Sheets.Count to a valid number, or Sheets to
Worksheets

Regards,
Peter T
 

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