[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Deleting Multiple Excel Rows

Posted on 2013-12-13
9
Medium Priority
?
365 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

650 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