VBA Form to Only Calculate once 3 textboxes are filled - excel

Currently I have a VBA form with 4 textboxes, the first starts populated with a value, the remaining 3 will start blank. My goal is to have the formula run only when all 3 are greater than zero or "", and recalculate on subsequent changes, provided again that all 3 have values. I found a slightly similar thread for this but it was on Java, not VBA. This is what I have so far. I've tried IF functions and it still throws an error. Thank you!
Option Explicit
Public RTempFormP As Double
Private Sub UserForm_Initialize()
RTempFormP = Range("RPress1")
TextBox1 = RTempFormP
End Sub
Private Sub TextBox2_AfterUpdate()
Call RunTemp
End Sub
Private Sub TextBox3_AfterUpdate()
Call RunTemp
End Sub
Private Sub TextBox4_AfterUpdate()
Call RunTemp
End Sub
Sub RunTemp()
Dim CoeffA As Double: CoeffA = TextBox2
Dim CoeffB As Double: CoeffB = TextBox3
Dim CoeffC As Double: CoeffC = TextBox4
Dim RTempFormT As Double
RTempFormT = CoeffB / (CoeffA - Log10(RTempFormP * 14.5)) - CoeffC
Label7 = (9 / 5) * (RTempFormT - 273.15) + 32
End Sub

Related

How to make counting the cells with button in Excel, VBA?

I'm making the simple app using the VBA code and forms in Excel. So, I need to have a simple Private Sub CommandButton1_Click() method which will call for calculation methods and write down the results in Label. How can I do this? (yes, I'm new to VBA)
Private Sub CommandButton1_Click()
MsgBox "My text here"
End Sub
Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
CommandButton1_Click
End Sub
But instead of the calling the window with my text there I need to make calculations of cells.
Will it be correct if I'll write code like shown down there and somehow add the calculations of cells?
Sub Button1_Click()
Sheet1.testing
End Sub
Sub testing()
Dim ell As Object
Dim post As String
Dim Count As Double
Dim cm As String
End Sub
In vba editor, double-click on the command-button in the UserForm to enter the code:
If you put the sub-procedure in the module (e.g doSomething sub-procedure), you have to add the module name:
Call Module1.doSomething
Option Explicit
Private Sub CommandButton1_Click()
Call doSomething
End Sub
'--------------------------------------
Sub doSomething()
MsgBox "My text here"
End Sub
#FunThomas helped me (you can see it in comments to this question). And here is the solution on my question:
Private Sub CommandButton1_Click()
testing
End Sub
Sub testing()
Dim value1
calc = value1 + 10
Label1 = value1 + 10
Dim ell As Object
Dim post As String
Dim Count As Double
Dim cm As String
Dim sText As String
sText = Sheet1.Range("A1").Value
Label2 = sText
End Sub

create a loop, that will fill 300 cells, from 300 checkboxes

I want to write a value (1) into a desired cell within Excel 2007, when I select a checkbox. The checkbox is in a Visual Basic userform, not on the active sheet itself.
The value (1) must revert back to zero when the checkbox is not selected.
I managed to get it working, however, I have more than 300 check-boxes, and want to know how to create one code that will do it in a loop?
{
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
ThisWorkbook.Sheets("sheet3").Range("b8").Value = 1
Else: ThisWorkbook.Sheets("sheet3").Range("b8").Value = 0
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
ThisWorkbook.Sheets("sheet3").Range("b9").Value = 1
Else: ThisWorkbook.Sheets("sheet3").Range("b9").Value = 0
End If
End Sub
Private Sub CheckBox3_Click()
If CheckBox3.Value = True Then
ThisWorkbook.Sheets("sheet3").Range("b10").Value = 1
Else: ThisWorkbook.Sheets("sheet3").Range("b10").Value = 0
End If
End Sub
}
from checkbox 1 to checkbox 300, the cell range will be "B8" all the
way to "B308"
checkbox1 = cell range b8
checkbox2 = cell range b9
checkbox3 = cell range b10
checkbox4 = cell range b11
etc.......
Use IIf to set a value based on True or False.
Private Sub CheckBox1_Click()
ThisWorkbook.Sheets("sheet3").Range("B8").Value = IIf(CheckBox1.Value, 1, 0)
End Sub
Private Sub CheckBox2_Click()
ThisWorkbook.Sheets("sheet3").Range("B9").Value = IIf(CheckBox2.Value, 1, 0)
End Sub
Private Sub CheckBox3_Click()
ThisWorkbook.Sheets("sheet3").Range("B10").Value = IIf(CheckBox3.Value, 1, 0)
End Sub
I don't know how your workbook/userform looks in detail, but this should show pretty good how to do it:
First, we need the class which we will call Class1. In this class we put the code:
Option Explicit
Public WithEvents CBoxC As MSForms.CheckBox
Private Sub CBoxC_Change()
Dim i As Long
i = CLng(Replace(CBoxC.Name, "CheckBox", ""))
Sheet3.Cells(i + 7, 2).Value = 0 - CBoxC.Value
End Sub
Now we need a variable which "transfere" the events to our class. We jast add to any module:
Option Explicit
Public CBox() As New Class1
As the last step, we need to insert all the controls into our variable. So we add (or just include, if already there):
Option Explicit
Private Sub UserForm_Initialize()
Dim b As Variant
For Each b In Me.Controls
If TypeName(b) = "CheckBox" Then
If (0 / 1) + (Not Not CBox) = 0 Then ReDim CBox(0) Else ReDim Preserve CBox(UBound(CBox) + 1)
Set CBox(UBound(CBox)).CBoxC = b
End If
Next
End Sub
Instead of _Click we better use _Change. This way also keyboard-input will work...
As it is pretty much no code, it also should be self explaining. Just keep in mind, that such events will come last. (which should not matter in your case)
If you still have any questions, just ask ;)

Use Variable in multiple subs VBA

So I'm using excel 2010, and right now, I'm trying to use calculate the value of a variable in one sub, and want it to be used in multiple subs, except I can't figure out how to do this. Here is an example of what I'm using in its most basic form
Public Sub Calc()
Dim i As Integer
i = 10 - 5
End Sub
'Other sub that will use the value for i calculated in the Calc sub
Sub Macro()
y = i +5
End Sub
How am I suppose to be able to use/pass this value for "i" in/into the sub macro?
Move the Dim above the subs:
Public i As Integer
Public Sub Calc()
i = 10 - 5
End Sub
Sub Macro()
y = i + 5
MsgBox y
End Sub
Sub MAIN()
Call Calc
Call Macro
End Sub
Try running MAIN()
Insert new module and define i as:
Public i As Integer
Then you'll be able to use it whenever you want.
For further information, please see: Scope of variables in Visual Basic for Applications
I would like to use Function instead of Sub.
Sub Main()
Msgbox Macro(calc)
end Sub
Function Calc() as Integer
Dim i As Integer
i = 10 - 5
Calc = i
End Function
Function Macro(i as Integer) as Integer
Macro = i + 5
End Function

"Ok" command box in userform

basically I have a userform which I would like to use to enter 2 data into another macro which I already have. The userform is as below:
Basically, I would like the OK button to be clicked and the data in the two boxes will be entered into another macro that I have. It would also be great if the OK button can help in a sense that it will prompt a warning if one of the boxes is not filled up.
So far, I do not have much of a code for this..
Private Sub UserForm_Click()
TextBox1.SetFocus
Sub Enterval()
End Sub
Private Sub TextBox1_Change()
Dim ID As String
ID = UserForm3.TextBox1.Value
End Sub
Private Sub TextBox2_Change()
Dim ID2 As String
ID2 = UserForm3.TextBox2.Value
End Sub
Private Sub OKay_Click()
Enterval
End Sub
Would appreciate any tips and help. Thanks!
My other macro
Private Sub CommandButton1_Click()
Dim Name As String
Dim Problem As Integer
Dim Source As Worksheet, Target As Worksheet
Dim ItsAMatch As Boolean
Dim i As Integer
Set Source = ThisWorkbook.Worksheets("Sheet1")
Set Target = ThisWorkbook.Worksheets("Sheet2")
Name = Source.Range("A3")
Problem = Source.Range("I13")
Do Until IsEmpty(Target.Cells(4 + i, 6)) ' This will loop down through non empty cells from row 5 of column 2
If Target.Cells(4 + i, 6) = Name Then
ItsAMatch = True
Target.Cells(4 + i, 7) = Problem ' This will overwrite your "Problem" value if the name was already in the column
Exit Do
End If
i = i + 1
Loop
' This will write new records if the name hasn't been already found
If ItsAMatch = False Then
Target.Cells(3, 6).End(xlDown).Offset(1, 0) = Name
Target.Cells(4, 6).End(xlDown).Offset(0, 1) = Problem
End If
Set Source = Nothing
Set Target = Nothing
End Sub
Thats the macro i have. As u said, i change the
othermacro
to CommandButton1_Click()
but it doesn't work
Quoting geoB except for one thing: when you .Show your UserForm from a main Sub, you can also .Hide it at the end and the macro that called it will continue its procedures.
Sub Okay_Click()
Dim sID1 As String, sID2 As String
' A little variation
If Me.TextBox1 = "" Or Me.TextBox2 = "" Then
MsgBox "Please fill all the input fields"
Exit Sub
End If
Me.Hide
End Sub
To address your TextBox, you can write in your main Sub UserForm3.TextBox1 for example
There is no need for an Enterval function. Instead, assume the user can read and follow instructions, then test whether that indeed is the case. Note that in your code ID and ID2 will never be used because they exist only within the scope of the subroutines in which they are declared and receive values.
To get started:
Sub Okay_Click()
Dim sID1 As String, sID2 As String
sID1 = UserForm3.TextBox1.Value
sID2 = UserForm3.TextBox2.Value
If Len(sID1 & vbNullString) = 0 Then
MsgBox "Box A is empty"
Exit Sub
End If
If Len(sID2 & vbNullString) = 0 Then
MsgBox "Box B is empty"
Exit Sub
End If
'Now do something with sID1, sID2
otherMacro(sID1, sID2)
End Sub
For your other macro, declare it like this:
Sub otherMacro(ID1, ID2)
...
End Sub
Also, the SetFocus method should occur in the form open event.

How to sum two numbers using a Userform and output it in a MsgBox?

I have created a userform with three textboxes.
The first textbox is for the first number, the second for entering the second number, and the last one is the result.
I have create a button named Calculate.
I have this code for textbox1:
Private Sub TextBox1_Change()
Dim a As Integer
a = Val(TextBox1.Text)
End Sub
and this for textbox2:
Private Sub TextBox2_Change()
Dim b As Integer
b = Val(TextBox2.Text)
End Sub
and I have a button which shows the result
Private Sub CommandButton1_Click()
Dim c As Integer
c = a + b
MsgBox (c)
End Sub
I enter 1 for textbox1 and 2 for textbox2, 1+2 will be 3, but in the MsgBox I
see 0. Why is this, and how can I fix it?
I wouldn't assign the values of the boxes to variables (and unless they are global variables, the scope of the variables life is the routine, so the variable will die after the sub() for each is over, so when the command button event occurs, the variables are no longer alive), just reference them directly. Just add this for your command button and it should do the job.
Private Sub CommandButton1_Click()
MsgBox(TextBox1.Value + TextBox2.Value)
End Sub
declaration of variables must be in general, it shouldn't be under the Sub...
Dim a As Integer
Dim c As Double
Dim b As Integer
Private Sub CommandButton1_Click()
c = a + b
MsgBox (c)
End Sub
Private Sub TextBox1_Change()
a = Val(TextBox1.Text)
End Sub
Private Sub TextBox2_Change()
b = Val(TextBox2.Text)
End Sub
Private Sub Calculate_Click()
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = Val(`TextBox1.Text`)
b = Val(`TextBox2.Text`)
c = a + b
MsgBox (c)
End Sub
Dim a As Double
Dim b As Double
Dim c As Double
Private Sub CommandButton1_Click()
a = Val(TextBox1.Text)
b = Val(TextBox2.Text)
c = a + b
MsgBox (c)
End Sub

Resources