Google Answers Logo
View Question
 
Q: excel macro ( Answered,   0 Comments )
Question  
Subject: excel macro
Category: Computers
Asked by: rooknyc-ga
List Price: $7.00
Posted: 02 Jun 2003 14:20 PDT
Expires: 02 Jul 2003 14:20 PDT
Question ID: 212127
I want to select a range in an excel macro using variables so I can
copy it from one sheet to another.

This is what I have now:

Range(B,r1:G,r1).Select

so if r1=3, it would select B3:G3

Clarification of Question by rooknyc-ga on 02 Jun 2003 14:26 PDT
Oh, and what I have does not work.
Answer  
Subject: Re: excel macro
Answered By: maniac-ga on 02 Jun 2003 17:22 PDT
 
Hello Rooknyc,

I will refer to using the Immediate Window in Visual Basic to
illustrate the solution. If you want to follow along, enter Visual
Basic (Tools -> Macro -> Visual Basic Editor and then use View ->
Immediate Window.

There are at least a few different ways to solve this problem. With
your example using Range, you must construct a string with the
appropriate text. Using your example where variable R1 (not cell) has
value 3, you can enter something like...
  print "B"&format(r1)&":G"&format(r1)
in the immediate window to produce the result
  "B3:G3"
This is the correct string to provide to Range. I determined that
"format" was needed using a search in Visual Basic of convert number
to string which refers to the "Str" function. Unfortunately using Str
in place of format produces
  "B 3:G 3"
which has extra spaces we do not need. The "See Also" under Str points
to Format which has the proper explanation and function.

Using this would lead to a solution like...
  R1=3
  S$="B"&format(R1)&":G"&format(R1)
  ActiveSheet.Range(S$).Select
which will work properly for the example you provided.

There are other methods possible. For example, you can use...
 - Cells(row,column) to refer to a single cell. Range requires two
cells, which looks something like
  ActiveSheet.Range(Cells(3,2),Cells(3,8)).Select
to select the same cells as above. Note that Cells uses Row/Column
format and string values to Range use Column/Row format.
 - If the value you need is in a Cell, use .Value of a cell reference
and assign it to a variable for manipulation. For example, if R1 above
was cell R1, you can use...
  Dim R1 As Integer
  R1 = ActiveSheet.Range("R1").Value
and the remainder of the above statements are the same. Note that R1
must be an Integer or you will get something like
  B       5.000000 :G       5.000000
as the value of S$ (assuming cell R1 was 5) which is not a valid cell
reference.

If any of this is unclear, please let me know with a clarification
request so I can provide additional explanations. Good luck with your
work.

  --Maniac

Request for Answer Clarification by rooknyc-ga on 03 Jun 2003 13:31 PDT
'
    
I think I am doing several things wrong.  Can you check?
     
         
         Dim A As Integer
         Dim B As Integer
         Dim C As Integer
         Dim D As Integer
         Dim E As Integer
                 
Dim X As Integer
Dim Y As Integer

Dim Z$ As String


Windows("FS.xls").Activate
 Sheets("data").Select
 
E = Range("E" & 2).Value
'For X = 1 To E

For X = 2 To 3

Windows("FS.xls").Activate
 Sheets("data").Select
 
A = Range("A" & X).Value
B = Range("B" & X).Value
C = Range("C" & X).Value
D = Range("D" & X).Value

For Y = 1 To C
    
    Application.CutCopyMode = False
    Windows("FSMSG.xls").Activate
   
'S$ = "B" & Format(A) & ":G" & Format(A)
   
Z$ = Range(Cells(A, 2), Cells(A, 7))
   
'Range ("B" & Format(B) & ":G" & Format(B))

'Range("B" & A).Select
   Selection.Copy
    Windows("FS.XLS").Activate
    Sheets("Consolidated").Select
        Range("B" & B).Select
        
 ActiveCell.FormulaR1C1 = Z
 
 
 '   ActiveSheet.Paste
    Application.CutCopyMode = False
A = A + 2
B = B + 1

Next Y
Next X


End Sub

Clarification of Answer by maniac-ga on 03 Jun 2003 15:07 PDT
Hello Rooknyc

I don't understand completely what you are trying to do, but a few
things to fix include...
 - Dim Z$ as String is not a valid statement. When I pasted that into
Visual Basic, it is shown in red (invalid statement). You can declare
Z$ (which is a string already), but then you cannot use it as an
object later (assignment of a range). Since you don't use Z$ anywhere
else, I can't say for sure what it is for.
 - In several places you use something like
  Range("B" & A).Select
which is trying to concatenate a string with an integer. Use
  Range("B" & Format(A)).Select
if you want a string "B2" (or whatever number A is) instead.
 - at the end of the loop, you increment A and B, but then at the top
of the loop assign them again. Thus the increment is lost. If you
intend to change the value of the cell that A was set to (e.g.,
Range("A2").Value), you have to use something that references the
cell, not the variable.
Those are the problems I see without understanding what you are trying
to do. To do more, I may need to understand the purpose of the
subroutine and I would then suggest a solution that may be easier to
implement.

Another comment, as I mentioned in the original message, I suggest
trying the steps you are working with in the immediate window, see
what happens and then put them into the subroutine. The other method I
often use in debugging macros is to set a breakpoint (click on the
left side of the module window at the line you want to stop at); a red
circle (or stop sign) appears. When you reach that point in your code,
you can single step and point at variables to determine their value; a
pop up will show the value. Alternatively, you can use the immediate
window to print values, do calculations, etc.
  --Maniac
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy