Solved

Excel: VBA to select dynamic range

Posted on 2015-02-16
6
290 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
[X]
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
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
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.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

726 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