[VBA] Outlook form to open Excel file, find last unused row and fill it with data

pitaszek

Board Regular
Joined
Jul 20, 2012
Messages
85
Hi Guys,

I've got an Outlook form that opens Excel and puts details of selected email there. I need to figure out how to find last unused row in this Excel file.

Normally the procedure goes more or less as show below but this work as long as we are within Excel application:
Code:
Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

For Outoolok <-> Excel relation it returns 'Object Required' error. The code goes as follows:
Code:
Private Sub CommandButton1_Click()
Dim xlApp As Object
Dim xlWB As Object
Dim Submitter As String
Dim From As String
Dim DateStamp As Date
Dim TaskName As String
    
    Set xlApp = CreateObject("Excel.Application")
    Set xlWB = xlApp.Workbooks.Open("[URL="file://\\networkdrive\something\ekselek1.xlsx"]\\networkdrive\something\ekselek1.xlsx[/URL]")
    Submitter = SubmitterBox.Value
    From = FromBox.Value
    DateStamp = DateStampBox.Value
    TaskName = TaskNameBox.Value
    
    Select Case True
        Case OptionButton1
            Region = "Americas"
        Case OptionButton2
            Region = "Europe"
        Case OptionButton3
            Region = "Asia"
    End Select
    
       Select Case True
        Case OptionButton4
            MainCat = "Data_Request_New_Report"
        Case OptionButton5
            MainCat = "Data_Request_ExistingReport"
        Case OptionButton6
            MainCat = "Changes_to_report"
        Case OptionButton7
            MainCat = "Manual_upload"
        Case OptionButton8
            MainCat = "Investigation"
        Case OptionButton9
            MainCat = "Static_maintenance"
    End Select
    
    'With xlWB.Sheets("Sheet1")
        NextRow = xlWB.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row '<------------------ DOES NOT WORK - returns 'object required'
        xlWB.Sheets("Sheet1").Range("A" & NextRow).Value = Submitter
        xlWB.Sheets("Sheet1").Range("B" & NextRow).Value = From
        xlWB.Sheets("Sheet1").Range("C" & NextRow).Value = DateStamp
        xlWB.Sheets("Sheet1").Range("D" & NextRow).Value = TaskName
    'End With
    
    Unload Me
    xlWB.Close SaveChanges:=True

End Sub

Cheers,
Witek
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
With
Code:
    'With xlWB.Sheets("Sheet1")
        NextRow = xlWB.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row '<------------------ DOES NOT WORK - returns 'object required'
        xlWB.Sheets("Sheet1").Range("A" & NextRow).Value = Submitter
        xlWB.Sheets("Sheet1").Range("B" & NextRow).Value = From
        xlWB.Sheets("Sheet1").Range("C" & NextRow).Value = DateStamp
        xlWB.Sheets("Sheet1").Range("D" & NextRow).Value = TaskName
    'End With
try either removing the 'With Statement' or the preceding 'xlWB.Sheets("Sheet1") from the lines sandwiched between the With Statement.
 
Upvote 0
Hello. Thanks for your reply however it is not the case. You can see this parts are commented.
 
Upvote 0
Does this line actually open the workbook?
Code:
  Set xlWB = xlApp.Workbooks.Open("\\networkdrive\something\ekselek1.xlsx")
If so, is there actually a worksheet named "Sheet1" in that open workbook?
 
Upvote 0
Is this a right way to do it? I remind I am writing a macro in Outlook.

Code:
Set xlApp = CreateObject("Excel.Application")
Set xlWB = xlApp.Workbooks.Open("[URL="file://\\networklocation\file.xlsx"]\\networklocation\file.xlsx[/URL]")
last_row = xlWB.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

Regards,
Witek
 
Upvote 0
Does this line of code actually open "file.xlsx"?
Code:
Set xlWB = xlApp.Workbooks.Open("\\networklocation\file.xlsx")
 
Upvote 0
Yes. If I set for example last_row = 5 it will put the details I need into 5th row of Sheet1.
 
Upvote 0
Try this...
Code:
With xlWB.Sheets("Sheet1")
        NextRow = .Range("A" & .Rows.Count).End(xlUp).Row
        .Range("A" & NextRow).Value = Submitter
        .Range("B" & NextRow).Value = From
        .Range("C" & NextRow).Value = DateStamp
        .Range("D" & NextRow).Value = TaskName
    End With
 
Upvote 0

Forum statistics

Threads
1,215,899
Messages
6,127,637
Members
449,393
Latest member
Messi1408

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