Reference variable between two modules

jakobt

Active Member
Joined
May 31, 2010
Messages
337
I have two subroutines in the same module named:
Sub JT()
Sub TJ()


in Sub TJ I want to use one of the variables I defined in sub JT()
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
What is the variable name & what type of variable is it?
 
Upvote 0
Again it is part of a bigger code but I want to use the variable "LR" defined in sub jt() and to be used in sub tj()
sub jt()
dim lr as long
lr= activesheet.cells(Activesheet.rows.count, "B").End(xlup).row
end sub

sub tj()
dim rng as range: set rng = range("v4:v50")
with range("F" & lr + 9).resize(rng.rows.count)
.value =rng.value
.removeduplicates columns:=1, header:=xlno
.sort key1:=range("F" & lr + 9), order1:xlascending, header:=xlno
end with
end sub
 
Last edited:
Upvote 0
If you just want it to get the last row, you're probably better off with a function
Code:
Function GetLastRow(Colm As Long, Optional Ws As Worksheet) As Long
   If Ws Is Nothing Then Set Ws = ActiveSheet
   GetLastRow = Ws.Cells(Ws.Rows.Count, Colm).End(xlUp).Row
End Function
Which you can call like
Code:
Sub tj()
   Dim rng As Range: Set rng = Range("v4:v50")
   Dim lr As Long
   
   lr = GetLastRow(2)
   With Range("F" & lr + 9).Resize(rng.Rows.Count)
      .Value = rng.Value
      .RemoveDuplicates Columns:=1, Header:=xlNo
      .Sort key1:=Range("F" & lr + 9), order1:=xlAscending, Header:=xlNo
   End With
End Sub
Or you can include the sheet name like
Code:
Sub tj()
   Dim rng As Range: Set rng = Range("v4:v50")
   Dim lr As Long
   
   lr = GetLastRow(2, Sheets("sheet2"))
   With Range("F" & lr + 9).Resize(rng.Rows.Count)
      .Value = rng.Value
      .RemoveDuplicates Columns:=1, Header:=xlNo
      .Sort key1:=Range("F" & lr + 9), order1:=xlAscending, Header:=xlNo
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,206
Members
452,618
Latest member
Tam84

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