Trying to get Wooksheet_Change to work:
Public rng As Range
Private Sub Workbook_Open()
Dim range1 As Range
Set range1 = Sheet4.Range("D4:F500")
Set rng = range1
'I need a way to access the subroutine Worksheet_Change
'to fire the object Target range. I tried:
'Worksheet_Change(rng) that didn't work.
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
|
|
0
|
|
|
|
Reply
|
Utf
|
1/24/2010 9:13:01 PM |
|
Detail on what you are trying to do.??
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
dguillett@gmail.com
"Philosophaie" <Philosophaie@discussions.microsoft.com> wrote in message
news:29A5613F-6BA9-4256-AD55-099235798EC1@microsoft.com...
> Trying to get Wooksheet_Change to work:
>
> Public rng As Range
> Private Sub Workbook_Open()
> Dim range1 As Range
> Set range1 = Sheet4.Range("D4:F500")
> Set rng = range1
>
> 'I need a way to access the subroutine Worksheet_Change
> 'to fire the object Target range. I tried:
> 'Worksheet_Change(rng) that didn't work.
>
> End Sub
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> End Sub
|
|
0
|
|
|
|
Reply
|
Don
|
1/24/2010 9:22:56 PM
|
|
This code works
Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D4:F500")) Is Nothing Then
MsgBox "Cell in D4:F500 has be changed"
End If
End Sub
Remember it must be added to the Sheet model not the Genaeal module
One way is to right click the sheet's tab and select View Code
best wishes
--
Bernard Liengme
Microsoft Excel MVP
http://people.stfx.ca/bliengme
"Philosophaie" <Philosophaie@discussions.microsoft.com> wrote in message
news:29A5613F-6BA9-4256-AD55-099235798EC1@microsoft.com...
> Trying to get Wooksheet_Change to work:
>
> Public rng As Range
> Private Sub Workbook_Open()
> Dim range1 As Range
> Set range1 = Sheet4.Range("D4:F500")
> Set rng = range1
>
> 'I need a way to access the subroutine Worksheet_Change
> 'to fire the object Target range. I tried:
> 'Worksheet_Change(rng) that didn't work.
>
> End Sub
> Private Sub Worksheet_Change(ByVal Target As Range)
>
> End Sub
|
|
0
|
|
|
|
Reply
|
Bernard
|
1/24/2010 9:33:37 PM
|
|
The Worksheet_Change procedure MUST be located in the code module for
the worksheet whose changes you want to trap. Each worksheet whose
changes need to be trapped will have its own Change event procedure in
its own code module. If you have Worksheet_Change in the ThisWorkbook
module, VBA does not recognize it as an event procedure and the
procedure will not be called automatically. VBA sees is as just
another ordinary procedure.
If you want to trap changes for all sheets in the ThisWorkbook module,
use the workbook's SheetChange event:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As
Range)
' your code here
End Sub
In this procedure, Sh references the worksheet on which the change
occurred, and Target references the cells on Sh that were changed.
This will trap changes on any sheet in the workbook (but not in other
workbooks).
For much more information about event procedures, see
http://www.cpearson.com/excel/Events.aspx .
Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
On Sun, 24 Jan 2010 13:13:01 -0800, Philosophaie
<Philosophaie@discussions.microsoft.com> wrote:
>Trying to get Wooksheet_Change to work:
>
>Public rng As Range
>Private Sub Workbook_Open()
> Dim range1 As Range
> Set range1 = Sheet4.Range("D4:F500")
> Set rng = range1
>
>'I need a way to access the subroutine Worksheet_Change
>'to fire the object Target range. I tried:
>'Worksheet_Change(rng) that didn't work.
>
>End Sub
>Private Sub Worksheet_Change(ByVal Target As Range)
>
>End Sub
|
|
0
|
|
|
|
Reply
|
Chip
|
1/24/2010 10:23:19 PM
|
|
I am trying to set the Target from the Workbook_Open subroutine so the
Worksheet_Change will fire when the cell in that range has been changed. I
need a way to get the object in Workbook_Open subroutine to set the object in
Worksheet_Change to initiallize Target range.
|
|
0
|
|
|
|
Reply
|
Utf
|
1/24/2010 10:36:01 PM
|
|
"Bernard Liengme" wrote:
> This code works
>
> Sub Worksheet_Change(ByVal Target As Range)
> If Not Intersect(Target, Range("D4:F500")) Is Nothing Then
> MsgBox "Cell in D4:F500 has be changed"
> End If
> End Sub
>
> Remember it must be added to the Sheet model not the Genaeal module
> One way is to right click the sheet's tab and select View Code
>
> best wishes
> --
> Bernard Liengme
> Microsoft Excel MVP
> http://people.stfx.ca/bliengme
I already have this in my code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim trgt As Range
Dim intersect As Range
Set intersect = Application.intersect(trgt, Target)
If Not intersect Is Nothing Then
With Sheets("Sheet4")
For n = 2 To 500
Cells(n, 5) = .cell((n - 1), 5) + .cell(n, 6) - .cell(n, 4)
Next n
End With
End If
End Sub
|
|
0
|
|
|
|
Reply
|
Utf
|
1/24/2010 10:43:01 PM
|
|
I would like to set the Target from the Workbook_Open subroutine so the
Worksheet_Change will fire when the cell in that range has been changed. I
need a way to get the object in Workbook_Open subroutine to set the object in
Worksheet_Change to initiallize Target range.
|
|
0
|
|
|
|
Reply
|
Utf
|
1/24/2010 10:54:01 PM
|
|
This has been answered several times. Do NOT (!) put your
Worksheet_Change code in the ThisWorkbook code module. It MUST reside
in the code module of the worksheet whose changes you want to trap.
You don't call Worksheet_Change directly; Excel calls it automatically
when a cell value is changed. It calls the Change event code that is
in its own code module. If you put Worksheet_Change in the
ThisWorkbook module, Excel doesn't recognize it as an event procedure
and will not execute it.
Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
On Sun, 24 Jan 2010 14:54:01 -0800, Philosophaie
<Philosophaie@discussions.microsoft.com> wrote:
>I would like to set the Target from the Workbook_Open subroutine so the
>Worksheet_Change will fire when the cell in that range has been changed. I
>need a way to get the object in Workbook_Open subroutine to set the object in
>Worksheet_Change to initiallize Target range.
|
|
0
|
|
|
|
Reply
|
Chip
|
1/24/2010 11:39:44 PM
|
|
You have all my code. Why can't I figure out why the Worksheet_Change is not
firing when a cell in the range is manipulated? I do save and exit the
workbook before I run the updated program and I am using a xls file in Excel
2007.
|
|
0
|
|
|
|
Reply
|
Utf
|
1/25/2010 1:59:01 AM
|
|
In this code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim trgt As Range
Dim intersect As Range
Set intersect = Application.intersect(trgt, Target)
If Not intersect Is Nothing Then
With Sheets("Sheet4")
For n = 2 To 500
Cells(n, 5) = .cell((n - 1), 5) + .cell(n, 6) - .cell(n, 4)
Next n
End With
End If
End Sub
trgt is never set to anything.
Are you trying to pass that range from the workbook_open event to that sheet's
_change event?
If yes, you could create a new module (Insert|Module) and put this into the
module:
Option Explicit
Public trgt as range
Then in the ThisWorkbook module, change your workbook_open procedure to use that
public variable:
Private Sub Workbook_Open()
Set trgt = Sheet4.Range("D4:F500")
Since trgt is declared in a General Module, every procedure in every module can
see it.
Then make sure you delete the declaration in Sheet4's worksheet module. So the
code becomes:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myIntersect As Range 'don't use a variable named Intersect!
Dim N as long
Set myintersect = Application.intersect(trgt, Target)
If Not myintersect Is Nothing Then
With Me 'the sheet owning the code.
'stop this event from firing itself
application.enableevents = false
For n = 2 To 500
'added a leading dot to the first cells() reference
.Cells(n, 5) = .cell((n - 1), 5) + .cell(n, 6) - .cell(n, 4)
Next n
application.enableevents = true
End With
End If
End Sub
==========
If you wanted this event to fire each time you opened the workbook, then you
could just change a value to itself in the range you want to inspect.
In the ThisWorkbook module:
Private Sub Workbook_Open()
with sheet4
Set trgt = .Range("D4:F500")
with .range("d4")
.value = .value
end with
end with
End Sub
================
You could call the worksheet_Change event in sheet4 IF you make a change to the
procedure statement:
Private Sub Worksheet_Change(ByVal Target As Range)
becomes
Sub Worksheet_Change(ByVal Target As Range)
And the code would look like:
Private Sub Workbook_Open()
with sheet4
Set trgt = .Range("D4:F500")
Call .Worksheet_Change(Target:=.Range("d4"))
end with
End Sub
Philosophaie wrote:
>
> "Bernard Liengme" wrote:
>
> > This code works
> >
> > Sub Worksheet_Change(ByVal Target As Range)
> > If Not Intersect(Target, Range("D4:F500")) Is Nothing Then
> > MsgBox "Cell in D4:F500 has be changed"
> > End If
> > End Sub
> >
> > Remember it must be added to the Sheet model not the Genaeal module
> > One way is to right click the sheet's tab and select View Code
> >
> > best wishes
> > --
> > Bernard Liengme
> > Microsoft Excel MVP
> > http://people.stfx.ca/bliengme
>
> I already have this in my code:
>
> Private Sub Worksheet_Change(ByVal Target As Range)
> Dim trgt As Range
> Dim intersect As Range
> Set intersect = Application.intersect(trgt, Target)
> If Not intersect Is Nothing Then
> With Sheets("Sheet4")
> For n = 2 To 500
> Cells(n, 5) = .cell((n - 1), 5) + .cell(n, 6) - .cell(n, 4)
> Next n
> End With
> End If
> End Sub
--
Dave Peterson
|
|
0
|
|
|
|
Reply
|
Dave
|
1/25/2010 4:07:11 AM
|
|
For the nth time, I will repeat that the Worksheet_Change event
procedure must NOT (!!!!!!!) be in the ThisWorkbook code module. If it
is, it will not be recognized by VBA and will not run when a cell
value is changed. The Worksheet_Change event MUST (!!!!!!) go in the
sheet module of the worksheet whose changes are to trapped. In Excel,
right-click on the tab of the sheet whose changes you want to trap,
and choose "View Code". Put your that code module. Again, do NOT (!)
put the Worksheet_Change event code in the ThisWorkbook module.
Maybe you don't understand what events are. See
http://www.cpearson.com/excel/Events.aspx for much more detail about
events, how they work, and how to code them.
Finally, again, do NOT put the Worksheet_Change event in the
ThisWorkbook module. This will NOT work.
Cordially,
Chip Pearson
Microsoft MVP 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com
[email on web site]
On Sun, 24 Jan 2010 17:59:01 -0800, Philosophaie
<Philosophaie@discussions.microsoft.com> wrote:
>You have all my code. Why can't I figure out why the Worksheet_Change is not
>firing when a cell in the range is manipulated? I do save and exit the
>workbook before I run the updated program and I am using a xls file in Excel
>2007.
|
|
0
|
|
|
|
Reply
|
Chip
|
1/26/2010 4:49:19 PM
|
|
Chip
Remember the Paul Newman movie "Cool hand Luke" and the guard said to Luke,
"What we have here is a failure to cummunikate (sp)
--
Don Guillett
Microsoft MVP Excel
SalesAid Software
dguillett@gmail.com
"Chip Pearson" <chip@cpearson.com> wrote in message
news:5u6ul512jvjvucp2pe924bulrfdduecuf5@4ax.com...
> For the nth time, I will repeat that the Worksheet_Change event
> procedure must NOT (!!!!!!!) be in the ThisWorkbook code module. If it
> is, it will not be recognized by VBA and will not run when a cell
> value is changed. The Worksheet_Change event MUST (!!!!!!) go in the
> sheet module of the worksheet whose changes are to trapped. In Excel,
> right-click on the tab of the sheet whose changes you want to trap,
> and choose "View Code". Put your that code module. Again, do NOT (!)
> put the Worksheet_Change event code in the ThisWorkbook module.
>
> Maybe you don't understand what events are. See
> http://www.cpearson.com/excel/Events.aspx for much more detail about
> events, how they work, and how to code them.
>
> Finally, again, do NOT put the Worksheet_Change event in the
> ThisWorkbook module. This will NOT work.
>
> Cordially,
> Chip Pearson
> Microsoft MVP 1998 - 2010
> Pearson Software Consulting, LLC
> www.cpearson.com
> [email on web site]
>
>
>
> On Sun, 24 Jan 2010 17:59:01 -0800, Philosophaie
> <Philosophaie@discussions.microsoft.com> wrote:
>
>>You have all my code. Why can't I figure out why the Worksheet_Change is
>>not
>>firing when a cell in the range is manipulated? I do save and exit the
>>workbook before I run the updated program and I am using a xls file in
>>Excel
>>2007.
|
|
0
|
|
|
|
Reply
|
Don
|
1/26/2010 7:39:42 PM
|
|
|
11 Replies
169 Views
(page loaded in 0.214 seconds)
Similiar Articles: Copy Pasting multiple columns - microsoft.public.mac.office.excel ...... select range "I:I" of sheet 1 of workbook source copy range (range "I:I" of sheet 1 of workbook source) open target activate object workbook target select (range ... Copy formatted text but not headers/footers - microsoft.public ...... ADD TEXT HERE||" ' Copy from source to clipboard docSource.Activate docSource.Sections(x).Range.Copy ' Paste from clipboard to target docTarget.Activate ... Macro - onAction arguments - microsoft.public.excel.worksheet ...... Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range ... MsgBox Application.CommandBars.ActionControl.Caption thisworkbook.activate ... how to add a running word count - microsoft.public.word ...... Target As Document Dim rngTarget As Range Dim strPages As String Dim arrPagee As Variant Set Source = ActiveDocument Set Target = Documents.Add Source.Activate ... MSCOMM32 Control in Excel 2003 VBA - microsoft.public.excel ...Private Sub Worksheet_SelectionChange(ByVal Target As Range) MSComm1.Enabled = True 'MSComm1.PortOpen = False MSComm1.Activate 'MSComm1.PortOpen = False 'MSComm1 ... How to run macros from combobox selection - microsoft.public.excel ...Example one: Private Sub Worksheet_Change(ByVal Target As Range) Dim Addx As ... code > > into the window that opens: > > > > Private Sub Worksheet_Activate ... Timestamp a cell when row is updated. - microsoft.public.excel ...... Target.Cells.count > 1 Or IsEmpty(Target) Then Exit Sub If Not Intersect(Target, Range ... is coloured as red..You can change that to suit Private Sub Worksheet_Activate ... Switching between documents and windows - microsoft.public.word ...I can activate it easily enough by Windows("Main.docx ... For example: TargetDoc.Sections(1).Footers(1).Range ... Target.docx via >Documents.Open FileName:="""Target ... Excel 2007 Chart colorindex? - microsoft.public.excel.programming ...... ca/bliengme Sub Macro1() ActiveSheet.ChartObjects("Chart 1").Activate ... How do I, under 2007, pick up the "fill" information out of the Target range ... Run-Time error '13': Type mismatch - microsoft.public ...... programming is limited): Private Sub Worksheet_SelectionChange(ByVal Target As Range ... cell A1 and protect the whole spreadsheet: > >Private Sub Worksheet_Activate ... Minimum row height - microsoft.public.excel.programming ...How do I activate or call the ChangeHeight procedure? Sorry for the newbie ... >> >> Private Sub Worksheet_Change(ByVal Target As Range) >> Const WS_RANGE As String = "A1 ... Add a paragraph mark at the end of every line? - microsoft.public ...... Error GoTo LastLine > With source > .Activate > Selection.HomeKey wdStory > Do While .Bookmarks("\line").Range.End <> .Range.End > target.Range ... SumProduct in VBA - Allow the user to select the file - microsoft ...... year code is replaced with 2010 > > Private Sub Worksheet_Change(ByVal Target As Range ... fname = Tarwk.Name shname = Tarwk.Worksheets(1).Name Acwk.Activate Worksheets ... Picture scroll with active cell - microsoft.public.excel ...Option Explicit Private Sub Worksheet_SelectionChange(ByVal Target As Range ... Scroll With Vba To Get Active Cell Near Top Of Window - If I activate a cell with ... Private Sub Workbook_Open() - microsoft.public.excel.programming ...... Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range ... Open() Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets ws.Activate ws ... Macro - Copy row base on criteria - microsoft.public.excel.misc ...... and condition format in both the source and target ... Count, "L").End(xlUp).Row To 4 Step -1 > If ws1.Range ... will copy all rows from the first sheet ... ws1.activate ... Locating Source Data in Master Spreadsheet - microsoft.public ...... ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Activate ... bird's eye view of all of the > childsheets' target ... How to identify Chart Source data as range? - microsoft ... Target Range Activation Excel - Excel Discussion List Wednesday ...Trying to get Wooksheet_Change to work: Public rng As Range Private Sub Workbook_Open() Dim range1 As Range Set range1 = Sheet4.Range(D4:F500) Set rng = Target Activation - Microsoft Corporation: Software, Smartphones ...Changing a cell in rng does not start the Sub Worksheet_Change(ByVal Target As Range). How does the "Target" get the data from Public rng? From my ... Target.com : Furniture, Baby, Electronics, Toys,...: TargetExpect more pay less with Target. Spend $50, get free shipping on over 500K items. Choose from a wide selection of furniture, baby, electronics, toys, shoes, a Event Macros, Worksheet Events and Workbook EventsPrivate Sub Worksheet_Change(ByVal Target As Excel.Range) If Target.Column <> 4 Then Exit Sub Application.EnableEvents = False Target.Activate Application.Run ... Excel Data Validation Won't Activate Worksheet_Change(ByVal Target ...Excel tips and Excel help from the MrExcel Message Board regarding Data Validation Won't Activate Worksheet_Change(ByVal Target As Range) Excel VBA: No Experience Necessary - Mindless Fun(Activate, BeforeDoubleClick, BeforeRightClick, etc.) Select the Activate ... the Worksheet_SelectionChange subroutine has a parameter called Target which is a Range ... Range Object (Excel) - Microsoft Corporation: Software ...Use the Activate method to activate a worksheet before you use the Range property without an explicit ... the source and target worksheets, and the source and target ranges ... Chapter 5: Using Ranges - Microsoft Corporation: Software ...If the top-left cell of the range you activate is not in the current selection, the ... It then uses the Intersect method, in the If test, to see if the Target range, which ... Walkie Talkie Radios at Target - Target.com : Furniture, Baby ...Shop for Walkie Talkie Radios at Target ... Average rating for Motorola Talkabout Two-Way Radio with 35 Mile Range - Black ... Worksheet Change Event: Run Excel Macros When a Cell Changes Or ...If Not Intersect(Target, Range("A1:A10")) Is Nothing Then. If our range of cells to monitor is a non-contiguous range we would use; If Not Intersect(Target, Range("A1,C1,E1 ... 7/22/2012 6:25:47 AM
|