Solved

Excel: VBA to select dynamic range

Posted on 2015-02-16
6
233 Views
Last Modified: 2016-02-10
Hi,

I have a spreadsheet that has a variable range of populated text cells in column B. This range would always start at B7 - and could extend to B200.

I would like to select this variable cell range via VBA, then copy it to cache.

I have a sample spreadsheet attached.


Cheers,
ee-variablerange.xlsx
0
Comment
Question by:dabug80
  • 4
  • 2
6 Comments
 
LVL 11

Accepted Solution

by:
Wilder1626 earned 500 total points
ID: 40613220
Hi

You can try like this:
Dim LR As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
Range("B7:B" & LR).SpecialCells(xlCellTypeConstants, 23).Copy

Open in new window

ee-variablerange-1.xlsm
0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40613245
if you want to copy from B7 to the last cell in the rage, including the empty cells, then take the below code:
Dim Mylastrow As Long
Mylastrow = Range("B:B").Find(What:="*", LookIn:=xlValues, SearchDirection:=xlPrevious).Row
Range("B7:B" & Mylastrow).Copy

Open in new window

All cells in range
If you only want the cells with values:
Dim Mylastrow As Long
Mylastrow = Range("B" & Rows.Count).End(xlUp).Row
Range("B7:B" & Mylastrow).SpecialCells(xlCellTypeConstants, 2).Copy

Open in new window

Cells with values
0
 
LVL 1

Author Comment

by:dabug80
ID: 40613303
Thanks. This is great. At the end of the code, is it possible to deselect the cells, yet still have the copied cells in cache?
0
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.

 
LVL 11

Expert Comment

by:Wilder1626
ID: 40613326
I don't thing it is possible. well from what i know.
0
 
LVL 1

Author Closing Comment

by:dabug80
ID: 40613422
OK Thanks. I found a 'work around' to remove the end range selection. I added a message box, then cleared the copy cache/selection afterwards. Here's the final code.

Dim LR As Long
LR = Range("B" & Rows.Count).End(xlUp).Row
Range("B7:B" & LR).SpecialCells(xlCellTypeConstants, 23).Copy
MsgBox "Range saved. Paste into Outlook."
Application.CutCopyMode = False

Open in new window


Thanks for the help
0
 
LVL 11

Expert Comment

by:Wilder1626
ID: 40614540
I'm glad you found a work around and glad i was able to help.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

9 Experts available now in Live!

Get 1:1 Help Now