Excel Crashing after SaveAs

ampd

New Member
Joined
Feb 2, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
  2. Web
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]
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes

Forum statistics

Threads
1,215,069
Messages
6,122,952
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top