Reset Outlook Macro

davesexcel

Well-known Member
Joined
Feb 26, 2006
Messages
1,529
I am using this code from an Outlook Userform.

It opens Excel workbook and places the textbox contents into the first empty cell, saves and closes the workbook.

Private Sub CommandButton1_Click()
Dim xlApp As Object, Sht As Object

Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = True
xlApp.Workbooks.Open "C:\DownLoads\Cover Pages Test.xls"

Set Sht = xlApp.Worksheets(1)
Sht.Activate
Dim LastRow As Long
LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1
Sht.Range("A" & LastRow) = TextBox1
Sht.Range("B" & LastRow) = TextBox2
xlApp.ActiveWorkbook.Close SaveChanges:=True 'save excel file
xlApp.Quit 'quit excel
Set xlApp = Nothing

Unload Me

End Sub

The code runs fine, but when it is run a second time it opens the workbook then stops because it has errored at:
LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1

When in VBA editor I press reset, and then the code works fine again. After spending several minutes trying to figure out why it only works every other attempt, it dawned on me that the reason it works the next time is because when I am in debugger, I press reset.

So now when I run the code the 1st time, I reset it in VBA editor and it runs again with no problems.

What can I add to my code so it resets ?
 
Last edited:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

Forum statistics

Threads
1,216,134
Messages
6,129,070
Members
449,485
Latest member
greggy

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