You are here:

Excel/Private Sub Change by Value

Advertisement


Question
QUESTION: Greetings Bob,

I am trying to run multiple Private Subs in a worksheet. I do not know how to combine them. I tried to just combine the code but the "Exit Sub" conditions cause problems and if I eliminate the "Exit Subs" that the sub hangs in an endless loop, I think because I am running real time data.

Private Sub Worksheet_Change_6(ByVal Target As Range)
 If Target.Address <> "$A$3" Then Exit Sub
 If Application.CountA(Range("F1:G1")) = 0 Then Range("B3:E3") = [A3]
 If [F1].Value = 0 Then [F1].Value = 1
 [E3].Value = [A3].Value
 If [A3].Value > [C3].Value Then [C3].Value = [A3].Value
 If [A3].Value < [D3].Value Then [D3].Value = [A3].Value
End Sub
Private Sub Worksheet_Change_7(ByVal Target As Range)
 If Target.Address <> "$A$8" Then Exit Sub
 If Application.CountA(Range("F5:G5")) = 0 Then Range("B8:E8") = [A8]
 If [F5].Value = 0 Then [F5].Value = 1
 [E8].Value = [A8].Value
 If [A8].Value > [C8].Value Then [C8].Value = [A8].Value
 If [A8].Value < [D8].Value Then [D8].Value = [A8].Value
End Sub
Private Sub Worksheet_Change_8(ByVal Target As Range)
 If Target.Address <> "$A$13" Then Exit Sub
 If Application.CountA(Range("F10:G10")) = 0 Then Range("B13:E13") = [A13]
 If [F10].Value = 0 Then [F10].Value = 1
 [E13].Value = [A13].Value
 If [A13].Value > [C13].Value Then [C13].Value = [A13].Value
 If [A13].Value < [D13].Value Then [D13].Value = [A13].Value
End Sub

Thank you for any assistance.

Dave

ANSWER: There's only one Worksheet_Change event per sheet, not WOrksheet_Change_6 or any other. None of these will run as event code (when the sheet changes), so I don't understand what you're trying to do.

---------- FOLLOW-UP ----------

QUESTION: This is an attempt to bring in a stock price in real time and have it extrapolate the Open, High, Low, Close until it is reset by other code.

Are you saying I can only have one change event per worksheet? If I run the following code only in the worksheet it runs perfectly. The problem is I need additional rows besides row "B3:E3" when "A3" changes even if they do not occur at the same time.

In other words I need to duplicate this code in rows 4 and 5 etc. This is an attempt to bring in a stock price in real time and have it extrapolate the open, High, Low, Close until it is reset by other code.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$A$3" Then Exit Sub
If Application.CountA(Range("F1:G1")) = 0 Then Range("B3:E3") = [A3]
If [F1].Value = 0 Then [F1].Value = 1
[E3].Value = [A3].Value
If [A3].Value > [C3].Value Then [C3].Value = [A3].Value
If [A3].Value < [D3].Value Then [D3].Value = [A3].Value
End Sub

Answer
Yes, only one change event per sheet.

something like:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.column <> 1 Then Exit Sub
If Target.Row<3 then exit sub
If Application.CountA(Target.offset(-2,5).Resize(1,2)) = 0 Then target.offset(0,1).resize(1,4)=target.value
If target.offset(-2,5).Value = 0 Then target.offset(-2,5).Value = 1
target.offset(0,4).Value = target.Value
If target.Value > target.offset(0,2).Value Then target.offset(0,2).Value = target.Value
If target.Value < target.offset(0,3).Value Then target.offset(0,3).Value = target.Value
End Sub
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Bob Umlas

Expertise

I`m a Microsoft Excel MVP (Most Valuable Professional) and have been since the inception of the program in 1995. I can answer every kind of Excel question except: API, Importing/exporting to other programs (powerpoint, word,...) Also check out my in-person training link at http://www.thumbtack.com/ny/new-york/excel-training/

Experience

Worked with MS Excel since version 0.99 (on the Mac!). Was contributing editor to Excellence Magazine, having written >300 articles. John Walkenbach said of me "I finally met someone who knows as much about Excel as I do."

Publications
Excellence, The Expert, Microsoft

Education/Credentials
BA in math, Hofstra University, 1965

Awards and Honors
MVP
Led sessions for the Convergence 2004-2006 seminar on Excel tips & tricks

©2016 About.com. All rights reserved.