dan_schofield
New Member
- Joined
- Feb 13, 2018
- Messages
- 4
Hello all
Im ok at writing some simple code in vba but when it comes to looping the code I always screw it up!
I have a code that is working and doing what I want it to do, except I have to re write this for 10000 cells! which is extremely time consuming, as you will see I have shown the exact code for a change event on cell O479 and O480. But I want to make it run from O20 through to O10000
How do I loop this? as in what is the correct procedure for Dim? Loop? Next Event? im not sure what to do
Any help or pointers would be great to help me understand how to create loops
Thanks
Dan
Im ok at writing some simple code in vba but when it comes to looping the code I always screw it up!
I have a code that is working and doing what I want it to do, except I have to re write this for 10000 cells! which is extremely time consuming, as you will see I have shown the exact code for a change event on cell O479 and O480. But I want to make it run from O20 through to O10000
How do I loop this? as in what is the correct procedure for Dim? Loop? Next Event? im not sure what to do
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("o480")) Is Nothing Then
If Worksheets("Purchase Orders").Range("o480").Value = "Yes" Then
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.CreateItem(0)
With xEmailObj
.To = "[EMAIL="Maintenance@newcenturycare.co.uk"]Maintenance@newcenturycare.co.uk[/EMAIL]; [EMAIL="Modrek.Zandy@newcenturycare.co.uk"]Modrek.Zandy@newcenturycare.co.uk[/EMAIL]; [EMAIL="Purchase.ledger@newcenturycare.co.uk"]Purchase.ledger@newcenturycare.co.uk[/EMAIL]"
.CC = Worksheets("Purchase Orders").Range("z480").Value
.Subject = "PO/Capex Request Approval"
.Body = "Hello," & vbCrLf & vbCrLf & "I have approved the following PO/Capex requests" & vbCrLf & vbCrLf & Worksheets("Purchase Orders").Range("C180").Value & vbCrLf & vbCrLf & "Regards" & vbCrLf & vbCrLf & Worksheets("Purchase Orders").Range("Q480").Value
.Display
End With
End If
End If
If Not Intersect(Target, Range("o479")) Is Nothing Then
If Worksheets("Purchase Orders").Range("o479").Value = "Yes" Then
Set xOutlookObj = CreateObject("Outlook.Application")
Set xEmailObj = xOutlookObj.CreateItem(0)
With xEmailObj
.To = "[EMAIL="Maintenance@newcenturycare.co.uk"]Maintenance@newcenturycare.co.uk[/EMAIL]; [EMAIL="Modrek.Zandy@newcenturycare.co.uk"]Modrek.Zandy@newcenturycare.co.uk[/EMAIL]; [EMAIL="Purchase.ledger@newcenturycare.co.uk"]Purchase.ledger@newcenturycare.co.uk[/EMAIL]"
.CC = Worksheets("Purchase Orders").Range("z479").Value
.Subject = "PO/Capex Request Approval"
.Body = "Hello," & vbCrLf & vbCrLf & "I have approved the following PO/Capex requests" & vbCrLf & vbCrLf & Worksheets("Purchase Orders").Range("C479").Value & vbCrLf & vbCrLf & "Regards" & vbCrLf & vbCrLf & Worksheets("Purchase Orders").Range("Q480").Value
.Display
End With
End If
End If
End Sub
Any help or pointers would be great to help me understand how to create loops
Thanks
Dan