Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA Selecting range only containing data

Posted on 2016-07-25
12
Medium Priority
?
283 Views
Last Modified: 2016-07-27
HI Guys

Im using this bit of code which I thought would select Column A to FF and go to the last row containing data in this range.

Sub selectrange()
Dim r As Long
r = Range(Selection, Selection.End(xlDown)).Row
Range("A" & Selection.Row, "FF" & r).Select
End Sub

It is only selecting the first row for some reason and Im not sure what the issue is.

Would you be able to help me on this?

The attached example should result in the range A1:FF31 being selected when the code is run but unfortunately, im missing something ..

J
Book1.xlsm
0
Comment
Question by:Jase Alexander
[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
  • 3
  • 3
  • 2
  • +3
12 Comments
 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41727743
Hi,

pls try ( in your code the row is the first row of the range=1)
Sub copyrange()
Dim r As Long
r = Selection.End(xlDown).Row
Range("A" & Selection.Row, "FF" & r).Select
End Sub

Open in new window

Regards
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 41727744
r = Selection.End(xlDown).Row
0
 

Author Comment

by:Jase Alexander
ID: 41727754
HI Guys

Thank you for the quick response

It work great except is there an addition to the code whereby it ALWAYS selects from cell A1 and the subsequent range containing data?

At the moment, if I click in cell d8 and run the code, it only selects the rows and range starting below this location?

If not, no problems I can work with this

Regards
J
0
Independent Software Vendors: 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!

 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 41727755
then try

Sub copyrange()
Dim r As Long
r = Selection.End(xlDown).Row
Range("A1", "FF" & r).Select
End Sub

Open in new window

0
 

Author Comment

by:Jase Alexander
ID: 41727767
HI RG

I tried the A1 change but what happens is if you click in A1 and then run the code, it works perfect.

If you click anywhere else on the sheet and run the code, it selects the WHOLE sheet from cell A1

However, no problems, I can work with this

Thank you for your help

J
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41727791
Are columns A to FF the only used columns?

If so just selecting Used.Range will do the same:

ActiveSheet.UsedRange.Select

Open in new window


Alternatively, determine the last row using LastCell:

LR = ActiveCell.SpecialCells(xlLastCell).Row

Open in new window

0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 41727804
What are you doing with the range once it is selected?

It is often not necessary to use the .Select method with a range; you may be able to do the actions without selecting the range.

Thanks
Rob H
0
 
LVL 52

Assisted Solution

by:Rgonzo1971
Rgonzo1971 earned 500 total points
ID: 41727904
or maybe
Sub copyrange()
ActiveCell.CurrentRegion.Select
End Sub

Open in new window

0
 
LVL 3

Accepted Solution

by:
AL_XResearch earned 1000 total points
ID: 41727956
Try the below (removing the final 'select' and replacing it with your processing code):
Sub selectLastRow()

    Dim wksTargetSheet As Worksheet
    
    Dim rngAllData As Range
    Dim rngLastRow As Range
    
    Set wksTargetSheet = ActiveSheet
    
    Set rngAllData = wksTargetSheet.Range("A1").CurrentRegion
    
    Set rngLastRow = rngAllData.Offset(rngAllData.Rows.Count - 1).Resize(1)
    
    rngLastRow.Select ' Or whatever you want to do with the row
    
End Sub

Open in new window

0
 
LVL 22

Assisted Solution

by:Roy Cox
Roy Cox earned 500 total points
ID: 41728035
CurrentRegion will select all cells in a sheet that contiguous to each other surrounding a specified cell

UsedRange is the area of the sheet that has been 'Used' for any purpose, since it was last saved.

I would think that currentRegion is probably the best choice for you, e.g.

MsgBox Range("A1").CurrentRegion.address
MsgBox ActiveSheet.UsedRange.address

Open in new window

0
 

Author Closing Comment

by:Jase Alexander
ID: 41731909
Cannot thank you guys enough

All suggestions were perfect in their own merit

I have one happy finance team !
0
 
LVL 22

Expert Comment

by:Roy Cox
ID: 41732380
Pleased to help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
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 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…
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 …

705 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