Link to home
Start Free TrialLog in
Avatar of dma70
dma70Flag for United States of America

asked on

How to soft code a cell location in a Range.Select statement in visual basic?

I have a visual basic module with the statement " Range(target).select".   I have the variable target set = cell N13"

target = "n13"

Now I want to make the cell (ie the value of the string assigned to target) variable.  that is I want to put the range.select statement in a loop where I vary the location of the "target" cell.   How do I accomplish this?
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

First you shouldn't use target as the variable name since Target is an Excel reserved word and it may get confused.

As to your question, do you mean something like this?

MyTarget = Selection.Range
Avatar of Norie
Norie

How do you want to loop?

By the way, Target is not a reserved wore but it can be unwise to use it under certain circumstances.
Avatar of dma70

ASKER

I just want the loop to progress from a starting row to an ending row and softcode the argument for the Range(Location).select statement.

So for example I want to loop thru rows 14 thru row 17 in column 10 (K).   I need to construct the 4 cell locations K14, k15, k16 and k17 without having to list them explicitly, as for example (Location="K14", then Location = "K15"....)
You don't need to 'construct' the cell locations to do that, there are various ways you could handle it.

Here's one.
For Each Target In Range("K14:K17")

Next Target

Open in new window

Avatar of dma70

ASKER

is there a way to use the column number instead of the "K".  What I am really looking for is the ability to use variable row numbers and column numbers.  Also does your example do the same think as "selecting" the Range {ie equivalent to Range(xxx).select}
Using the column number shouldn't be a problem.
For Each Target in Cells(14,10).Resize(4)

Next Target

Open in new window



However, I don't know why you want to use Select

In Excel VBA there is hardly ever (never?) a need to select.

What are you actually going to do with the cells you want to refer to?
Avatar of dma70

ASKER

I will admit I am probably not using it correctly -- I took the code from a record macro script and instead of repeating the series of commands explicitly I want to put them in a loop where I soft code the cell location.
 
Here is the code script:

Source = "m13"
    target = "n13"
 
    Sheets("output").Select
    Range(Source).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("sheet1").Select
    Range("BP10").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("BQ25").Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("output").Select
    Range(target).Select
 
   
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
   


I want to be able to vary the variable "source" and "target"  for many cells in a the loop but want to reference row and column numbers (R1C1) rather than explicit cell locations.
What, in words, do you want to do?

Obviously you want to copy from one sheet to another but it's not really clear which sheet you want to copy from and which sheet is the destination, or which cells/ranges are involved.

In the (recorded?) code you just posted it appears you are copying M13 from 'output' to BP10 on 'sheet1', then you are copying BQ25 on 'sheet1' to N13 on 'output'.

Or something along those lines.
ASKER CERTIFIED SOLUTION
Avatar of dma70
dma70
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.