Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unable to delete rows in excel....?? 1

Status
Not open for further replies.

nimarii

MIS
Jan 26, 2004
213
US
Hello -

I have an access module that needs to edit an excel spreadsheet. I'm trying to delete the first 7 rows of the spreadsheet regardless of whatever data or blanks is in it, but for some reason, it seems to be deleting only the odd rows. (maybe i'm just imagining things?)
here's the code:
Code:
Private Sub Import_Click()
Dim xlapp As Excel.Application
Dim xlwb As Excel.Workbook
Dim sheet As Excel.Worksheet
Set xlapp = GetObject(, "Excel.Application")
Me.PathName.SetFocus
Path = Me.PathName.Text
Set xlwb = xlapp.Workbooks.Open(Path)
Set sheet = xlapp.ActiveWorkbook.Sheets(1)

With xlapp
  For r = 1 To 7 Step 1
      sheet.Rows(r).Delete
  Next r
End With

'some more code...

can anyone see what i'm doing wrong? i'd greatly appreciate any help!
 
The reason is because when you delete a row it moves the data up, use this instead
Code:
For r = 1 To 7 Step 1
      Rows(r / r).Delete
Next r
 
The usual way of doing this is to step backwards through the rows, eg:-

For r = 7 To 1 Step -1
sheet.Rows(r).Delete
Next r

Regards
Ken...........

----------------------------------------------------------------------------
[peace]It's easier to beg forgiveness than ask permission[2thumbsup]

----------------------------------------------------------------------------
 
I guess I would just:
For r = 1 To 7 Step 1
sheet.Rows(1).Delete
Next r

but we all get the same job done.

Tim


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top