Button and VBA Viewer running differently
Hello all, I'm new to VBA so I'm not sure how to fix this.
I have a sub where the first sheet in my workbook needs to be saved in a new workbook as values (and some number formatting) but in an .xlsx format. The way I've set it up, the code runs perfectly when I have the editor open and I run it through there, but when I assign the macro to a button and try and run it from there, it saves the workbook the code is in with a new name. I've looked online and can't see any way to stop this from happening.
I'll add the sub below
Sub Upload_Sheet()
Dim NewWrk As Workbook
Dim ThisWb As Workbook
Dim ThisWbName As String
Dim Amount As Currency
Dim InputterName As String
Set ThisWb = ThisWorkbook
ThisWbName = "Agency Upload"
'Amount is currency
Amount = ThisWb.Sheets("Upload").Range("S9").Value
InputterName = InputBox("Inputted by:")
Set NewWrk = Workbooks.Add
ThisWb.Sheets("Upload").Range("A1:S99999").Copy
NewWrk.Sheets("Sheet1").Range("A1:S99999").PasteSpecial Paste:=xlPasteValues, Transpose:=False
ThisWb.Sheets("Upload").Range("E1:E99999").Copy
NewWrk.Sheets("Sheet1").Range("E1:E99999").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
ThisWb.Sheets("Upload").Range("I1:I99999").Copy
NewWrk.Sheets("Sheet1").Range("I1:I99999").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Transpose:=False
NewWrk.Activate
NewWrk.Application.Dialogs(xlDialogSaveAs).Show (ThisWbName & " £" & Amount & " " & InputterName & ".xlsx")
NewWrk.Close
MsgBox "Upload sheet saved"
End Sub
Posted Oct 9, 2023, 10:52 AM UTC
14