Method 'Cells' of object'_Global' failed and I haven't a clue

gavindave84

New Member
Joined
Feb 27, 2013
Messages
2
Hi

I have been trying to automate some bureaucratic processes in my work. I have created a fair few macro's in my time but I am no master of vba. I have created a macro in word which is suppose to open an excel workbook, find the next available empty row and paste variable values into that row. The macro I have works first time but subsequent times is falls over with the error Method 'Cells' of object'_Global' failed. I have read a few posts and the answer seems to be to do with correctly referencing excel objects each time they are used. Or something like that. I am completely stumped and if anyone could point me in the right direction I would be eternally greatful.


Public Sub OpenExcelFile()

'open excel variables
Dim oExcel As Excel.Application
Dim oWB As Workbook
Dim oWS As Worksheet
Dim quality_database As String
Dim lastrow_available As Integer
Dim entryrow As Integer
Dim number As Integer
Dim submission_date As Date

Application.DisplayAlerts = False
Set oExcel = CreateObject("Excel.Application")
Set oWB = oExcel.Workbooks.Open("U:\G Davis\Internal checking_Quality metrics.xlsx")
Set oWS = oExcel.Worksheets("detail")
oExcel.Visible = True

submission_date = Now()

lastrow_available = oWS.Cells(Cells.Rows.Count, "A").End(xlUp).Row
entryrow = lastrow_available + 1
number = entryrow - 1


'Entry od data into database is done with the assumption that column positions do not change
'These variables are defined in an earlier subroutine
With oWS
.Range("A" & entryrow) = number
.Range("B" & entryrow) = project
.Range("C" & entryrow) = Report
.Range("D" & entryrow) = Author
.Range("E" & entryrow) = Checker
.Range("F" & entryrow) = checked_date
.Range("G" & entryrow) = submission_date
.Range("H" & entryrow) = w
.Range("I" & entryrow) = x
.Range("J" & entryrow) = y
.Range("K" & entryrow) = Z
.Range("L" & entryrow) = w + x + y + Z
End With
With oWB
.Save
.Close
End With
End Sub

The line in bold is where the error is happening. Any thoughts?

Thanks in advance
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try this.
Code:
lastrow_available = oWS.Cells(oWS.Cells.Rows.Count, "A").End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,214,853
Messages
6,121,935
Members
449,056
Latest member
denissimo

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