Solved

Excel 2010 – Call a subroutine from a macro

Posted on 2011-03-15
5
1,134 Views
Last Modified: 2012-05-11
I wrote (borrowed) some simple VB to take a variable from a text box in an Excel sheet and perform a search on the same sheet.  It works fine when I used a command button to run it, but I want to run it from a macro and can’t seem to get it to work.
The command buttons look old fashon and cluncky as compared to the shapes that can be created with
Excel 2010.  The shapes can only be used as buttons to run macros, however. So I want a macro to call the code to do the search.

(I saved the file down to Excel 2003 since not everyone has 2010)

This is in Module 1
Sub RoundedRectangle2_Click()
     Call FindProject
   'Application.Run "FindProject"
End Sub

This is in Sheet1 module
Public Sub CommandButton1_Click()
   ‘ This runs fine from the Command button

           Dim strFindWhat As String
           strFindWhat = TextBox1.Text
           On Error GoTo ErrorMessage

    Cells.Find(What:=strFindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Select
    Exit Sub
ErrorMessage:
                  MsgBox ("The data you are searching for does not exist")

End Sub

Private Sub FindProject()
  ‘ This code was copied from above and I try use a macro to call this sub – it doesn’t work

           Dim strFindWhat As String
           strFindWhat = TextBox1.Text
           On Error GoTo ErrorMessage

    Cells.Find(What:=strFindWhat, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
        :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
        False).Select
    Exit Sub
ErrorMessage:
                  MsgBox ("The data you are searching for does not exist")

End

Open in new window

SearchTest.xls
0
Comment
Question by:upsfa
  • 3
5 Comments
 
LVL 9

Expert Comment

by:McOz
ID: 35141096
You need to end the sub with "End Sub".

Cheers
0
 
LVL 1

Author Comment

by:upsfa
ID: 35141422
Sorry, cut and paste error.  My code does have End Sub int it.
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 500 total points
ID: 35141535
change

Call FindProject

to

Call Sheet1.FindProject
0
 
LVL 1

Author Comment

by:upsfa
ID: 35147173
Perfect!  Thank you.
0
 
LVL 1

Author Closing Comment

by:upsfa
ID: 35147175
Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question