PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 1.00 average.

Call Subroutine in Excel

 
 
DogLover
Guest
Posts: n/a
 
      4th Nov 2009
Please help. I am trying to set up a subroutine that I can call several
times to eliminate having to type this code a bunch of times. The
subroutine that I would like to call is this Newtest. The subroutines where
it is used and Newtest are actually all in the same workbook.

Public Sub Newtest_Click()
Dim NewFillRange, lookuprng As Range
Dim EntityVar, NewRange As String
Dim StartRow, EndRow As Integer

If CheckBoxEntity.Value = False Then Set NewFillRange =
Worksheets("Demo").Range("DemoDept")
If CheckBoxEntity.Value = True Then Set NewFillRange =
Worksheets("Demo").Range("DemoDeptByEntity")

If CheckBoxDept.Value = True Then
ComboBoxDept.Visible = True
If ComboBoxEntity.Value = "<>" Then Set NewFillRange =
Worksheets("Demo").Range("DemoDept") 'Show ALL depts

With Worksheets("RFJ").ComboBoxDept
.ListFillRange = NewFillRange.Address(external:=True)
End With


Application.Goto Reference:="R12C14"
Application.Goto Reference:="R5C4"

' All Departments Selected
ElseIf CheckBoxDept.Value = False Then
ComboBoxDept.Visible = False
Application.Goto Reference:="R12C14"
ActiveCell.FormulaR1C1 = "<>"
Application.Goto Reference:="R5C4"
End If
End Sub

Where do you put a Public subroutine? It it in the "This Workbook"? How do
you call it in a subroutine? I tried application.Run "Workbookname!Newtest",
but got an error regarding a macro.


 
Reply With Quote
 
 
 
 
Sam Wilson
Guest
Posts: n/a
 
      4th Nov 2009
Put them in a module.

In the VB window, right click your project and insert module. These two
macros show you how to call a piece of code, as the second calls the first:
Sub test()

MsgBox "Hello"

End Sub

Sub test2()

Call test

End Sub


"DogLover" wrote:

> Please help. I am trying to set up a subroutine that I can call several
> times to eliminate having to type this code a bunch of times. The
> subroutine that I would like to call is this Newtest. The subroutines where
> it is used and Newtest are actually all in the same workbook.
>
> Public Sub Newtest_Click()
> Dim NewFillRange, lookuprng As Range
> Dim EntityVar, NewRange As String
> Dim StartRow, EndRow As Integer
>
> If CheckBoxEntity.Value = False Then Set NewFillRange =
> Worksheets("Demo").Range("DemoDept")
> If CheckBoxEntity.Value = True Then Set NewFillRange =
> Worksheets("Demo").Range("DemoDeptByEntity")
>
> If CheckBoxDept.Value = True Then
> ComboBoxDept.Visible = True
> If ComboBoxEntity.Value = "<>" Then Set NewFillRange =
> Worksheets("Demo").Range("DemoDept") 'Show ALL depts
>
> With Worksheets("RFJ").ComboBoxDept
> .ListFillRange = NewFillRange.Address(external:=True)
> End With
>
>
> Application.Goto Reference:="R12C14"
> Application.Goto Reference:="R5C4"
>
> ' All Departments Selected
> ElseIf CheckBoxDept.Value = False Then
> ComboBoxDept.Visible = False
> Application.Goto Reference:="R12C14"
> ActiveCell.FormulaR1C1 = "<>"
> Application.Goto Reference:="R5C4"
> End If
> End Sub
>
> Where do you put a Public subroutine? It it in the "This Workbook"? How do
> you call it in a subroutine? I tried application.Run "Workbookname!Newtest",
> but got an error regarding a macro.
>
>

 
Reply With Quote
 
DogLover
Guest
Posts: n/a
 
      4th Nov 2009
I see how that part works. However, I get an error when my module to the code

If CheckBoxEntity.Value = False Then Set NewFillRange =
Worksheets("Demo").Range("DemoDept")

I think I may need to pass CheckBoxEntity.Value to the subroutine from the
call statement, but I'm not sure.


"Sam Wilson" wrote:

> Put them in a module.
>
> In the VB window, right click your project and insert module. These two
> macros show you how to call a piece of code, as the second calls the first:
> Sub test()
>
> MsgBox "Hello"
>
> End Sub
>
> Sub test2()
>
> Call test
>
> End Sub
>
>
> "DogLover" wrote:
>
> > Please help. I am trying to set up a subroutine that I can call several
> > times to eliminate having to type this code a bunch of times. The
> > subroutine that I would like to call is this Newtest. The subroutines where
> > it is used and Newtest are actually all in the same workbook.
> >
> > Public Sub Newtest_Click()
> > Dim NewFillRange, lookuprng As Range
> > Dim EntityVar, NewRange As String
> > Dim StartRow, EndRow As Integer
> >
> > If CheckBoxEntity.Value = False Then Set NewFillRange =
> > Worksheets("Demo").Range("DemoDept")
> > If CheckBoxEntity.Value = True Then Set NewFillRange =
> > Worksheets("Demo").Range("DemoDeptByEntity")
> >
> > If CheckBoxDept.Value = True Then
> > ComboBoxDept.Visible = True
> > If ComboBoxEntity.Value = "<>" Then Set NewFillRange =
> > Worksheets("Demo").Range("DemoDept") 'Show ALL depts
> >
> > With Worksheets("RFJ").ComboBoxDept
> > .ListFillRange = NewFillRange.Address(external:=True)
> > End With
> >
> >
> > Application.Goto Reference:="R12C14"
> > Application.Goto Reference:="R5C4"
> >
> > ' All Departments Selected
> > ElseIf CheckBoxDept.Value = False Then
> > ComboBoxDept.Visible = False
> > Application.Goto Reference:="R12C14"
> > ActiveCell.FormulaR1C1 = "<>"
> > Application.Goto Reference:="R5C4"
> > End If
> > End Sub
> >
> > Where do you put a Public subroutine? It it in the "This Workbook"? How do
> > you call it in a subroutine? I tried application.Run "Workbookname!Newtest",
> > but got an error regarding a macro.
> >
> >

 
Reply With Quote
 
Sam Wilson
Guest
Posts: n/a
 
      5th Nov 2009
You can pass variables as follows:

Sub test(byval strDemo as string)

MsgBox "Hello " & strDemo

End Sub

Sub test2()

dim myStr as string
myStr = "Sam"

Call test(myStr)

End Sub


"DogLover" wrote:

> I see how that part works. However, I get an error when my module to the code
>
> If CheckBoxEntity.Value = False Then Set NewFillRange =
> Worksheets("Demo").Range("DemoDept")
>
> I think I may need to pass CheckBoxEntity.Value to the subroutine from the
> call statement, but I'm not sure.
>
>
> "Sam Wilson" wrote:
>
> > Put them in a module.
> >
> > In the VB window, right click your project and insert module. These two
> > macros show you how to call a piece of code, as the second calls the first:
> > Sub test()
> >
> > MsgBox "Hello"
> >
> > End Sub
> >
> > Sub test2()
> >
> > Call test
> >
> > End Sub
> >
> >
> > "DogLover" wrote:
> >
> > > Please help. I am trying to set up a subroutine that I can call several
> > > times to eliminate having to type this code a bunch of times. The
> > > subroutine that I would like to call is this Newtest. The subroutines where
> > > it is used and Newtest are actually all in the same workbook.
> > >
> > > Public Sub Newtest_Click()
> > > Dim NewFillRange, lookuprng As Range
> > > Dim EntityVar, NewRange As String
> > > Dim StartRow, EndRow As Integer
> > >
> > > If CheckBoxEntity.Value = False Then Set NewFillRange =
> > > Worksheets("Demo").Range("DemoDept")
> > > If CheckBoxEntity.Value = True Then Set NewFillRange =
> > > Worksheets("Demo").Range("DemoDeptByEntity")
> > >
> > > If CheckBoxDept.Value = True Then
> > > ComboBoxDept.Visible = True
> > > If ComboBoxEntity.Value = "<>" Then Set NewFillRange =
> > > Worksheets("Demo").Range("DemoDept") 'Show ALL depts
> > >
> > > With Worksheets("RFJ").ComboBoxDept
> > > .ListFillRange = NewFillRange.Address(external:=True)
> > > End With
> > >
> > >
> > > Application.Goto Reference:="R12C14"
> > > Application.Goto Reference:="R5C4"
> > >
> > > ' All Departments Selected
> > > ElseIf CheckBoxDept.Value = False Then
> > > ComboBoxDept.Visible = False
> > > Application.Goto Reference:="R12C14"
> > > ActiveCell.FormulaR1C1 = "<>"
> > > Application.Goto Reference:="R5C4"
> > > End If
> > > End Sub
> > >
> > > Where do you put a Public subroutine? It it in the "This Workbook"? How do
> > > you call it in a subroutine? I tried application.Run "Workbookname!Newtest",
> > > but got an error regarding a macro.
> > >
> > >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call a subroutine using variable subroutine name dhstein Microsoft Excel Misc 3 26th Jul 2009 08:28 PM
call Excel subroutine from Sql Server? news.microsoft.com Microsoft Excel Programming 0 17th Sep 2008 06:31 PM
Can't call a subroutine donwb Microsoft Excel Programming 3 6th Aug 2008 05:05 AM
Call Subx for excel 97 Add-In subroutine Bob Phillips Microsoft Excel Programming 4 2nd Mar 2005 08:05 PM
Call asp.net subroutine from javascript Christophe Microsoft ASP .NET 1 25th Mar 2004 09:02 AM


Features
 

Advertising
 

Newsgroups
 


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