So, the below code works just fine on my end. But, if I send the excel file containing the code below to a different person, they get the Run-time Error 52. I've tried several different iterations of the code below, but all keep coming up with the same error.
UPDATE: Error 52 Resolved, but error 1004 now occurs when reading the line containing the objConnection.Refresh statement.
I'm curious to learn what situations would cause something to work fine on my end but not on someone else's PC, and how best to circumvent this issue. Code below:
Sub Download_CSV()
Dim myURL As String
Dim PathExcel As String
Dim csvfilenamefull As String
PathExcel = Application.ActiveWorkbook.Path
csvfilenamefull = PathExcel & "\SharepointData.xlsx"
'Remove File if already there
If Dir(csvfilenamefull) <> "" Then
VBA.SetAttr csvfilenamefull, vbNormal ' remove any file attributes (e.g. read-only) that would block the kill command
VBA.Kill csvfilenamefull ' delete the file
End If
myURL = "https://company.sharepoint.com/sites/Shared%20Documents/Data.xlsx"
Workbooks.Open (myURL)
ActiveWorkbook.SaveAs filename:=csvfilenamefull, _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
'Run check to ensure the opened file has finished connect to online database
For Each objConnection In ActiveWorkbook.Connections
'Get current background-refresh value
bBackground = objConnection.OLEDBConnection.BackgroundQuery
'Temporarily disable background-refresh
objConnection.OLEDBConnection.BackgroundQuery = False
'Refresh this connection
objConnection.Refresh
'Set background-refresh value back to original value
objConnection.OLEDBConnection.BackgroundQuery = bBackground
Next
End Sub
I simply tried various bits of code from different recommendations on this site (all of which worked successfully on my computer), but continue to give the other party the Run Time Error 52.
Related
I´ve been getting an automation error and so far have been unsuccessful to prevent it.
Since it seems like the cause for this can be very different from case to case, i´ll try my best to describe my project:
Inside of a frontend workbook there is all the code and inside a backend workbook there is the relevant data.
On startup the frontend opens the backend like this
Set daten_betrieb = Workbooks.Open(speicherort & "GB-Backend-" & kuerzel_betrieb & ".xlsx", _
UpdateLinks:=0, _
ReadOnly:=True, _
IgnoreReadOnlyRecommended:=True, _
Notify:=False, _
CorruptLoad:=xlNormalLoad)
Everything works fine except when the user manually reopens the backend and closes it, while the frontend is still open.
I added the following line to the code
If daten_betrieb Is Nothing Then Call backend_betrieb
backend_betrieb is the sub that uses the Workbooks.Open method mentioned earlier.
Even though the backend is open (again), i still get 2147221080 - Automation error on this line:
If daten_betrieb.Sheets("Arbeitsmittel & AKZ").Cells(1, i).Value = Arbeitsmittel Then
daten_betrieb refers to the freshly opened Workbook which is set to this name.. why is this a problem? Any ideas?
Thanks in Advance!
Edit:
Maybe the problem is with If backend_betrieb Is Nothing Then call backend_betrieb
When stepping through I see that excel doesnt jump into the sub backend_betrieb(). Does excel somehow think that the name/variable "backend_betrieb" is still there --> not Nothing. But the Workbook behind it is closed which leads to the error?!
I found a way to handle the Error.
On Error Resume Next
If daten_betrieb.Sheets("Arbeitsmittel & AKZ").Cells(1, i).Value = Arbeitsmittel Then
xxx
End If
If Err.Number = -2147221080 Then
Set daten_betrieb = Nothing
GoTo Start
End If
On Error GoTo ErrorHandler
Start is at the beginning of the sub, before If daten_betrieb Is Nothing call backend_betrieb
So basically it really seems like the Workbook was closed (manually through the user) but the object/name/whatever "daten_betrieb" was not Nothing. So i had to set it to Nothing for the If daten_betrieb Is Nothing call backend_betrieb line to take action.
I have been working with this issue for days. I tried multiple different ways. I am attempting to append multiple files into an Access linked table or even a temp table or even into a single excel file. At first EVERY SINGLE TIME on the first attempt the program works perfectly, then after that it stops functioning for a period of time and then starts operating again. When it stop functioning I get an Subscript out of range run-time error 9.I open the proper excel file but for some reason it won't let me set it... How can it OPEN THE FILE but in the next line CAN'T FIND IT??? It is driving me insane, it works, it stops working, then it works again... Any advice or hints would be very much appreciated.
This is just one way I tried to do this but they all end the same.
i = 2 'i is created through another loop previously.
j = 0
With MyXL
.Visible = True
.DisplayAlerts = True
End With
Do
Set MyXL = CreateObject("Excel.Application")
MyXL.Workbooks.Open Directory & fileArray(j), Notify:=False, ReadOnly:=False 'Tried True previously but changed since i was making changes to the file.
Set wb = Workbooks(fileArray(j)) 'DING DING DING!!! WHY??? You WORKED before!!!
If wb.Sheets("Sheet1").Range("A1") = "System Status" Then
wb.Sheets("Sheet1").Range("A1") = "PO System Status"
wb.Save
End If
wb.Close True
Set wb = Nothing
MyXL.Quit
Set MyXL = Nothing
Set wb = Nothing
j = j + 1
Loop Until j = i
Previously I thought I wasn't closing the workbook correctly, but I have closed the MyXL and previous wb but i still run into the error. I was wondering if this is something that Access/vba just can't do in succession as well. I changed the ReadOnly to true and it still ends up the same way.
Set your workbook to the return value from the Open method:
Set wb = MyXL.Workbooks.Open(Directory & fileArray(j), Notify:=False, ReadOnly:=False)
If wb.Sheets("Sheet1").Range("A1") = "System Status" Then
wb.Sheets("Sheet1").Range("A1") = "PO System Status"
wb.Save
End If
You don't need to/shouldn't create a new Excel application instance for every file - set that up before you enter the loop, and close it once you're done updating files. Check your Task Manager and make sure you don't have a bunch of Excel instances hanging around.
I'm stuck. I'm running an array to open selected files, which do open, but once I begin to attempt to add processing code I can't seem to activate the sheets I want. I try and debug on strShName and the variable never gets populated, always null, but the variable ExpRptFileName(N) does get populated with the full path and file name.ext.
I'm wondering if the path should be removed (which I've tried with several variations but it fails) or if the type of loop I'm using is hanging me up.
In the following code, the variable ExpRptID is null; doesn't pull the data from the sheet......while the variable ExpRptFileName(N) contains the proper path, file name, and file ext.
Here's the code:
ExpRptFileNameInLoop = Right(ExpRptFileName(N), Len(ExpRptFileName(N)) - InStrRev(ExpRptFileName(N), Application.PathSeparator, , 1))
If bIsBookOpen(ExpRptFileNameInLoop) = False Then
Set mybook = Nothing
On Error Resume Next
Set mybook = Workbooks.Open(ExpRptFileName(N))
On Error GoTo 0
If Not mybook Is Nothing Then
Workbooks.Open ExpRptFileName(N)
Dim ExpRptID As String
ExpRptID = Sheets("Expense Report").Range("R3").Value
Exit Sub
Function bIsBookOpen(ByRef szBookName As String) As Boolean
' ************** Notes here
On Error Resume Next
bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function
I understand similar questions with these errors have been posted before, but I found nothing when it came to formatting tables so don't close this please. In my VBA code in MS Access 2013 it exports data from MS Access to Excel. 6 different queries get exported into 1 excel file, each on a different worksheet. This works fine. I then format each sheet to have all the data in a table. I have a form which lets the user choose the path to save the file. If it is the first time creating the file, it works properly. If it is the second time creating the file in that same directory, it doesn't work and it gives me the error:
Run-time error 1004: Method Range of object _Global failed
I figured this was because I was overwriting my file instead of deleting it and recreating it. So I added in some code to check if the file exists, and if it does, delete it. I added breakpoints and while running through this part of the code, I was watching my documents folder. The file successfully got deleted and then recreated which is what I wanted. It still gave me that error. I manually went to delete the file and then reran my code again. It worked properly.
How come I need to manually delete this file in order to rerun my code? Or is it something else that is causing the problem? Here is the important parts of my code as the whole thing is too long to post:
'Checks if a file exists, then checks if it is open
Private Sub checkFile(path As String)
Dim openCheck As Boolean
'If file exists, make sure it isn't open. If it doesn't, create it
If Dir(path) <> "" Then
openCheck = IsFileLocked(path)
If openCheck = True Then
MsgBox "Please close the file in " & path & " first and try again."
End
Else
deleteFile (path)
End If
Else
End If
End Sub
Sub deleteFile(ByVal FileToDelete As String)
SetAttr FileToDelete, vbNormal
Kill FileToDelete
End Sub
Private Sub dumpButton_Click()
On Error GoTo PROC_ERR
Dim path As String
Dim testBool As Boolean
path = pathLabel4.Caption
path = path & Format(Date, "yyyy-mm-dd") & ".xlsx"
checkFile (path)
dumpQueries (path)
formatFile (path)
'Error Handling
PROC_ERR:
If Err.Number = 2001 Then
MsgBox "A file may have been sent to " & path
Exit Sub
ElseIf Err.Number = 2501 Then
MsgBox "A file may have been sent to " & path
Exit Sub
ElseIf Err.Number = 3021 Then
MsgBox "A file may have been sent to " & path
Exit Sub
ElseIf Err.Number = 2302 Then
MsgBox "A file may have been sent to " & path
Exit Sub
ElseIf Err.Number = 0 Then
MsgBox "Your file has been stored in " & pathLabel4.Caption
Exit Sub
Else
MsgBox Err.Number & ": " & Err.Description & vbCrLf & vbCrLf & "New Error. Please contact the IT department."
End If
Private Sub dumpQueries(path As String)
Dim obj As AccessObject, dB As Object
Set dB = Application.CurrentData
For Each obj In dB.AllQueries
testBool = InStr(obj.name, "Sys")
If testBool <> True Then
If obj.name = "example1" Or obj.name = "example2" Then
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, obj.name, path, True, editWorksheetName(obj.name)
End If
End If
Next obj
End Sub
'Autofits the cells in every worksheet
Private Sub formatFile(path As String)
Dim Date1 As Date, strReportAddress As String
Dim objActiveWkb As Object, appExcel As Object
Set appExcel = CreateObject("Excel.Application")
appExcel.Visible = False
appExcel.Application.Workbooks.Open (path)
Set objActiveWkb = appExcel.Application.ActiveWorkbook
With objActiveWkb
Dim i As Integer
For i = 1 To .Worksheets.count
.Worksheets(i).Select
.Worksheets(i).Cells.EntireColumn.AutoFit
.Worksheets(i).ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).name = "myTable1"
Next
End With
appExcel.ActiveWindow.TabRatio = 0.7
objActiveWkb.Close savechanges:=True
appExcel.Application.Quit
Set objActiveWkb = Nothing: Set appExcel = Nothing
End Sub
The error occurs near the bottom of the code. It's the line:
.Worksheets(i).ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).name = "myTable1"
There may be a couple functions I left out but they work fine and shouldn't be needed for answering the question.
This is the only relevant code:
Set objActiveWkb = appExcel.Application.ActiveWorkbook
With objActiveWkb
Dim i As Integer
For i = 1 To .Worksheets.count
.Worksheets(i).Select
.Worksheets(i).Cells.EntireColumn.AutoFit
.Worksheets(i).ListObjects.Add(xlSrcRange, Range("A1").CurrentRegion, , xlYes).name = "myTable1"
Next
End With
Things get easier to follow when you trim the fluff away and start naming things - there's no need to .Select anything, appExcel is already an Application object, and there's no need to make a copy reference to the active workbook just to use in a With block, especially if that copy is going to be an Object variable anyway - if the copy were a Workbook object then you would at least get IntelliSense for its members...
Your source range is ambiguous. Range("A1") in Excel-VBA is an implicit reference to the active worksheet.. but this is Access-VBA, so there's no such thing, xlSrcRange is an enum value defined in the Excel object model, so if you don't have a reference to the Excel object model (you're late-binding this, right?), and Option Explicit isn't specified, then xlSrcRange is treated by VBA like just another undeclared/uninitialized variable, and therefore you're passing a 0 there, and the xlSrcRange enum value stands for a 1 - and 0 happens to be the underlying value for xlSrcExternal. Same with xlYes.
Since we cannot possibly guess what the actual source range is supposed to be from the code you posted, I'm leaving you with this:
Dim target As Object
Dim srcRange As Object
Set srcRange = TODO
With appExcel.ActiveWorkbook
Dim i As Integer
For i = 1 To .Worksheets.Count
.Worksheets(i).Cells.EntireColumn.AutoFit
Set target = .Worksheets(i).ListObjects.Add(1, srcRange, , 1)
If target Is Not Nothing Then target.Name = "myTable1"
Next
End With
Side question... why name the table myTable1 when Excel will already have named it Table1 anyway? Also note, if .Add fails, your code blows up with a runtime error 91 because you'd be calling .Add off Nothing. Verifying that the target is not Nothing before setting its Name will avoid that.
To answer your question in the comments:
#Mat'sMug is this what you were talking about? because it gives me this error: "438: Object doesn't support this property or method" Here's the code: .Worksheets(i).ListObjects.Add(SourceType:=xlSrcRange, Source:=.Cells(1).CurrentRegion, _ XlListObjectHasHeaders:=xlYes, TableStylename:="TableStyleMedium1").name = "Table"
The reason this throws a 438 is because your With block variable is a Workbook object, and a Workbook object doesn't have a .Range member.
What I was talking about, is that in Excel VBA unqualified calls to Range, Row, Column, and Cells are implicitly referencing the ActiveSheet, and unqualified calls to Worksheets, Sheets and Names are implicitly referencing the ActiveWorkbook - that's a recurrent problem in a lot of VBA code and a very common mistake to make. The solution is basically to say what you mean, and mean what you say; in this case the failure is on "mean what you say" - the unqualified Range("A1") call is, according to the error message, calling [_Globals].Range("A1")... which is weird because it implies that you're referencing the Excel object model library, which means your late-binding and Object variables could just as well be early-bound: why deal with Object variables and lack of IntelliSense when you're already referencing the library you're late-binding to?
I have a Power Query set in myexcel.xlsx. I set its connections's properties as
this and this.
I wrote a VBA code like the following
Sub UpdateData()
Dim filename As String
Dim wbResults As Workbook
filename = "C:\myexcel.xlsx"
Set wbResults = Workbooks.Open(filename)
ActiveWorkbook.RefreshAll
wbResults.Close savechanges:=True
End Sub
When I open the myexcel.xslx manually, the Power Query connection updates. But through VBA code it doesn't. I should add I tested this with an old fashioned Excel Connection andit works fine through VBA code. But the problem is with Power Query connections. Any thoughts?
It is actually rather easy, if you check out your existing connections, you can see how the power query connection name starts, they're all the same in the sense that they start with "Query - " and then the name... In my project, I've written this code which works:
Sub RefreshQuery()
Dim con As WorkbookConnection
Dim Cname As String
For Each con In ActiveWorkbook.Connections
If Left(con.name, 8) = "Query - " Then
Cname = con.name
With ActiveWorkbook.Connections(Cname).OLEDBConnection
.BackgroundQuery = False 'or true, up to you
.Refresh
End With
End If
Next
End Sub
This will refresh all your power queries, but in the code you can see it says:
If Left(con.name, 8) = "Query - " Then
This just means if the name of the connection, the first EIGHT characters starting from the LEFT and moving towards the RIGHT (the first 8 characters) equals the string "Query - " then...
and if you know the name of your query, adjust the 8 to a number that will indicate the amount of characters in your query name, and then make the statement equal to your query connection name, instead of the start of all power query connections ("Query - ")...
I'd advise NEVER updating all power queries at once IF you have a large amount of them. Your computer will probably crash, and your excel may not have auto saved.
Happy coding :)
If you refresh all connections via a loop, you cannot control the order in which this happens. If you need control of the sequence, or if you need to refresh just a couple of Power Queries, this is also an option:
The first function refreshes one single Power Query. The argument of the function in parentheses is the name of the query as visible on the "Queries and connections" pane in Excel. Note how this is translated into the connection name by adding "Query - " as prefix.
The second function then uses the first function to call specific Power Queries in a specific order, giving you full control.
Public Sub RefreshSpecificPowerQuery(pqName As String)
Dim con As WorkbookConnection
Dim conName As String
conName = "Query - " & pqName
With ActiveWorkbook.Connections(conName).OLEDBConnection
.BackgroundQuery = False 'or TRUE, as the case requires
.Refresh
End With
End Sub
Public Sub RefreshListOfPowerQueries()
Call RefreshSpecificPowerQuery("pqMyFirstPowerQueryName")
Call RefreshSpecificPowerQuery("pqMySecondPowerQueryName")
End Sub
Since you're using Power Query, which is different to Power Pivot, you have two options:
Automatic Update the data source when the file is open - (http://www.excel2013.info/power-query/automatic-update/)
Write a VBA script for updating it
For Each cn In ThisWorkbook.Connections
If cn = "Power Query – Employee" Then cn.Refresh
Next cn
End Sub
copied from here:
https://devinknightsql.com/category/power-query/
Just in response to James Heffer’s post which worked for me after some tweaking.
If you live in non-English speaking country your connection changes name.
You can see connection name by adding a Debug.Print command like here
Sub RefreshQuery()
Dim con As WorkbookConnection
Dim Cname As String
For Each con In ActiveWorkbook.Connections
Debug.Print con
If Left(con.name, 8) = "Query - " Then
Cname = con.name
With ActiveWorkbook.Connections(Cname).OLEDBConnection
.BackgroundQuery = False 'or true, up to you
.Refresh
End With
End If
Next
End Sub
After you run the code, it will show you the localized names. Mine is called “Forespørgsel – LevBonusData”
Hope it helps somebody 😊
You can try this code as well
Sub auto_open()
ActiveWorkbook.RefreshAll
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
ThisWorkbook.Save
ChDir "D:\Data"
ActiveWorkbook.SaveAs Filename:="D:\Data\abc.txt", FileFormat:=xlText, CreateBackup:=False
Application.Quit
End Sub
When you will open file at that time macro will run automatically and also data will be saved and in last file will be saved as TXT format as well :)