EXCEL VBA procedure ambigous name - vba

My VBA code is too large and I'm trying to make smaller SUBS so the error won't come up, but then the error "Ambigoius name" pops up. I've tried to rename my subs...
Ex.
Private Sub worksheet_calculate()
Range("I9").Interior.Color=Range("AK9").Display.Format.Interior.Color
end sub
Private Sub worksheet_calculate2()
Range("J9").Interior.Color=Range("AQ9").Display.Format.Interior.Color
end sub
...when I rename the other subs as shown in the example it doesn't do anything, only the original work properly. How do I rename them so they can work properly?

In my understanding, worksheet_calculate is the predefined name of the subroutine, triggered by the event when the worksheet is recalculated.
You can define and call other private subs from it.
like
Private Sub worksheet_calculate()
rem sub body
CalculateSub1 pars 'variant one
Call CalculateSub1(pars) 'variant two
rem sub body
End Sub
Sub CalculateSub1(pars)
Rem Sub body
End Sub

Just insert the below line of codes at the end of your main sub which is "worksheet_calculate"
Call worksheet_calculate2
Call worksheet_calculate3
Call worksheet_calculate4
Call worksheet_calculate4
.
.
.
Call worksheet_calculaten

Related

How do I get a reference to the Application object of a UserForm?

If I want to write a Sub that positions a UserForm relative to the Application object that displays it, How would I go about doing it?
I want to write a sub that goes like this:
Sub PositionForm(WhichForm as Object)
WhichForm.Left = <WhichForm Application Object>.Left
End Sub
I understand that there are many workarounds to this. I am interested in knowing whether there is a way to getting that reference.
in Excel the following works:
Sub PositionForm(WhichForm As Object)
WhichForm.Left = Application.Left
End Sub
to be called from any UserForm code as:
Private Sub CommandButton2_Click()
... any code
PositionForm Me
... any code
End Sub

Sub Not Defined Error: Calling a Macro in Another Sheet

I'm trying to call a sub from another sub and am getting a Sub Not Defined error.
I'm trying to run a macro that hides a row in Sheet4 when a specific cell in Sheet1 changes.
Here is my code for Sheet1:
Public Sub HiddenValidations()
If [O18]=true then call test
End Sub
Here is my code for Sheet4:
Public Sub test()
Rows("7").entirerow.hidden=true
End Sub
Thank you for your help!
-Kim
You need to qualify your macro name:
Public Sub HiddenValidations()
If [O18]=true Then Sheet4.test
End Sub
(And there is no need for the Call to invoke a macro - as far as I know, it exists only for backward-compatibility reasons.)

Create global variables

Bit of a newbie to VBA, sorry
I need to create some variables that are available throughout my workbook, but I can't seem to figure it out. I've read in previous questions where some people have suggested create a separate dim for this?
When the workbook opens I need to set some variables equal to certain cells in a worksheet, these variables need to be called from dims in others worksheets.
So far I have tried to use
Workbook_Open()
In the 'ThisWorkbook' code area but to no avail.
Any tips?
Reagards
EDIT ----
I have tried with the following:
In 'ThisWorkbook'
Public wsDrawings As String
Public Sub Workbook_Open()
wsDrawings = "Hello"
End Sub
And in Sheet1
Private Sub CommandButton1_Click()
MsgBox wsDrawings
End Sub
I do not get an error, but the message box is empty.
Just declare the variables you need wherever they are first used (ThisWorkbook is a fine place to do it) and replace the typical Dim with Public. It will then be accessable in all your code
You can create global variable with code like this
Public testVar As String
you need to place it outside function or sub and then this variable has value till you close workbook. But i think it have scope only in current module.
So you can have something like this
Public testVar As String
Private Sub Workbook_Open()
testVar = "test"
End Sub
Sub testEcho()
MsgBox testVar
End Sub
for shared variable between multiple modules look here
edit:
So now i found, that you can use public variable from ThisWorkbook using this
Sub testSub()
MsgBox ThisWorkbook.wsDrawings
End Sub
you can use module for creating global variable.

Auto_open & Workbook_open with Connection refresh in VBA

I have a script that refreshes my data connection to a SQL server. I need to the macro to run when the workbook is opened but for some reason it will not. I guess it has to do with the connection refresh. I tried with both Workbook_Open and Auto_Open() and neither are working. Other than the data load the only thing I am doing is filtering and copying data, nothing exotic.
Does the data refresh need permissions outside of the script, is that the problem?
Here are the first couples lines.
Sub Auto_Open()
ActiveWorkbook.Connections("Connection Name").Refresh
other stuff
end sub
You are putting the code in 'ThisWorkbook' object, right.
Private Sub Workbook_Open()
ActiveWorkbook.RefreshAll
End Sub
Private Sub Workbook_Open()
'Step 1: Use the RefreshAll method
Workbooks(ThisWorkbook.Name).RefreshAll
End Sub
user6058587 was close, but you just need to add this to ThisWorkbook:
Private Sub Workbook_Open()
'Step 1: Use the RefreshAll method
Workbooks(ThisWorkbook.Name).RefreshAll
End Sub

VBA: Copying Cell Values at Specific Time

I have contents in column K that is constantly updating throughout the day. I want to copy the values of specific cells at specific times, and paste them into column F. What I lack is the VBA code to copy the contents at a specific time.
Based on chris neilsen's comment (I was not aware of Application.OnTime).
You can do the following (the code below goes in the ThisWorkbook module):
Option Explicit
Private Sub Workbook_Open()
Call ScheduleTask
End Sub
Public Sub ScheduleTask()
Application.OnTime TimeValue("15:00:00"), "ThisWorkbook.Execute"
End Sub
Public Sub Execute()
Debug.Print "Executing task", Now
'Place your code here
Call ScheduleTask
End Sub