简体   繁体   中英

Excel VBA Run-time error 1004 on Mac Excel 2016 (16.36)

Public ConnestionString As String
Public Function DriverConnector()  
#If Mac Then
    'if Mac then use this driver
    ConnectionString = "ODBC;Driver={Amazon Redshift};SERVER={rs.asdfreqt2.us-east-1.redshift.amazonaws.com};UID=user_ro;PASSWORD=pwd;DATABASE=rptg;PORT=8192"
#End If
End Sub

The above is the Mac snippet from the connector function

#If Mac Then
    

    ThisWorkbook.Sheets(Sheet7.Name).Range("A1:A2").Clear
    sqlCommand = "SELECT CASE WHEN usename IS NOT NULL THEN 'True' ELSE 'False' END AS authorized " & _
                 "FROM pg_user , pg_group  WHERE " & _
                 "pg_user.usesysid = ANY(pg_group.grolist) AND  " & _
                 "pg_group.groname='sssrfullaccess' AND usename = '" & LCase(Environ("USER")) & "'"

    With ThisWorkbook.Sheets(Sheet7.Name).QueryTables.Add(Connection:=ConnectionString, Destination:=ThisWorkbook.Sheets(Sheet7.Name).Range("A1"), Sql:=sqlCommand)
        .BackgroundQuery = False
        .Refresh
    End With
    user_auth = ThisWorkbook.Sheets(Sheet7.Name).Range("A2").Value
    
    If user_auth = "True" Then
        MsgBox ("Access granted. Check Approved Filters tab for approval parameters.")
    Else
        reqAccess = MsgBox("You do not have permission to use this file.  Click Yes to Close the File and request access, click No to close the file.", vbYesNo)
        If reqAccess = vbYes Then
            Call Mail_Access_Request_Outlook
            ThisWorkbook.Close
        End If
    End If

Code snippet for the Mac. I am getting a 1004 with .Refresh and I don't know why. I have verified the ConnectionString is correct in the QueryTables.Add, I verified the sqlCommand is correct in the same spot. It throws a 1004 only on the .Refresh

I know in the past versions of Excel for Mac this was an issue, but I haven't been able to find anything recent or anything to say if it was fixed or not. How can I fix this?

Try this:

With ThisWorkbook.Sheets(Sheet7.Name).QueryTables.Add(Connection:=ConnectionString, Destination:=ThisWorkbook.Sheets(Sheet7.Name).Range("A1"), Sql:=sqlCommand).Refresh 
     BackgroundQuery:=False
End With

Other answers around this error code suggest Refresh is a method, and With should be used to set properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM