Solved

Managing Range Selection in Excel Macro

Posted on 2013-12-10
6
782 Views
Last Modified: 2013-12-10
I have an Excel workbook with a bunch of tabs that refresh from a connected Access database. The refreshed data fills columns starting with A and then out to the right for between 5 and 12 columns. For a special purpose, I have formulas further out to the right which perform conversions on some of the refreshed data and I want to have a Macro that does a couple of things:

1: Deletes all of the formula rows below the range of the refreshed data. (So, if the refreshed data fills up down to row 10, for example, I want to delete all rows from 11 on down.)

2: "Copy/Paste/Values" of the formula data in on top of the refreshed columns. (So the data calculated by the formulas replaces the data brought in from Access.)

I tried to record a macro to do this, and most of it works OK. My problem is that to identify the first "blank" row in the refreshed data, I have been moving the Excel cursor to the top of one of the refresh columns (one I KNOW has data for all rows) and then using the "End/Down" to get to the bottom populated row. Then, to get to the NEXT row down, I hit the down arrow key, and this is the problem. The Macro does not record "Down One Row." It records the explicit selection of the cell I moved into - e.g. "H11". This means the macro will only work for this specific import and if a new refresh pulls in a different number of rows, the Macro is useless.

How can I replace the explicit selection of the cell with something generic that identifies the "Next Row Down" so that my delete command will remove the desired rows regardless of how the number of rows varies from refresh to refresh?

Thanks.
0
Comment
Question by:Buck_Beasom
  • 4
6 Comments
 
LVL 32

Accepted Solution

by:
Rob Henson earned 500 total points
ID: 39708474
You can use the Application.Offset function:

Application.Offset(RowOffset, ColumnOffset).Range(Start:Finish).Select

So to go down one row and select a single cell:

Application.Offset(1,0).Select

To go down one row and across one column:

Application.Offset(1,1).Select

To go down one row and select a range 5 columns wide and 3 rows deep:

Application.Offset(1,0).Range("A1:E3").Select

Thanks
Rob H
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 39708478
Also, when recording a macro, there is an option to record for Relative cell references rather than Absolute. Activating this would have have used the Offset method for you.

Thanks
Rob H
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 39708491
There are also ways and means of not physically selecting the cell to ascertain the extent of the data but I don't recall the syntax using the "Rows.Count" property. For the first blank row it would be "Rows.Count +1". This also gets round the need for the data in the column to be continuous, ie scenarios with non-continuous data where the End/Down wouldn't work.

Thanks
Rob
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 43

Expert Comment

by:Saqib Husain, Syed
ID: 39708589
What I think you are looking for is;

if your data column is H then

range("H1").end(xldown).offset(1)

will give the first blank row below the data if H1 and H2 are not blank.
0
 
LVL 32

Expert Comment

by:Rob Henson
ID: 39708706
I assume therefore that:

Range("H1048576").end(xlup).offset(1)

Would give same result but allow for scenario where column H may not be continuous.
0
 

Author Closing Comment

by:Buck_Beasom
ID: 39708760
All your comments were outstanding and very helpful.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

777 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