?
Solved

Excel: VBA to select dynamic range

Posted on 2015-02-16
6
Medium Priority
?
368 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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 …
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

850 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