Combining VBA codes

RW594

New Member
Joined
Nov 20, 2018
Messages
5
Hi there!

I'm new to this forum, so please let me know if I'm doing anything wrong!

I'm not versed in the arts of VBA, but have managed to find the codes I need through Google. The codes read:


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 1 Or Target.Cells.Count > 1 Then Exit Sub
Target.Offset(0, 1) = Now
End If
End Sub



These codes allow me to automatically insert a time stamp in Column 2, whenever Column 1 is filled or changed. Nice and simple.

I intend to run these codes twice on the same sheet, once for Column 1&2, and once for Column 5&6.

I've tried pasting the codes below themselves, like this:


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 5 Or Target.Cells.Count > 1 Then Exit Sub
Target.Offset(0, -1) = Now
End If


If Target.Column <> 5 Or Target.Cells.Count > 1 Then Exit Sub
Target.Offset(0, -1) = Now
End If

End Sub


But I then get a compilation error: end if without block if.

I've googled and read many post about combining VBAs, but still haven't come to grips.

So, many many thanks if you are willing to help!!!

Best,
Ed
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Remove both end ifs
Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 5 Or Target.Cells.Count > 1 Then Exit Sub
Target.Offset(0, -1) = Now
End If
Code:
If Target.Column <> 5 Or Target.Cells.Count > 1 Then Exit Sub
Target.Offset(0, -1) = Now
[color=red]End If[/color]
 
Last edited:
Upvote 0
Thank you for the fast response Michael. I've taken out the last End If (below the second part of code), but somehow I'm still getting the compliation error... Am I doing something wrong?
 
Upvote 0
Oh, yes, I removed both End Ifs, and I'm no longer getting the compilation error.

However, the second part of codes (column 4&5) doesn't seem to work. The first part functions fine tho...
 
Upvote 0
And btw I've noticed an error in the initial codes I'd given you. The combined codes read this:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 2 Or Target.Cells.Count > 1 Then Exit Sub
Target.Offset(0, -1) = Now

If Target.Column <> 5 Or Target.Cells.Count > 1 Then Exit Sub
Target.Offset(0, -1) = Now

End Sub
 
Upvote 0
This would be easier

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column <> 1 And Target.Column <> 5 Or Target.Cells.Count > 1 Then Exit Sub
Target.Offset(0, 1) = Now
End Sub
 
Upvote 0
This code is magnificent! Much simpler and way more elegant:laugh:

Thank you so much Michael, for helping with the problem and for openning my eyes to new possibilities with VBA!!!

Best,
Ed
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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