erros in refresh copy sheets remove queries export

rjmdc

Well-known Member
Joined
Apr 29, 2020
Messages
703
Office Version
  1. 365
Platform
  1. Windows
hi
i have 2 separate queries one to referesh
the other to export
as many times as as i try to combine them somehow i strip the queries form the main book and it breaks
separately they both work fine it doesnt matter which i run first or second
please help me combine and call the refresh from the export module
i fail the books again and again

Rich (BB code):
Sub ExportCopy()
    
    Dim SaveFolder1         As String:          SaveFolder1 = "M:\all\Exports Copied Sheets\"
    Dim FileName            As String
    Dim FileDateTime        As String:          FileDateTime = Format(Now, "m-d-yy hh-mm")
    Dim FullFileName        As String
    Dim ReportDoc           As String:          ReportDoc = Worksheets("Variables").Range("ReportDocument")
    Dim cn                  As WorkbookConnection
    Dim qry                 As WorkbookQuery
    Dim NewWB               As Workbook
    
    FileName = "Copies " & ReportDoc
    FullFileName = Replace(FileName, ".xlsx", "") & " (" & FileDateTime & ")"

    Sheets(Array(" bef 12", " 6am", " before 6 AM", "12am", "Hours", "ALL times")).Copy
    Set NewWB = ActiveWorkbook
    
    'Clean PQ
    NewWB.Activate
    On Error Resume Next
    For Each cn In NewWB.Connections
        cn.Delete
    Next cn
    For Each qry In NewWB.Queries
        qry.Delete
    Next qry
    
    'Save and Close New WB
    NewWB.SaveAs SaveFolder1 & FullFileName & ".xlsx", xlOpenXMLWorkbook
    NewWB.Close SaveChanges:=False
    DoEvents

    'Back to Main WB
    ThisWorkbook.Activate
    
    'MsgBox "Process Complete"

End Sub
refresh module
Sub RefreshPQs()

Dim ws As Worksheet
Dim ActSheet As String: ActSheet = ActiveSheet.Name

If MsgBox("Did you update Timeclock Download Folder (Yellow Cell)?", vbYesNo + vbCritical) = vbNo Then
MsgBox "Please choose Viventium Upload Folder to Update"
Exit Sub
End If

ThisWorkbook.Activate
Set wb = ThisWorkbook

'Unprotect
For Each ws In wb.Worksheets
Call WSUnProtect(ws)
Next ws

'Refresh All
wb.RefreshAll
DoEvents

' Call ExportCopy

'Protect
For Each ws In wb.Worksheets
Call WSProtect(ws)
Next ws

Worksheets(ActSheet).Activate

' Call SendEmail

MsgBox "Process Complete", vbInformation
End Sub

Rich (BB code):
Sub RefreshPQs()
   
    Dim ws                      As Worksheet
    Dim ActSheet                As String:          ActSheet = ActiveSheet.Name
    
    If MsgBox("Did you update Timeclock Download Folder (Yellow Cell)?", vbYesNo + vbCritical) = vbNo Then
        MsgBox "Please choose Viventium Upload Folder to Update"
        Exit Sub
    End If
        
    ThisWorkbook.Activate
    Set wb = ThisWorkbook
        
    'Unprotect
    For Each ws In wb.Worksheets
        Call WSUnProtect(ws)
    Next ws
    
   'Refresh All
    wb.RefreshAll
    DoEvents
    
'    Call ExportCopy
    
   'Protect
    For Each ws In wb.Worksheets
        Call WSProtect(ws)
    Next ws
    
    Worksheets(ActSheet).Activate
    
'    Call SendEmail
    
    MsgBox "Process Complete", vbInformation
End Sub

Sub RefreshPQs_Folders()
    Dim ws                      As Worksheet
    Dim wb                      As Workbook
    Dim ActSheet                As String:          ActSheet = ActiveSheet.Name
    
    If wb Is Nothing Then
        Set wb = ThisWorkbook
    End If

'    'Unprotect
    For Each ws In wb.Worksheets
        Call WSUnProtect(ws)
    Next ws
    
    wb.Connections("Query - ALL timeclock downloads 2023").Refresh
           
'    'Protect
    For Each ws In wb.Worksheets
        Call WSProtect(ws)
    Next ws
    
    Worksheets(ActSheet).Activate
    
    MsgBox "It is now possible to choose the folder path", vbInformation
End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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