PintaPinta
New Member
- Joined
- Jun 13, 2019
- Messages
- 7
I am new to macros and VBA. I built a macro by scraping a few bits of code I found online so this may look a bit messy but it is very close to working for me.
My macro basically takes a column in excel and pastes it into a new workbook in order to save it as a .msg file (opens in notepad as the text that is in the columns. This file it produces is then used in a handheld inkjet printer.
I have it almost working but the folder that it saves this file to needs to be able to be changed regularly by the user. The way i tried to do this is by having the path refer to a cell in the spreadsheet that has the path necessary. My issue is in the SaveAs part of the macro.
I am trying to save to the following folder
P:\users\R&DGROUP\Projects\704 CalTrain Thermal and Acoustical Insulation\4. Drawings\Wagon AB\Printer Programs\AB3\MSGGROUP
With the file name: AB3-19
But instead it saves to
P:\users\R&DGROUP\Projects\704 CalTrain Thermal and Acoustical Insulation\4. Drawings\Wagon AB\Printer Programs\AB3
With the Filename: MSGGROUPAB3-19.msg
In the code below, in green has a few more details about the file path.
Sub Macro12()
'
' Macro3 Macro
'
'
Range("O1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-9]"
Range("O1").Select
Selection.AutoFill Destination:=Range("O1:O191"), Type:=xlFillDefault
Range("O1:O191").Select
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.Delete Shift:=xlUp
'
' Macro4 Macro
'
'
Columns("O:O").Select
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
Selection.Copy
'
' SaveAs Macro
'
'
Dim Filename1 As String
Dim Folder1 As String
Filename1 = Range("L2").Text
Folder1 = Range("L3").Text ' The folder1 cell looks like this "\Wagon AB\Printer Programs\AB3\MSGGROUP" Where the AB and AB3 are variables that are set by the person
' because depending on the work the output folder will change so one time it will go by the path about or else it could go on a path like this "\Wagon E\Printer Programs\E3\MSGGROUP". The cell looks like this =""&N5&" "&N6&""&"Printer Programs"&""&N2&""&"MSGGROUP"
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ChDir "P:\users\R&DGROUP\Projects\704 CalTrain Thermal and Acoustical Insulation\4. Drawings"
ActiveWorkbook.SaveAs Filename:="P:\users\R&DGROUP\Projects\704 CalTrain Thermal and Acoustical Insulation\4. Drawings" & Folder1 & Filename1 & ".msg" _
_
, FileFormat:=xlText, CreateBackup:=False
ActiveWindow.Close
End Sub
My macro basically takes a column in excel and pastes it into a new workbook in order to save it as a .msg file (opens in notepad as the text that is in the columns. This file it produces is then used in a handheld inkjet printer.
I have it almost working but the folder that it saves this file to needs to be able to be changed regularly by the user. The way i tried to do this is by having the path refer to a cell in the spreadsheet that has the path necessary. My issue is in the SaveAs part of the macro.
I am trying to save to the following folder
P:\users\R&DGROUP\Projects\704 CalTrain Thermal and Acoustical Insulation\4. Drawings\Wagon AB\Printer Programs\AB3\MSGGROUP
With the file name: AB3-19
But instead it saves to
P:\users\R&DGROUP\Projects\704 CalTrain Thermal and Acoustical Insulation\4. Drawings\Wagon AB\Printer Programs\AB3
With the Filename: MSGGROUPAB3-19.msg
In the code below, in green has a few more details about the file path.
Sub Macro12()
'
' Macro3 Macro
'
'
Range("O1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-9]"
Range("O1").Select
Selection.AutoFill Destination:=Range("O1:O191"), Type:=xlFillDefault
Range("O1:O191").Select
Selection.SpecialCells(xlCellTypeFormulas, 1).Select
Selection.Delete Shift:=xlUp
'
' Macro4 Macro
'
'
Columns("O:O").Select
Selection.SpecialCells(xlCellTypeFormulas, 23).Select
Selection.Copy
'
' SaveAs Macro
'
'
Dim Filename1 As String
Dim Folder1 As String
Filename1 = Range("L2").Text
Folder1 = Range("L3").Text ' The folder1 cell looks like this "\Wagon AB\Printer Programs\AB3\MSGGROUP" Where the AB and AB3 are variables that are set by the person
' because depending on the work the output folder will change so one time it will go by the path about or else it could go on a path like this "\Wagon E\Printer Programs\E3\MSGGROUP". The cell looks like this =""&N5&" "&N6&""&"Printer Programs"&""&N2&""&"MSGGROUP"
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ChDir "P:\users\R&DGROUP\Projects\704 CalTrain Thermal and Acoustical Insulation\4. Drawings"
ActiveWorkbook.SaveAs Filename:="P:\users\R&DGROUP\Projects\704 CalTrain Thermal and Acoustical Insulation\4. Drawings" & Folder1 & Filename1 & ".msg" _
_
, FileFormat:=xlText, CreateBackup:=False
ActiveWindow.Close
End Sub