Solved

Deleting Multiple Excel Rows

Posted on 2013-12-13
9
348 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now