简体   繁体   中英

Error while copying sheet to csv - VBA: Method 'Value' of object 'Range' failed (Run-time error '1004')

I'm having trouble debugging my vba code. The goal of the macro is to take a sheet in the current workbook and save that as a specific csv file. This code worked fine until I got a new computer with Catalina (10.15.4). The error occurs at pasteRange.Value = copyRange.Value and the error code is VBA: Method 'Value' of object 'Range' failed (Run-time error '1004') . So when it errors, MasterLoad.csv is open, but the source data just can't copy over.

Sub SheetToCSV()
    Application.ScreenUpdating = False:  Application.EnableEvents = False:  Application.DisplayAlerts = False
'    On Error GoTo Cleanup

    Dim strSourceSheet As String
    Dim strFullname As String
    Dim fileAccessGranted As Boolean
    Dim filePermissionCandidates
    Dim copyWB As Workbook
    Dim pasteWB As Workbook
    Dim copyRange As Range
    Dim pasteRange As Range
    
    Set copyWB = ThisWorkbook

    ' set variables for sheet name and file path
    strSourceSheet = "MasterLoad"
    strFullname = "/Users/mypath/MasterLoad.csv"
    
    ' grant permission for VBA to open/save MasterLoad file
    filePermissionCandidates = Array(strFullname)
    grantFileAccess (filePermissionCandidates)
    
    ' set copy range
    Set copyRange = copyWB.Sheets(strSourceSheet).Range("A1:ZZ2000")
    
    ' open paste WB, set paste range, set values, and save
    Set pasteWB = Workbooks.Open(strFullname)
    Set pasteRange = pasteWB.Sheets(1).Range("A1:ZZ2000")
    pasteRange.Value = copyRange.Value
    pasteWB.SaveAs FileName:=strFullname, _
                          FileFormat:=xlCSV
    pasteWB.Close SaveChanges:=True 'close wb and save
    
    Application.ScreenUpdating = True:  Application.EnableEvents = True: Application.DisplayAlerts = True
End Sub


Function grantFileAccess(filePermissionCandidates)
  grantFileAccess = GrantAccessToMultipleFiles(filePermissionCandidates) 'returns true if access granted, false otherwise_
End Function

I'm not sure if it's actually an OS issue, because I have virtually the same code in a different workbook and it worked fine with the new computer, but nothing has else changed but getting a new computer with this macro. Any thoughts?

Just to be sure, put a breakpoint on the line

pasteRange.Value = copyRange.Value

and check that both the ranges are well defined.

I had a similar issue when copying and inserting an entire column, sometimes it failed with VBA: Method 'Insert' of object 'Range' failed (Run-time error '1004') for no apparent reason and Excel broke completely and had to be restarted. I'm pretty sure it is a bug in Excel.

As suggested by @ComputerVersteher I removed Application.DisplaysAlerts = False and got an error message saying I didn't have enough memory to complete the operation. I changed the copy/paste range to A1:JZ2000 and it worked, but I still don't understand why the range A1:ZZ2000 wouldn't work on my newer/better computer.

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