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

x
?
Solved

selecting range variable

Posted on 2013-06-05
7
Medium Priority
?
176 Views
Last Modified: 2013-06-19
hey guys,

i've got a function that is accepting a range argument. now i'm trying to select that range cell like range("A1").select action so that i can do offsets, etc and it would keep state by using selection.offset(1,0) etc.

how do i select a cell in excel by using this range variable?

I've tried:

rngStartingCell.select - and it returns an error
range(rngstartingcell).select - and it returns an error
sheets("graphs").rngstartingcell.select - and it returns an error

Function ClearCells(ByRef rngStartingCell As Range) --> this is the function definition

thanks guys in advance!!! = ))
0
Comment
Question by:developingprogrammer
[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
7 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 39221715
How do you assign the range to the variable?
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 39221722
It would best if you post the workbook for us to look at. If not then as ssaqibh noted please advise how the range is assigned as the error is likely there

Michael
0
 

Author Comment

by:developingprogrammer
ID: 39221782
thanks guys, here's the workbook.

kindly search for comment "'this is the line that has a problem" and put a breakpoint on the following line that contains "rngStartingCell.Select", then just run UpdateVMS().

i'm not sure why it can't work.

i've tried this same way of assigning the variable and then selecting the cell through 2 layers of functions and it works. not sure why this one can't work = (
testing.xls
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 85

Accepted Solution

by:
Rory Archibald earned 2000 total points
ID: 39221818
You can't select a cell if it's sheet isn't active, but then you don't need to. Try this version:

Function ClearCells(ByRef rngStartingCell As Range)
   Dim rngEndCell             As Range
   With rngStartingCell
      If .Value = "" Then Exit Function
      'this is the line that has a problem
      If .Offset(, 1).Value <> "" Then
         Set rngEndCell = .End(xlToRight)
      Else
         Set rngEndCell = .MergeArea
      End If
   End With
   With rngEndCell
      If .Offset(1).Value <> "" Then
         Set rngEndCell = .End(xlDown).MergeArea
      End If
   End With
   Range(rngStartingCell, rngEndCell).ClearContents
End Function

Open in new window

0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 39221858
Your problem here is the merged cells. The easiest way to solve this would be to not use the merges

Michael
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 39224468
Sorry another thing, this method has the additional advantage that the workbook cannot be accessed if macros are disabled. In the past I have set up the Unlock page with a warning that macros must be enabled and information for the user on how to enable macros.

Michael
0
 

Author Closing Comment

by:developingprogrammer
ID: 39261310
guys thanks for all your help! it was indeed the sheet not being selected = )
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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,…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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…

715 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