+ Reply to Thread
Results 1 to 5 of 5

selecting a range of cells

  1. #1
    Registered User
    Join Date
    03-22-2005
    Posts
    10

    Question selecting a range of cells

    I have a problem in selecting several ranges of cells. What i wish to do is to look at some data and identify where a certain cell occurs. In this case it contains the text "Calshot". I then want to select this cell and the 8 cells above it. The macro searches for all of these ranges ( down one column). Once selected i wish to copy and paste to another sheet. However, I can't seem to get the macro to select all the ranges of cells.

    So far I have this:

    Sub copydata()

    Do
    If ActiveCell = ("Calshot") Then
    Range("Calshot").Offset(-8, 0).Select
    With Selection.Interior
    .ColorIndex = 35
    .Pattern = xlSolid
    End With
    End If


    ActiveCell.Offset(1, 0).Select
    Loop Until IsEmpty(ActiveCell.Offset(1, 0))

    End Sub

    which doesn't work. The problem lies with the 'Range' line of code. If i remove this the macro runs and selects every cell containing the word "Calshot". I just can't select a range of cells based upon this one cell which appears at irregular points down the column.

    So far I'm just experimenting so I'm filling the cells with colour to check it's working. copying and pasting is not a problem.

    Any help would be much appreciated.

    Luke

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Luke,

    You are right about the range. You are trying to use the string value "Calshot" as an address. This will only work if you have a named range = "Calshot". Here is what to change...

    Is:
    Range("Calshot").Offset(-8, 0).Select


    Change To:
    ActiveCell.Offset(-8, 0).Select

    Hope this clarifies things a bit,
    Leith Ross

  3. #3
    Registered User
    Join Date
    03-22-2005
    Posts
    10
    Quote Originally Posted by Leith Ross
    Hello Luke,

    You are right about the range. You are trying to use the string value "Calshot" as an address. This will only work if you have a named range = "Calshot". Here is what to change...

    Is:
    Range("Calshot").Offset(-8, 0).Select


    Change To:
    ActiveCell.Offset(-8, 0).Select

    Hope this clarifies things a bit,
    Leith Ross
    I tried this and the code now only selects the cell 8 rows above the cell containing the text "Calshot". how can i select a range 8 rows high based upon a single cell that i have told the code to select?

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Luke,

    To select 8 rows up from a cell selected by code, you must have the Range address for the single cell or multiple cells in a variable somewhere, like input from an InputBox, TextBox, ComboBox, or another worksheet dedicated to this Range.

    For this example, let's say the user types the cell address into TextBox1.

    Dim Rng As Range

    TextBox1.Text = "A10"
    Rng = ActiveSheet.Range(TextBox1.Text)

    Rng.Offset(-8, 0).Select


    This will select 8 rows above the cell address typed in by the user.

    Hope this answers your question,
    Leith Ross

  5. #5
    Registered User
    Join Date
    09-22-2004
    Posts
    35
    Take a look at my last post. Someone suggested using a selection rather than a Range object where your initial selection is a specific cell(which you can substitute variables in for any way) and then a similar coordinate command as the "offset" command. Works pretty good for me. Hope you find something that works.


    Cells(1, 7).Select
    Selection.Resize(x - 1, 6).Select
    Last edited by skrimpy; 03-22-2005 at 02:04 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1