Solved

Deleting Multiple Excel Rows

Posted on 2013-12-13
9
351 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
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…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

803 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