Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Deleting Multiple Excel Rows

Posted on 2013-12-13
9
352 Views
Last Modified: 2013-12-13
I've been using this line of code:

objExcel.Rows(lngRow).Delete -4162

to delete a single row in an Excel sheet from Access VBA. I've tried creating a variable called strClearRange and populating it with, say "5:10000" to delete all the rows between 5 and 10,000 but it doesn't seem to get them all. It strikes me as weird that the command above used a long integer as its argument but everything I find about deleting multiple rows seems to use a string.

If I have two long integers with the beginning and ending rows of the range I want to delete, what is the syntax. (Mind you, I want to delete the entire rows and not just their contents, as some of the rows in the delete range may have shaded cells and I want to get rid of those, too.)

Thanks.
0
Comment
Question by:Buck_Beasom
  • 5
  • 2
  • 2
9 Comments
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39716596
Have you tried this? :)
Range("A2:A100").EntireRow.Delete shift:=xlup

Open in new window

0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39716603
If you have the start and end row numbers as ints you can use this:
Range("A" & rowStart & ":A" & rowEnd).EntireRow.Delete shift:=xlup

Open in new window

0
 
LVL 17

Expert Comment

by:JezWalters
ID: 39716613
Or you could try this:

Dim appExcel As Excel.Application
Dim lngEndRow As Long
Dim lngStartRow As Long
Dim wkbWorkBook As Excel.Workbook
Dim wksWorksheet As Excel.Worksheet

' Open spreadsheet
Set appExcel = CreateObject("Excel.Application")
Set wkbWorkBook = appExcel.Workbooks.Open("C:\Temp\YourSpreadsheet.xlsx")  ' Change to your file name!
Set wksWorksheet = wkbWorkBook.Worksheets("YourWorksheet")                 ' Change to your worksheet name!

' Delete rows
lngStartRow = 5  ' Change to your start row!
lngEndRow = 10   ' Change to your end row!
wksWorksheet.Range(lngStartRow & ":" & lngEndRow).Delete xlShiftUp

' Close spreadsheet
wkbWorkBook.Close True  ' Save changes
Set wkbWorkBook = Nothing
Set wksWorksheet = Nothing
appExcel.Quit
Set appExcel = Nothing

Open in new window

0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39716633
...apparently my second example can even be simplified:
Range(rowStart & ":" & rowEnd).Delete shift:=xlup

Open in new window

0
 

Author Comment

by:Buck_Beasom
ID: 39716670
Angelp1ay: Two questions.

I am using Late Binding, so I think I have to use the -4162 instead of "shift:=xlup. Right?

Second: I am assuming that "rowStart" "rowEnd" are string values converted from the long integers I am using to identify rows. (I think this is also the case for the code proposed by JezWaters.) Is this correct?
0
 
LVL 17

Accepted Solution

by:
JezWalters earned 500 total points
ID: 39716711
The start/end values are indeed declared as Long variables.

Here's a late binding version of my code:

Const XLSHIFT_UP As Long = -4162

Dim appExcel As Object      ' Excel.Application
Dim lngEndRow As Long
Dim lngStartRow As Long
Dim wkbWorkBook As Object   ' Excel.Workbook
Dim wksWorksheet As Object  ' Excel.Worksheet

' Open spreadsheet
Set appExcel = CreateObject("Excel.Application")
Set wkbWorkBook = appExcel.Workbooks.Open("C:\Temp\YourSpreadsheet.xlsx")  ' Change to your file name!
Set wksWorksheet = wkbWorkBook.Worksheets("YourWorksheet")                 ' Change to your worksheet name!

' Delete rows
lngStartRow = 5  ' Change to your start row!
lngEndRow = 10   ' Change to your end row!
wksWorksheet.Range(lngStartRow & ":" & lngEndRow).Delete XLSHIFT_UP

' Close spreadsheet
wkbWorkBook.Close True  ' Save changes
Set wkbWorkBook = Nothing
Set wksWorksheet = Nothing
appExcel.Quit
Set appExcel = Nothing

Open in new window

0
 

Author Closing Comment

by:Buck_Beasom
ID: 39716859
I did have to convert the longs to strings, but the key piece I was missing was specifying the Worksheet BEFORE the range, which your code cleared up for me.

Thanks.
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39717044
I am using Late Binding, so I think I have to use the -4162 instead of "shift:=xlup. Right?
Not something I know much about. Seems the 2 of you have it solved though :)

Second: I am assuming that "rowStart" "rowEnd" are string values converted from the long integers I am using to identify rows. (I think this is also the case for the code proposed by JezWaters.) Is this correct?
They were actually the integers directly in mine. They were automatically converted to strings during the application of the & operator.
0
 
LVL 11

Expert Comment

by:Angelp1ay
ID: 39717046
Anyway, problem solved and I have something new to lookup! Excel VBA Late Binding!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question