Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the result of an input box in another bit of my code?

Tags:

excel

vba

So I am very new to VBA and coding and have been playing around writing very simple subs. The sub takes a sheet from a workbook, copies into a new workbook and then saves it. the problem is that I want to be able to specify the name it uses when it saves the document. I am using an input box, I just can't seem to get the code to accept the result. This is my code

Sub CC_Export()
Dim name As String
Dim Wb As Workbook

Set Wb = Workbooks("workbook1")
    Wb.Sheets("Sheet1").Copy
    name = InputBox("Enter Name of New Workbook", "New Workbook")
    ActiveWorkbook.SaveAs "name.xlsx", FileFormat:=51

End Sub
like image 630
Robert Sorgedrager Avatar asked Jan 27 '26 04:01

Robert Sorgedrager


1 Answers

Your code is using the literal text "name.xlsx". To use the variable you need to concatenate its value into your text string

ActiveWorkbook.SaveAs name & ".xlsx", FileFormat:=51
like image 173
Rory Avatar answered Feb 01 '26 09:02

Rory



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!