Solved

Excel 2010 – Call a subroutine from a macro

Posted on 2011-03-15
5
1,103 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now