Passing values between three subroutines

AndyTampa

Board Regular
Joined
Aug 14, 2011
Messages
199
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I'm trying to pass a value between subs that can be used by other subs. I was able to pass a value to one sub, but since I may get that value to use in a few subs, I created a subroutine to get that value. But it won't get the value and pass it to the main macro.

In this macro, I want to run the subroutine LastRow to get the last row of data and pass that value to MAIN. Then I want MAIN to pass the value to TWO. I want to be able to use both LastRow and TWO in other macros too. When I put the entire LastRow code into MAIN, the variable is passed to TWO without a problem. How can I get LastRow to pass the variable to MAIN first?

VBA Code:
Sub MAIN()

   Dim LRow As Long

   LastRow (LRow)

   TWO (LRow)

'a whole bunch of stuff

End Sub
-------------
Sub LastRow(LRow)

  LRow = Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row

End Sub
-----------
Sub TWO(LRow)

'more stuff

End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
the easy way is to define lrow at the module level so that it is retained between calls like this:
VBA Code:
   Dim LRow As Long   ' put this right at the top of the module

Sub MAIN()


Call LastRow
MsgBox LRow & " Main"
Call TWO

'a whole bunch of stuff

End Sub
Sub LastRow()

  LRow = Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row
 MsgBox LRow & " Lastrow "
End Sub

Sub TWO()
MsgBox LRow & "two"
'more stuff

End Sub
 
Upvote 0
Hi,
I suspect that your LastRow sub should be a Function that returns the value to the main sub - you can then pass the returned value to other subs as required

VBA Code:
Sub MAIN()

   Dim LRow As Long

   LRow = LastRow
  
   MsgBox LRow

   TWO LRow

'a whole bunch of stuff

End Sub
'-------------
Function LastRow() As Long

  LastRow = Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row

End Function
'-----------
Sub TWO(ByVal LRow As Long)

MsgBox LRow

'more stuff

End Sub

Dave
 
Upvote 0
Solution
offthelip, VB will not allow me to place the DIM before the SUB.

Dave, that worked. Thanks.
 
Upvote 0
offthelip, VB will not allow me to place the DIM before the SUB.

Dave, that worked. Thanks.
offthelip solution should work ok but you need to place the variable at very TOP of the module as indicated

Glad we were able to help & appreciate feedback

Dave
 
Upvote 0
OOOOHHHH! The module and not the subroutine? I have a Public Function above that subroutine. I'd have to place the DIM statement above that, in a different macro?
 
Upvote 0
Yes the very first line in the module, you will find vba automatically puts a dividing line underneath it
Module.JPG
 
Upvote 0
So if the very top of my module looks like this:
VBA Code:
Public Function UName()
    UName = Environ$("USERNAME")
End Function
--------------------------------------
Sub ONE()

  Dim LRow As Long, RNum As Long, ILBDTLErr As Long, InvEnt As Long, BLKErr As Long, ADJErr As Long, BPErr As Long, VALNum As Long, ErrQty As Long
  
'------------------
'| FIND LAST ROW  |
'------------------
   LRow = LastRow

And the two subroutines below ONE() are:

VBA Code:
Function LastRow() As Long

  LastRow = Cells.Find(What:="*", After:=Range("A1"), LookAt:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, MatchCase:=False).Row

End Function
-------------------------------
Sub TWO(ByVal LRow As Long)

Does moving the DIM line to the top of the module function for every subroutine in the module?
Do I still have to pass LRow as a variable to Sub TWO or can I simply call LastRow without adding the LRow = LastRow line to my Sub ONE?
 
Upvote 0
My solution changed your LastRow Sub to a Function - A Function RETURNS a value to the calling procedure. In the example given, there is no need to have a variable at top of module as you can pass the returned value to other procedures from the calling procedure as shown in my post.

The solution posted by @offthelip retains your codes as a Sub but the value of the variable is held at Module level (outside any procedure) Variables declared with Dim statement at the module level are available to all procedures within that module.

Either solution should work for you

Dave
 
Upvote 0

Forum statistics

Threads
1,215,556
Messages
6,125,495
Members
449,235
Latest member
Terra0013

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