Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Excel: VBA to select dynamic range

Posted on 2015-02-16
6
276 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
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.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
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.

829 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