Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default Passing a range between two subroutines.

Could someone show me where I am going wrong.

[In "ThisWorkbook"]
Private Sub Workbook_Open()
Dim RowsEnd As Double
Dim Target As Range
RowsEnd = Cells(.Rows.Count, "E").End(xlUp).Row

'Not certain if correct
Target = "D27:F" & (RowsEnd + 25)

'Unsure if this will call Worksheet_Change
Worksheet_Change(Target as Range).

End Sub

Private Sub Worksheet_Change(Target as Range)
With Sheets("Sheet4")
For n = 2 To RowsEnd
.Cells(n, 5) = .cell((n - 1), 5) + .cell(n, 6) - .cell(n, 4)
Next n
End With
End Sub

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Passing a range between two subroutines.


Hello

I dont know of a way to run a worksheet change event like this, though
there shouldnt erealy be a need just create a sub and call that.

hope this makes sence.


Code:
--------------------
Option Explicit
'Dim will keep it private (only within the module of sheet level)
Dim Target
Dim RowsEnd As Long

'public will allow you to use the in other workbooks)
'Public Target
'Public RowsEnd As Long


'#####[In "ThisWorkbook"]
'Private Sub Workbook_Open():'changed out for testing
Sub Test()

With Sheets(1)
RowsEnd = Cells(.Rows.Count, "E").End(xlUp).Row
Target = "D27:F" & (RowsEnd + 25)
End With
'dont run the worksheet event run the sub (i dont think you can call a worksheet event, good chance i could be wrong)
SubOne
'or if the sub is located within a sheet (note will not work with native events)
Sheet1.SubTwo

End Sub

Sub SubOne()
Dim n As Long

With Sheets("Sheet1")
For n = 2 To RowsEnd
.Cells(n, 5) = .cell((n - 1), 5) + .cell(n, 6) - .cell(n, 4)
Next n
End With
End Sub


'### WorkSheet Code

Private Sub Worksheet_Change(Target As Range)
SubTwo
End Sub


Sub SubTwo()
Cells(1, 2).Value = "New Value"
End Sub

--------------------


cheers


--
D_Rennie
------------------------------------------------------------------------
D_Rennie's Profile: 1412
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=172761

Microsoft Office Help

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 110
Default Passing a range between two subroutines.

I would like to know how to get "Target as Range" to the Worksheet_Change
subroutine from the Workbook_Open.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Passing a range between two subroutines.

Ahh ok maby i dint understand what you where doing.

Though with the worksheet change event the TARGET is the range value
that is going to be changed, And to the best of my knoledge this cannon
be changed.

Maby you would be better off decribing why you think you need to do
this. and from there there may be a solution.,

cheers/.


--
D_Rennie
------------------------------------------------------------------------
D_Rennie's Profile: 1412
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=172761

Microsoft Office Help

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Passing a range between two subroutines.


The workbook open occurs before the worksheet change will take affect.

If you want to use the same code for both the worksheet open and a
change event then create a new subroutine in the module and call the new
routine from both the open and change events. You can pass a range to
the new subroutine


sub wokbook open

set MyRange = Sheet("sheet1").range("A1")
call common_sub(MyRange)
end sub
---------------------------------------
sub change event (target)

call common_sub(target)

end sub
-----------------------------------------

new sub in a module
sub common_sub(MyRange as Range)

put common code here

end sub


--
joel
------------------------------------------------------------------------
joel's Profile: 229
View this thread: http://www.thecodecage.com/forumz/sh...d.php?t=172761

Microsoft Office Help

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA - passing Variables to subroutines Madduck Excel Discussion (Misc queries) 12 September 19th 08 03:20 AM
Calling subroutines and passing variables matpj[_55_] Excel Programming 1 March 29th 06 04:22 PM
Calling subroutines and passing variables matpj[_56_] Excel Programming 0 March 29th 06 03:55 PM
Passing a Range Jerry Excel Programming 4 February 14th 05 10:52 AM
passing range to c# Jerry Excel Programming 0 January 29th 05 08:29 PM


All times are GMT +1. The time now is 09:11 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"