Solved

Managing Range Selection in Excel Macro

Posted on 2013-12-10
6
716 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 31

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 31

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 31

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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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 31

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

706 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

17 Experts available now in Live!

Get 1:1 Help Now