modifying an Excel spreadsheet with V

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Yes. I was in the process naming the thread and something happened with my keyboard. Anyhow, the intended title is "modifying an Excel spreadsheet with VBA code in an Access Form." My code works most of the time but the times that it does not work I am getting the error:

Run-time error '1004':
Method 'Rows' of object '_Global' failed
Any ideas on why the code only works sometimes?

Code:
Option Compare Database
Public Sub removerowsPGRpt()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSh As Excel.Worksheet
Dim lastRow As Long
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open("D:\PG_Rpt_CustomerExportPT.xls") '<modify as="" needed
Set xlSh = xlWB.Sheets("PG_Rpt_CustomerExportPT.rpt")         '<modify as="" needed


'Delete first 5 rows
Rows("1:5").Select
Selection.Delete Shift:=xlUp

'Delete rows after last record
With ActiveSheet
lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
Rows(lastRow + 1 & ":" & Rows.Count).Delete
End With
xlWB.Save
xlWB.Close
xlApp.Quit
Set xlApp = Nothing
End Sub[CODE]



The debugger is highlighting this "Rows("1:5").Select."


</modify></modify>
 
Last edited:
Upvote 0
When you are automating Excel from Access, or any other application, you should make sure everything is fully qualified.

In your code you have Rows("1:5") without any worksheet/workbook/anything reference.

Also, you shouldn't use ActiveSheet use the reference to the worksheet you created earlier.

Try this.
Code:
Public Sub removerowsPGRpt()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSh As Excel.Worksheet
Dim lastRow As Long

    Set xlApp = New Excel.Application
    Set xlWB = xlApp.Workbooks.Open("D:\PG_Rpt_CustomerExportPT.xls")
    Set xlSh = xlWB.Sheets("PG_Rpt_CustomerExportPT.rpt")
    
    ' Delete first 5 rows
    With xlSh
        .Rows("1:5").Delete Shift:=xlUp
        lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
        .Rows(lastRow + 1 & ":" & .Rows.Count).Delete
    End With
    
    xlWB.Save
    xlWB.Close
    xlApp.Quit
    Set xlApp = Nothing
    
End Sub
 
Upvote 0
I used your code and it works. Thank you very much Norie!!


When you are automating Excel from Access, or any other application, you should make sure everything is fully qualified.

In your code you have Rows("1:5") without any worksheet/workbook/anything reference.

Also, you shouldn't use ActiveSheet use the reference to the worksheet you created earlier.

Try this.
Code:
Public Sub removerowsPGRpt()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSh As Excel.Worksheet
Dim lastRow As Long

    Set xlApp = New Excel.Application
    Set xlWB = xlApp.Workbooks.Open("D:\PG_Rpt_CustomerExportPT.xls")
    Set xlSh = xlWB.Sheets("PG_Rpt_CustomerExportPT.rpt")
    
    ' Delete first 5 rows
    With xlSh
        .Rows("1:5").Delete Shift:=xlUp
        lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
        .Rows(lastRow + 1 & ":" & .Rows.Count).Delete
    End With
    
    xlWB.Save
    xlWB.Close
    xlApp.Quit
    Set xlApp = Nothing
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,692
Messages
6,161,327
Members
451,697
Latest member
pedroDH

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