This question already has an answer here:
Dynamic Function Calls in Excel VBA
(1 answer)
Closed 7 years ago.
This is my test code
Sub dotask()
Dim qusub As String
qusub = Worksheets("Task List").Range("C2").Value
MsgBox qusub
Application.Run qusub
End Sub
Sub msg1()
MsgBox "sub msg1"
End Sub
Sub msg2()
MsgBox "sub msg2"
End Sub
Sub msg3()
MsgBox "sub msg3"
End Sub
Sub msg4()
MsgBox "sub msg4"
End Sub
All of this is contained in a single standard module. I've read Trying to call a Sub with a String - VBA and wrote my code according to what I found there (i.e. using Application.Run). Cell C2 of Task List worksheet contains "msg3" at the moment. When I execute sub "dotask" I first get a message box saying "msg3" as I want but then I get the following error message:
Run-time error '1004':
Cannot run the macro 'msg3'. The macro may not be available in this workbook or all macros may be disabled.
I'm working on Excel 2010 and the file is .xlsm - what should I do to get my code to execute as I want it?
just ran it over here. msg1 seems to be a reserved word... change it to something else and it works fine =)
Using GetRef, you give the reference to the sub.
See my question here for example
EDIT: following the suggestions in the comments, here part of the solution to this question.
sub one(para)
WScript.Echo para & " from one"
end sub
sub two(para)
WScript.Echo para & " from two"
end sub
sub main(subname, para)
Dim f : Set f = GetRef(subname)
f para
end sub
main "one", "test" '=>test from one
Related
I have a Macro (the name is Test() ) from the Module1, and normally to call this Macro, i am using the code :
Call Module1.Test
But i am looking to a VBA code to call my Macro Test from Module1 until certain lines of code
Example stop the runing when the macro is arriving to the line 900
Thanks for your help
I'm not sure, but I can guess that you might want to run the Test() subroutine in different parts:
Sub Test(Optional stop_at_900 = False)
Debug.Print vbTab & "do the first part of the code"
'other code
900: If stop_at_900 Then Exit Sub ' 900 label is not needed, it is placed here for labeling
Debug.Print vbTab & "do the rest of the code"
'other code
End Sub
Sub UsageExample()
Debug.Print "Doing full Test() code..." ' usual (full) use
Call Test
Debug.Print "Doing Test() code until 900 line..." '
Call Test(stop_at_900:=True) 'partial use
End Sub
I'm currently trying to create a Print Button on one of my worksheets. I need it to print that worksheet as well as another one. Both of the names are "Budget Sheet" and "Listed Commitments Sheet" without the quotation marks.
I created the button without hassle, but I know very little about Macros so I still need the code. I've tried multiple solutions, but nothing seems to work. I've recently tried to use this Code, but it hasn't worked. What am I doing wrong? What code could I possibly use instead?
Private Sub CommandButton1_Click()
Function PrintMultipleSheets()
Sheets(Array("Budget Sheet", "Listed Commitments Sheet")).PrintOut
End Function
End Sub
It comes up with an error that says "Compile Error: Expected End Sub".
The code doesn't compile, because you can't have a Function inside a Sub.
Get ride of the line Function PrintMultipleSheets() and get rid of End Function. It should work I think. You'll end up with:
Private Sub CommandButton1_Click()
Sheets(Array("Budget Sheet", "Listed Commitments Sheet")).PrintOut
End Sub
Just a simple loop with a print call:
Sub forEachWs()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Call printSheet(ws)
Next
End Sub
Function pasteContents(ws as Worksheet)
ActiveSheet.PrintOut
End Function
I have added a private sub worksheet_calculate() in a sheet called Main. I have a value in column AP with formulas derived from other sheets and if that number is greater than value in X I want to display a message as a warning that it's over, but the code is not working any suggestions why?
Private Sub Worksheet_Calculate()
If Sheets("Main").Range("AP7").value > Sheets("Main").Range("x7").value Then
MsgBox "You Are Over Pieces Suggested"
End If
End Sub
Try this.
Private Sub Worksheet_Calculate()
If Range("AP7").Value > Range("X7").Value Then
MsgBox "You Are Over Pieces Suggested."
End If
End Sub
EDITED####
Edited the original code to run as a Worksheet_Calculate rather than a Change.
Working on trying to set the ranges to columns for you now.
EDIT#########
I flippin love a challenge. Try This.
Private Sub Worksheet_Calculate()
Set Target = Range("AP:AP").SpecialCells(xlCellTypeFormulas)
If Target Is Nothing Then Exit Sub
For Each c In Target
If c > Range("X" & c.Row) Then
MsgBox "You Are Over Pieces Suggested - Cell " & "AP" & c.Row
End If
Next
End Sub
Consider using Data Validation on cell AP7 using a "Custom" formula of: =AP7<=$X$7
Fill in the Error Alert tab on the validation menu: Stop; "You Are Over Pieces Suggested". I think this might achieve what you want without any macros. In fact, it can prevent an invalid number from being entered in the first place.
I have a userform to call a macro in a separate module when a button is clicked. I get the following error: "Run-time error '450': Wrong number of arguments or invalid property assignment"
In troubleshooting I removed the arguments and changed the dummy macro I was calling to not take arguments, but I get the same error.
Public Sub btnSubmit_Click()
Dim Description As String
Dim Priority As String
If (checkCleared.Value = False) Then
MsgBox ("Please certify that all sensitive informationhas been removed and then submit")
Exit Sub
Else
'Description = formScreen.txtDesc.Value
'Priority = formScreen.comboPriority.Value
'Application.Run ThisOutlookSession!postScreenedEmail(Priority, Description)
Application.Run ThisOutlookSession!postScreenedEmail
End If
End Sub
In the separate module:
Public Sub postScreenedEmail() '(Priority As String, Description As String)
MsgBox ("postScreened")
'MsgBox ("Priority is: " & Priority & " and Description is " & Description)
End Sub
I have tried other methods of calling the macro such as "Call postScreenedEmail()" but it cant see the macro then. My end goal is just to grab values from the userform and pass them to the other macro so they can be used with the API I am working with.
Edit: I may have mixed my terminology, this is the hierarchy I am working with (can't post pic with my rep). That being said I tried to do the call with just Application.Run "postScreenedEmail", Priority, Description and it changed nothing
-Project1(VbaProject.OTM)
-Microsoft Outlook Objects
| ThisOutlookSession
-Forms
| formScreen
|
-Modules
Module1
Try:
call postScreenedEmail
instead of:
Application.Run ThisOutlookSession!postScreenedEmail
Since your sub is public, vba should be able to find it without the module reference.
If this works, add the reference again (makes your code more readable, especially for others, as ckuhn203 pointed out in the comments) and see if it breaks. If so, that's where the problem is.
EDIT:
Are you sure you're referencing the right module?
If I try:
-Project1(VbaProject.OTM)
-Microsoft Outlook Objects
| ThisOutlookSession
-Modules
| Module1
in Module1:
Sub jzz()
Debug.Print "test"
End Sub
and in ThisOutlookSession:
Sub test()
Call Module1.jzz
End Sub
it works. No problem. Using:
Application.Run Module1.jzz
instead of Call trows a compile error.
Even:
Sub test2()
Call ThisOutlookSession.test
End Sub
from Module1 works, without problems.
Can you run such small tests to try to get the references right?
Try this... Application.Run takes a string for procedure name, and then comma-separated list of parameters/arguments:
Application.Run "Procedure_Name", arg1, arg2, arg3
So I think this should work:
Application.Run "ThisOutlookSession!postScreenedEmail", Priority, Description
I am using data validation in excel 2007. I am using this code to make invalid data marked with red circle.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rc As Integer
Range(Target.Address).Select
ActiveSheet.ClearCircles
ActiveSheet.CircleInvalid
If Not Range(Target.Address).Validation.Value Then
rc = MsgBox("Data Validation errors exist! " & Range
(Target.Address).Validation.ErrorMessage & " Please correct circled entries!", vbCritical, "Failure")
Exit Sub
End If
End Sub
As you can see in the code when I put wrong data then first of that specific range is going to selected and then all invalid data is marked with red circle.
But I want that only that specific cell should be marked with red not all data .
Thanks.
You can try this code from an Excel MVP:
Dim TheCircledCell As Range
Sub CircleCells(CellToCircle As Range)
If Not CellToCircle Is Nothing Then
With CellToCircle
If .Count > 1 Then Exit Sub
Set TheCircledCell = CellToCircle
.Validation.Delete
.Validation.Add xlValidateTextLength, xlValidAlertInformation, xlEqual, 2147483647#
.Validation.IgnoreBlank = False
.Parent.CircleInvalid
End With
End If
End Sub
Sub ClearCircles()
If Not TheCircledCell Is Nothing Then
With TheCircledCell
.Validation.Delete
.Parent.ClearCircles
End With
End If
End Sub
Note that you can't use the Excel standard Validation function on these cells.
[Source and explanation of the code]