I have a "main" workbook that has several tabs for several different order processing tasks. I past data, run some macros that format or add columns based on values. Then I export the current worksheet to new predefined XLS file named based on cell value variables. E.g. - there's a "setting" sheet that contains the path and filename of the exported sheet.
I've been using the following code which was based on someone's forum post, slightly modified. It uses the old file save method (which sometimes crashes randomly), yes I know, but I don't know how to write newer code.
The problem is that these sheets show a user form on active that has buttons to do the tasks. When the sheet is exported to the new XLS it's done by the copy sheet method, which carries over the VBA "worksheet.activate" code. When the new book opens it tries to run the userform which doesn't exist.
I found code that tried to delete the VBA here and on other forums, and I enabled the trusted VBA model option in the trust center, but it doesn't work. So, basically, I need help with finding a way to export the sheet's active ranges (there are no blank lines) to the new XLS file (which gets overwritten every day so technically it's not a "new file") using the cell values, not the worksheet.copy method.
Public Sub PrintExport()
Dim rng As Range
Dim wkb As Workbook
Dim sht As Worksheet
Dim EXPath As String
EXPath = ThisWorkbook.Worksheets("Settings").Range("B2")
Range("A2").Select
If Range("A2") = "" Then
MsgBox ("Not enough data.")
Exit Sub
Else
Set rng = ActiveSheet.UsedRange
On Error GoTo 0
If Not rng Is Nothing Then
fileSaveName = Application.GetSaveAsFilename(InitialFileName:=EXPath, filefilter:="xls Files (*.xls),*.xls,")
'Application.GetSaveAsFilename(fileFilter:="xls Files (*.xls), *.xls")
If fileSaveName <> False Then
ActiveSheet.Copy
Set wkb = ActiveWorkbook
Set sht = wkb.ActiveSheet
ActiveSheet.Name = "sheet1"
sht.Cells.Delete
rng.Copy sht.Range("A1")
'this is where I tried the VBA delete code
wkb.SaveAs fileSaveName, FileFormat:=xlExcel8
wkb.Close
End If
End If
End If
I tried this VBA delete code but no luck.
With ThisWorkbook.VBProject.VBComponents(strName).CodeModule
.DeleteLines 1, .CountOfLines
.xlsx. - Ralph.xlsxformat. One could simply save it as.xlsxto remove the VBA code and then save it again as.xls. All you need is Office 2007+. - Ralph