Option Explicit
Global ExportType$, S$
Sub ExportToXL()
On Error GoTo errorhandler
Dim w As Worksheet, nb As Workbook, n As Long, ThisCell$, nm As Name, z As Byte, i As Long, FirstRow As Long, LastRow As Long, NewPackNames$ ', wName$
Dim Area As String, timestamp As String, wbName As String, ActRge As String, Append As String, DelData As String, SelCell As String, Locked As Boolean
Application.ScreenUpdating = False
S = ThisWorkbook.FullName
S = Replace(S, ThisWorkbook.Name, ("Data Extracts" & Application.PathSeparator))
If Len(Dir(S, vbDirectory)) = 0 Then
MkDir S
End If
Area = GetLocName() '### 19-Oct-2013 ALG
If InStr(Area, "(") Then
Area = Trim(Left(Area, InStr(Area, "(") - 1))
Else
If Len(Area) > 28 Then
Area = Left(Area, 28)
Area = Trim(Left(Area, InStrRev(Area, " "))) & "..."
End If
End If
wbName = Area
S = S & wbName
Beginning:
Area = GetLocName()
Application.CutCopyMode = False
If Not FileExists(S & ".xlsx") Then 'checks if file exists
Application.ScreenUpdating = False
Set w = expExcel3
w.Visible = xlSheetVisible
w.Copy
Set nb = ActiveWorkbook
' wName = nb.Name
w.Visible = xlSheetVeryHidden
'exports sheets to new workbook nb
For n = 2 To 1 Step -1
With ThisWorkbook.Sheets("ex_" & n)
.Visible = xlSheetVisible
.Copy before:=nb.Sheets(1)
.Visible = xlSheetVeryHidden
End With
Next n
nb.Sheets("ex_1").Activate
'code here
For Each nm In nb.Names
If InStr(nm.RefersTo, "[") Then nm.Delete
Next nm
Application.DisplayAlerts = False
nb.SaveAs S & ".xlsx"
Application.DisplayAlerts = True
Else
ThisWorkbook.Activate
Application.ScreenUpdating = True
If guiControls.Range("B24").Value = True Then
Dim YesNo$: YesNo = MsgBox("Would you like to view the dashboard with the existing data?", vbYesNo + vbApplicationModal, "Existing data export")
If YesNo = vbYes Then
Application.Windows(nb.Name).Activate
Exit Sub
Else
nb.Save
Application.DisplayAlerts = False
nb.Close
Application.DisplayAlerts = True
ThisWorkbook.Activate
End If
Exit Sub
End If
Append = MsgBox("A data extract already exists" & vbCr & vbCr & "Click Yes to append the new data to the existing tables" & _
vbCr & "Click No to create a new data export to replace the old data", _
vbYesNoCancel + vbApplicationModal + vbQuestion, "Existing data output")
If Append = vbCancel Then
ThisWorkbook.Activate
Exit Sub
ElseIf Append = vbYes Then
Dim LastPack As String, NewPack As String, SearchPack As String, shName As String
Dim m As Long, q As Long, RowDiff As Long, Insertrows As String, StrSourceRge As String, SearchCol As String, NewRow As String, CopyRange As String
Dim FirstRowSource As String, SourceRange As Range, TargetRange As Range
Application.ScreenUpdating = False
If Not FileOpen(Area & ".xlsx") Then Set nb = Application.Workbooks.Open(S & ".xlsx")
'-- open existing file
ProgressBar 0.1
For n = 1 To 2
'code here
With ThisWorkbook.Sheets("Package " & n)
.Visible = xlSheetVisible
.Copy after:=nb.Sheets(nb.Sheets.Count)
.Visible = xlSheetVeryHidden
End With
Next n
Else
Application.DisplayAlerts = False
If FileOpen(Area & ".xlsx") Then Workbooks(Area & ".xlsx").Close
Kill S & ".xlsx"
Application.DisplayAlerts = True
GoTo Beginning
End If
End If
If InStr(nb.Name, Area) = 0 Then nb.SaveAs S & ".xlsx"
If nb.Sheets("ex_1").Visible = True Then nb.Sheets("ex_1").Visible = xlSheetVeryHidden
If nb.Sheets("ex_2").Visible = True Then nb.Sheets("ex_2").Visible = xlSheetVeryHidden
nb.Sheets("Metric Dashboard").Activate
Workbooks(nb.Name).Save
Application.ScreenUpdating = True
DoEvents
Application.Windows(nb.Name).Activate
Exit Sub
errorhandler:
ErrorTrap "#050001", Err, Right(S, Len(S) - InStrRev(S, Application.PathSeparator)) & ".xlsx"
End Sub
Bookmarks