Hello! I am building a process where upon clicking a rectangular shape, several steps will happen.
1. A new file will generate from one tab in my active file and will save that tab as a text file into the main folder location as well as a copy to sharepoint.
2. Then, it goes back to the original file, saves the original, and saves a copy to the sharepoint.
The step before this is a "master" file, where a user is able to generate up to 4 files, and each of those 4 generated files then independently run this code, so I have this code tied to the worksheet as I was not able to get it to work from a module. The process works great when only one file is generated, but keeps crashing upon closing when more than one is open and I am not sure if it is not properly finding/reactivating my original workbook or if I have something else incorrect in my code, but any help or suggestions would be greatly appreciated!
Sub savetxt()
Dim SheetComp As String, ws As Worksheet, ws1 As Worksheet, NewFileName As String, NFN As String, SPFN As String, ANFileName As String, ANFN As String
Application.ScreenUpdating = False
ActiveSheet.Shapes.Range(Array("Rectangle 2")).Select
Selection.OnAction = "wsCover.savetxt"
Set ws = ActiveWorkbook.Sheets("COVER")
NewFileName = ActiveWorkbook.Sheets("COVER").Range("B1").Value
ANFileName = ActiveWorkbook.Sheets("COVER").Range("A1").Value
If (ActiveWorkbook.Sheets("QuantStudio 12K Flex_export").Range("A1") = "" Or ActiveWorkbook.Sheets("QuantStudio 12K Flex_export").Range("B1") <> ws.Range("A2")) Then
MsgBox "Start with the green button. Remember to paste QS data to tab!"
Exit Sub
End If
If MsgBox("Are you sure you completed QC Checks?", vbYesNo) = vbNo Then Exit Sub
If ActiveWorkbook.Sheets("COVER").Range("A4").Value = "LAX1" Then
NFN = "M:\OPEN ARRAY\EXPORTS\"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "ATL1-HULK" Then
NFN = "K:\OPEN ARRAY\EXPORTS\"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "ATL1-THOR" Then
NFN = "K:\OPEN ARRAY\EXPORTS\"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "SDF1" Then
NFN = "X:\OPEN ARRAY\EXPORTS\"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "DFW1" Then
NFN = "V:\OpenArray\Exports\"
End If
If ActiveWorkbook.Sheets("COVER").Range("A4").Value = "LAX1" Then
SPFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/LAX1/"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "ATL1-HULK" Then
SPFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/ATL1/"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "ATL1-THOR" Then
SPFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/ATL1/"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "SDF1" Then
SPFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/SDF1/"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "DFW1" Then
SPFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/DFW1/"
End If
ActiveWorkbook.Sheets("QuantStudio 12K Flex_export").Copy
With ActiveWorkbook
.SaveAs Filename:=NFN & NewFileName, FileFormat:=xlText
.SaveAs Filename:=SPFN & NewFileName & ".txt", FileFormat:=20
End With
ActiveWorkbook.Close
' Workbooks(NewFileName & ".txt").Close SaveChanges:=True
' Workbooks(ANFileName & ".xlsm").Close SaveChanges:=True
' added new Workbooks(ANFileName).Activate code to see if that helps with file saving
Workbooks(ANFileName & ".xlsm").Activate
wsCover.Select
Workbooks(ANFileName & ".xlsm").Save
ANFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/CS Analyst Files/"
With ActiveWorkbook
.SaveAs Filename:=ANFN & ANFileName, FileFormat:=52
End With
'Application.ScreenUpdating = True
' Workbooks(ANFileName & ".xlsm").Activate
' wsCover.Select
' wsCover.Range("A1").Select
Workbooks(ANFileName & ".xlsm").Close SaveChanges:=False
' Workbooks(ANFileName & ".xlsm").Close
' ThisWorkbook.Close SaveChanges:=False
DoEvents
DoEvents
End Sub[/CODE]
1. A new file will generate from one tab in my active file and will save that tab as a text file into the main folder location as well as a copy to sharepoint.
2. Then, it goes back to the original file, saves the original, and saves a copy to the sharepoint.
The step before this is a "master" file, where a user is able to generate up to 4 files, and each of those 4 generated files then independently run this code, so I have this code tied to the worksheet as I was not able to get it to work from a module. The process works great when only one file is generated, but keeps crashing upon closing when more than one is open and I am not sure if it is not properly finding/reactivating my original workbook or if I have something else incorrect in my code, but any help or suggestions would be greatly appreciated!
Sub savetxt()
Dim SheetComp As String, ws As Worksheet, ws1 As Worksheet, NewFileName As String, NFN As String, SPFN As String, ANFileName As String, ANFN As String
Application.ScreenUpdating = False
ActiveSheet.Shapes.Range(Array("Rectangle 2")).Select
Selection.OnAction = "wsCover.savetxt"
Set ws = ActiveWorkbook.Sheets("COVER")
NewFileName = ActiveWorkbook.Sheets("COVER").Range("B1").Value
ANFileName = ActiveWorkbook.Sheets("COVER").Range("A1").Value
If (ActiveWorkbook.Sheets("QuantStudio 12K Flex_export").Range("A1") = "" Or ActiveWorkbook.Sheets("QuantStudio 12K Flex_export").Range("B1") <> ws.Range("A2")) Then
MsgBox "Start with the green button. Remember to paste QS data to tab!"
Exit Sub
End If
If MsgBox("Are you sure you completed QC Checks?", vbYesNo) = vbNo Then Exit Sub
If ActiveWorkbook.Sheets("COVER").Range("A4").Value = "LAX1" Then
NFN = "M:\OPEN ARRAY\EXPORTS\"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "ATL1-HULK" Then
NFN = "K:\OPEN ARRAY\EXPORTS\"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "ATL1-THOR" Then
NFN = "K:\OPEN ARRAY\EXPORTS\"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "SDF1" Then
NFN = "X:\OPEN ARRAY\EXPORTS\"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "DFW1" Then
NFN = "V:\OpenArray\Exports\"
End If
If ActiveWorkbook.Sheets("COVER").Range("A4").Value = "LAX1" Then
SPFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/LAX1/"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "ATL1-HULK" Then
SPFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/ATL1/"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "ATL1-THOR" Then
SPFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/ATL1/"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "SDF1" Then
SPFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/SDF1/"
ElseIf ActiveWorkbook.Sheets("COVER").Range("A4").Value = "DFW1" Then
SPFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/DFW1/"
End If
ActiveWorkbook.Sheets("QuantStudio 12K Flex_export").Copy
With ActiveWorkbook
.SaveAs Filename:=NFN & NewFileName, FileFormat:=xlText
.SaveAs Filename:=SPFN & NewFileName & ".txt", FileFormat:=20
End With
ActiveWorkbook.Close
' Workbooks(NewFileName & ".txt").Close SaveChanges:=True
' Workbooks(ANFileName & ".xlsm").Close SaveChanges:=True
' added new Workbooks(ANFileName).Activate code to see if that helps with file saving
Workbooks(ANFileName & ".xlsm").Activate
wsCover.Select
Workbooks(ANFileName & ".xlsm").Save
ANFN = "https://paclabs.sharepoint.com/sites/LabOperations/Shared Documents/EXPORTS/CS Analyst Files/"
With ActiveWorkbook
.SaveAs Filename:=ANFN & ANFileName, FileFormat:=52
End With
'Application.ScreenUpdating = True
' Workbooks(ANFileName & ".xlsm").Activate
' wsCover.Select
' wsCover.Range("A1").Select
Workbooks(ANFileName & ".xlsm").Close SaveChanges:=False
' Workbooks(ANFileName & ".xlsm").Close
' ThisWorkbook.Close SaveChanges:=False
DoEvents
DoEvents
End Sub[/CODE]