简体   繁体   中英

VBA Error: 'Range' of object '_Global' Failed

I am using MS Access and MS Excel to automate a repetitive process wherein I delete the old.csv file, create a new.csv file in it's place, populate a set of cells with data, save and close the file. The following code works exactly as intended every Odd time I execute it. Every Even time, I get "'Range' of object '_Global' Failed" on line 29 (marked with ***). When I get the error, I end process and close the excel window it created without saving. Then when I execute the code again, it works.

I understand that the error is due to a bad cell reference, but nothing I have tried has fixed the issue so that the code works every time it is executed. I appreciate any insight anyone can offer. Thank you.

    'Creating Excel Application and workbook instance
    Dim x1 As New Excel.Application
    Dim xWB As Excel.Workbook
    
    'Delete Previous version of file to avoid overwrite errors
    If Dir("Z:\ETI\01 ETI Engine\automated\ETI Hootsuite FaceBook Feed.csv") <> "" Then
        Kill ("Z:\ETI\01 ETI Engine\automated\ETI Hootsuite FaceBook Feed.csv")
    End If
    
    'Open new Workbook and save as CSV
    Excel.Application.DisplayAlerts = False
    Set x1 = New Excel.Application
    Set xWB = x1.Workbooks.Add
    x1.Visible = True
    xWB.SaveAs "Z:\ETI\01 ETI Engine\automated\ETI Hootsuite FaceBook Feed.csv", FileFormat:=xlCSV
       
    'Assign values to each post, after checking that they are not scheduled in the past.
    Dim url As String
    url = "https://web-ded.uta.edu/wconnect/CourseStatus.awp1?&course=" & Me.txtCourseCode
    
    Dim i As Integer
    For i = 1 To 4
        'Create and assign a generated date for the post
        Dim rndDate As Date
        rndDate = SocialMedia.randDateFB(Me.txtBegDate, i)
        'if the random date is after now, then create the post.
        If rndDate > Now() Then
            With xWB.Worksheets("ETI Hootsuite FaceBook Feed")
                ***.Range("A" & i).Value = rndDate
                .Range("B" & i).Value = SocialMedia.fbPost(Me.txtCatCode, SocialMedia.courseLocation(Me.txtCity, Me.txtState, Me.chkSimulcast), SocialMedia.courseDates(Me.txtBegDate, Me.txtEndDate))
                .Range("C" & i).Value = url
            End With
        Else
        End If
    Next i
    
    'Removes empty rows and removes duplicate posts to meet Hootsuite standards
    Range("A1", "C100").RemoveDuplicates Columns:=Array(2), Header:=xlNo
    Range("A1", "C100").RemoveDuplicates Columns:=Array(1), Header:=xlNo
    
    
    'The following code helped close a program loop, so that it doesn't need to be manually reset every time the code is run.
    xWB.Save
    xWB.Close
    x1.Quit
    
    Set x1 = Nothing
    Set xWB = Nothing
    
    Me.chkFacebookLinkedInPostsSent.Value = True

Every Excel method/property/object must be qualified with your own Excel objects. Otherwise it creates global references that stay alive and stop your code from working the second time.

These are left in your code:

Excel.Application.DisplayAlerts = False

change to

x1.DisplayAlerts = False

And

Range("A1", "C100").RemoveDuplicates Columns:=Array(2), Header:=xlNo
Range("A1", "C100").RemoveDuplicates Columns:=Array(1), Header:=xlNo

must be qualified with your worksheet.

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