Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing values from one variable to another 1

Status
Not open for further replies.

adgesap

Technical User
Mar 11, 2005
10
GB
I playing with some code where I want to set up a list of values, that can then be picked up in a loop. The loop works fine but what's being written to the spreadsheet is wrong.
The code:
Code:
Sub Macro3()
'
    Dim Franchise1 As String
    Dim Franchise2 As String
    Dim Franchise3 As String
    Franchise1 = "Audi"
    Franchise2 = "BMW"
    Franchise3 = "Ford"
    Dim Franchise As String
    Dim Number As Integer
    Number = 1
Do
    Dim NumberString As String
    NumberString = Number
    Franchise = "Franchise" + NumberString
    Range("A" + NumberString).Select
    ActiveCell.FormulaR1C1 = Franchise
    Number = Number + 1
Loop Until Number = 4
End Sub
What I was expecting was the car names to appear, but what I'm getting is "Franchise1" etc. Is there a way for the code to recognize that when "Franchise" = Franchise1 it should print "Audi"?
 
Try arrays
Code:
Sub Macro3()
    Dim NumberString As String
    Dim FranchiseA() As String
    Dim Number       As Integer
    Redim FranchiseA(2)
    FranchiseA(0) = "Audi"
    FranchiseA(1) = "BMW"
    FranchiseA(2) = "Ford"
    
    For Number = 0 to Ubound(FranchiseA)
        NumberString = Number + 1
        Range("A" + NumberString).Select
        ActiveCell.FormulaR1C1 = FranchiseA(Number)
    Next
End Sub
 



Code:
Sub Macro3()
'
    Dim Franchise(1 To 3) As String
    Dim Number As Integer
    Franchise(1) = "Audi"
    Franchise(2) = "BMW"
    Franchise(3) = "Ford"
    
    For Number = 1 To UBound(Franchise)
        Range("A" & Number).Value = Franchise(Number)
    Next
End Sub


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top