ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Creating a Sub Function (https://www.excelbanter.com/excel-programming/298723-creating-sub-function.html)

No Name

Creating a Sub Function
 
How do I create a function that is sent one variable. I
want it be sent a variable called i and another FinalRow.
Then I want it to do it's thing. I want to be able to
call this function at anytime in my macro by just typing
FunctionName(i,FinalRow). Here what I want to be included
in my function

n = i
For n = i To FinalRow
If (Range("A" & n).Value) = "" And (Range("B" & n).Value)
= 1 And (Range("C" & n).Value) = 1 And (Range("D" &
n).Value) = "" Then
Rows(n & ":" & n).Cut
Rows(i & ":" & i).Insert Shift:=xlDown
End If
Next n

Thank you


Township of East Hanover

Creating a Sub Function
 
Public Sub MySub(I As Integer, FinalRow As Integer)
Your Code Here
End Sub

Then to call it:

MySub(I,FinalRow)

I would put the sub in a module for example Module1 then call it this way.

Module1.MySub(I,FinalRow)

wrote in message
...
How do I create a function that is sent one variable. I
want it be sent a variable called i and another FinalRow.
Then I want it to do it's thing. I want to be able to
call this function at anytime in my macro by just typing
FunctionName(i,FinalRow). Here what I want to be included
in my function

n = i
For n = i To FinalRow
If (Range("A" & n).Value) = "" And (Range("B" & n).Value)
= 1 And (Range("C" & n).Value) = 1 And (Range("D" &
n).Value) = "" Then
Rows(n & ":" & n).Cut
Rows(i & ":" & i).Insert Shift:=xlDown
End If
Next n

Thank you




Harald Staff

Creating a Sub Function
 
"Township of East Hanover" skrev i melding
...
Public Sub MySub(I As Integer, FinalRow As Integer)
Your Code Here
End Sub

Then to call it:

MySub(I,FinalRow)


It's usually either

MySub I,FinalRow

or

Call MySub (I,FinalRow)

They do the same, question of style and taste only. But
MySub(I,FinalRow)
should produce an error.

HTH. Best wishes Harald




All times are GMT +1. The time now is 12:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com