Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 8th, 2016, 07:23 AM
Registered User
 
Join Date: Jun 2016
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Default Several Worksheet_Change formulas in a worksheet

Hello Friends,

This is my first time on an IT forum and I am also new to VBA and therfore was wondering how to write the same formula below several times for several different cells given that I am only able to use one Worksheet change.



Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'If the cell just changed (Target) is the cell we're looking for (F27)
If Target.Address = Me.Range("F30").Address Then
'then let the value of cell G28 be a formated date/time stamp, separated by a space
Me.Range("G31").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
End If
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'If the cell just changed (Target) is the cell we're looking for (F27)
If Target.Address = Me.Range("G20").Address Then
'then let the value of cell G28 be a formated date/time stamp, separated by a space
Me.Range("G21").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
End If
End Sub
 
Old June 8th, 2016, 11:11 AM
Authorized User
 
Join Date: Oct 2015
Posts: 48
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Hi, your comments do not match your code.
Using the description you wrote : use a case or multiple ElseIf statements.
If Target.Address = Me.Range("F30").Address Then
Do ABC...
ElseIf If Target.Address = Me.Range("F32").Address Then
Do XYZ...
ElseIf If Target.Address = Me.Range("F33").Address Then
Do KLM...
__________________
Nostalgia 4 Infinity
The Following User Says Thank You to Zakalwe For This Useful Post:
matthewEnd (June 8th, 2016)
 
Old June 8th, 2016, 12:18 PM
Registered User
 
Join Date: Jun 2016
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Smile Soloution

Many thanks Zakalwe

I now have working multiple Worksheet change formulas Cheers

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = Me.Range("F30").Address Then
Me.Range("G31").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("D52").Address Then
Me.Range("E53").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("D55").Address Then
Me.Range("E56").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("D58").Address Then
Me.Range("E59").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("D61").Address Then
Me.Range("E62").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("D88").Address Then
Me.Range("E88").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("C126").Address Then
Me.Range("D126").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("C127").Address Then
Me.Range("D127").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("C128").Address Then
Me.Range("D128").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("C129").Address Then
Me.Range("D129").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("C130").Address Then
Me.Range("D130").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("C131").Address Then
Me.Range("D131").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("C132").Address Then
Me.Range("D132").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
ElseIf Target.Address = Me.Range("C133").Address Then
Me.Range("D133").Value = Format(Date, "dd-mmmm-yyyy") & " " & Format(Now, "h:mm:ss AM/PM")
End If
End Sub
 
Old June 8th, 2016, 02:57 PM
Authorized User
 
Join Date: Oct 2015
Posts: 48
Thanks: 0
Thanked 5 Times in 5 Posts
Default

I am happy it works but I don't think that's the best way to implement the solution.
What if you have a larger number of ranges? You'd have a very long and slow program.

I'd consider placing the ranges of interest in a union then test if target is part of a union. If it is then use the offset formula on the target.
__________________
Nostalgia 4 Infinity





Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple Worksheet_Change Events nbdau7o Excel VBA 2 July 21st, 2015 01:40 PM
Using Worksheet_Change Event in Excel nbdau7o Excel VBA 1 April 9th, 2015 03:59 PM
Please help!!!!!Private Sub Worksheet_Change VicM Excel VBA 1 March 6th, 2008 10:35 PM
Worksheet_Change executing selectively lfrench Excel VBA 3 April 3rd, 2007 03:59 PM
How to step into Worksheet_Change Piblon Excel VBA 2 December 17th, 2003 04:11 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.