# Using string variables in a sequence formula



## vegas808 (Dec 17, 2022)

Hello! First post

This following line works.

Range("A1").Formula2R1C1 = "=TEXT(SEQUENCE(3),""00"")"

01
02
03

I'd like to use string variables in the formula so that my results would be ...

HELLO-WORLD-01
HELLO-WORLD-02
HELLO-WORLD-03


----------



## Fluff (Dec 17, 2022)

Hi & welcome to MrExcel.
How about
	
	
	
	
	
	



```
Range("A1").Formula2 = "=""Hello-world-""&TEXT(SEQUENCE(3),""00"")"
```


----------



## vegas808 (Dec 17, 2022)

Fluff said:


> Hi & welcome to MrExcel.
> How about
> 
> 
> ...


Hi Fluff,

I should've posted that I used that too and it works but I wanted to use 2 string variables in place of "HELLO" and "WORLD".   

string1, string2 as string


----------



## Fluff (Dec 17, 2022)

In that case please post the code with the variables.


----------



## vegas808 (Dec 17, 2022)

This formula doesn't work. 

    Dim string1 As String: string1 = "HELLO"
    Dim string2 As String: string2 = "WORLD"

    Range("A1").Formula2 = "=string1 & ""-"" & string2 & ""-"" &  TEXT(SEQUENCE(3),""00"")"


----------



## vegas808 (Dec 17, 2022)

vegas808 said:


> This formula doesn't work.
> 
> Dim string1 As String: string1 = "HELLO"
> Dim string2 As String: string2 = "WORLD"
> ...


Been playing around with this and did a work-around. Went for 20 lines this time. Can it be improved? I'm so mentally drained. 

Sub my_test()

    Dim string1 As String: string1 = "HELLO"
    Dim string2 As String: string2 = "WORLD"

    Application.ScreenUpdating = False

    Range("B1").Value = string1 & "-" & string2 & "-"
    Range("A1").Formula2 = "=CONCAT(B1)&TEXT(SEQUENCE(20),""00"")"

    With Range("A1:A20")
          .Copy
          .PasteSpecial Paste:=xlPasteValues
    End With
    Application.CutCopyMode = False

    Range("B1").ClearContents
    Range("A1").Select

    Application.ScreenUpdating = True

End Sub


----------



## kvsrinivasamurthy (Dec 17, 2022)

Try this

```
Sub my_test()

Dim string1 As String: string1 = "HELLO"
Dim string2 As String: string2 = "WORLD"
Dim A As String

Application.ScreenUpdating = False
A = string1 & "-" & string2 & "-"
Range("A1:A20").Formula = "=""" & A & """&Text(rows($A$1:$A1),""00"")"

Application.ScreenUpdating = True

End Sub
```


----------



## vegas808 (Dec 18, 2022)

kvsrinivasamurthy said:


> Try this
> 
> ```
> Sub my_test()
> ...


Thank you kvsrinivasamurthy!!!!!!

Your code works just as well, if not a little quicker.  Less lines too.  You actually helped me on using a string in the front of the formula, after the "=".  Why didn't I know about doing this    =""" & A & """  ?   Thanks again!


----------



## jdellasala (Dec 18, 2022)

Oy! VBA? 
Book1A1Hello World -012Hello World -023Hello World -034Hello World -045Hello World -056Hello World -067Hello World -078Hello World -089Hello World -0910Hello World -10Sheet1Cell FormulasRangeFormulaA1:A10A1="Hello World -"&TEXT(SEQUENCE(10),"0#")Dynamic array formulas.Why would anyone use VBA instead?


----------



## vegas808 (Dec 18, 2022)

jdellasala said:


> Oy! VBA?
> Book1A1Hello World -012Hello World -023Hello World -034Hello World -045Hello World -056Hello World -067Hello World -078Hello World -089Hello World -0910Hello World -10Sheet1Cell FormulasRangeFormulaA1:A10A1="Hello World -"&TEXT(SEQUENCE(10),"0#")Dynamic array formulas.Why would anyone use VBA instead?


jdellasala,

In a simple explanation, automation.  In the big picture, I wanted to use this sample code to apply to generating multiple items in a Column.  It doesn't have to be just starting at "A1".  The strings, qty, row, the start sequence index would change.


----------



## vegas808 (Dec 17, 2022)

Hello! First post

This following line works.

Range("A1").Formula2R1C1 = "=TEXT(SEQUENCE(3),""00"")"

01
02
03

I'd like to use string variables in the formula so that my results would be ...

HELLO-WORLD-01
HELLO-WORLD-02
HELLO-WORLD-03


----------



## jdellasala (Dec 18, 2022)

vegas808 said:


> jdellasala,
> 
> In a simple explanation, automation.  In the big picture, I wanted to use this sample code to apply to generating multiple items in a Column.  It doesn't have to be just starting at "A1".  The strings, qty, row, the start sequence index would change.


That's what Cell References are for. The hard coded 10 parameter can be a value in a cell, as can any or all of the text which could be more as well.
Whatever.


----------



## vegas808 (Dec 18, 2022)

jdellasala said:


> That's what Cell References are for. The hard coded 10 parameter can be a value in a cell, as can any or all of the text which could be more as well.
> Whatever.


"Whatever." Everytime someone ends a convo with "whatever", it's usually relayed as sarcasm to me.  smh.  If so, "thanks" for the warm welcome on my first thread.

I'm sure there are many ways to go about this and I'm here to learn about those many options and make a decision on what's best for my program.  In fact, your cell formula is almost exactly as it is in my first post.


----------

