?
Solved

Deleting Multiple Excel Rows

Posted on 2013-12-13
9
Medium Priority
?
361 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

801 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