Macro to loop through all rows of data

Sangdrax

New Member
Joined
Oct 13, 2014
Messages
9
Good Morning,

I have created the following macro which is supposed to loop through all the data points and create Outlook calendar entries for each line of data in my Spreadsheet.

At the moment it looks like it only enters the first line as an entry to Outlook before stopping.

What do I need to edit to make this include all rows of data?

Code:
Sub CreateAppointment()    Dim myOlApp As Outlook.Application
    Dim myItem As Outlook.AppointmentItem
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim i As Long
   
    Set myOlApp = GetObject(, "Outlook.Application")
    Set ws = Sheets("Sheet1")   'Edit to your worksheet name
   
    With ws
        lastRow = .Cells(1, "A").End(xlUp).Row + 1  'Last row of data
    End With
   
    For i = 2 To lastRow  'Starting at 2 assumes column headers on row 1
        'The following line adds one appointment item for each loop
        Set myItem = myOlApp.CreateItem(olAppointmentItem)
        With myItem
            .Subject = ws.Cells(i, "A")
            .Location = ws.Cells(i, "B")
            .Body = ws.Cells(i, "C")
            .Start = ws.Cells(i, "D") + ws.Cells(i, "E")
            .End = ws.Cells(i, "F") + ws.Cells(i, "G")
            .ReminderMinutesBeforeStart = ws.Cells(i, "H")
            .Save
        End With
    Next i
End Sub

Many thanks for any assistance/suggestions you may have.

Kind Regards,

S
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
oops....misread question ???
 
Last edited:
Upvote 0
lastRow = .Cells(1, "A").End(xlUp).Row + 1 'always returns 2

Perhaps you want:

lastRow = .Cells(Rows.Count,1).End(xlUp).Row
 
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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