Usually the Excel VBA editor auto-capitalizes keywords and property names for you, but now it is un-capitalizing them. Like this:
Private Sub CommandButton1_Click()
Range("A1").Value = "test"
End Sub
changes to:
Private Sub CommandButton1_Click()
Range("A1").value = "test"
End Sub
And then the code doesn't run properly. Any ideas what could cause this behavior? Thanks.
Possible reasons
You have named one of the modules as value
You have a variable called value in one of your procedures/functions
You have a procedure/function with that name
Example for point 1
Example for point 2
Sub Sample()
Range("A1").value = "Sid"
End Sub
Sub Blah()
Dim value As Long
value = 1
End Sub
Example for point 3
Sub Sample()
Range("A1").value = "Sid"
End Sub
Sub value()
'
'
'
End Sub
Related
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
I add CheckBox1 to my UserForm with this code:
Private Sub UserForm_Initialize()
Dim opt As Variant
Set opt = UserForm1.Controls.Add("Forms.checkbox.1", "CheckBox1", True)
End Sub
Now when I click on a CommandButton I want to Check if the CheckBox1 is checked or not:
Private Sub CommandButton1_Click()
If CheckBox1.Value = False Then
MsgBox "F"
End If
End Sub
But this code doesn't work; I think because the check box is added dynamically.
This is just a simplification of the code for solving the problem.
This is what you are thinking of:
Option Explicit
Private Sub UserForm_Initialize()
Dim opt As Variant
Set opt = Me.Controls.Add("Forms.checkbox.1", "CheckBox1", True)
End Sub
Private Sub CommandButton1_Click()
If Not Me.Controls("CheckBox1") Then
MsgBox "F"
End If
End Sub
However, depending on your experience and desires to write better code, you may decide to follow some MVC pattern in working with Forms. Read these for some more ideas about it:
https://codereview.stackexchange.com/questions/154401/handling-dialog-closure-in-a-vba-user-form
http://www.vitoshacademy.com/vba-the-perfect-userform-in-vba/ (Disclaimer - this is my blog)
https://rubberduckvba.wordpress.com/2017/10/25/userform1-show/
It will need to be as follows
Private Sub CommandButton1_Click()
If Me.Controls("Checkbox1").Value = False Then
MsgBox "F"
End If
End Sub
I have a Problem with a Userform which I called "ComboTest2". It only consists of two Comboboxes. If I instantiate the USerform as an object then the following Code doesn't work in the sense that the second combobox of the Userform doesn't contain the desired data.
Sub FillCombo(ByVal row As Long)
Dim rgCities As Range
Set rgCities = Worksheets("Tabelle2").Range("B2:D2").Offset(row)
ComboTest2.ComboBox2.Clear
ComboTest2.ComboBox2.List = WorksheetFunction.Transpose(rgCities)
ComboTest2.ComboBox2.ListIndex = 0
End Sub
Sub FillMain()
Dim ComboForm2 As ComboTest2
Set ComboForm2 = New ComboTest2
ComboForm2.Show
End Sub
UserForm-Code:
Private Sub CommandButton1_Click()
Me.Hide
End Sub
Private Sub CommandButton2_Click()
Me.Hide
End Sub
Private Sub ComboBox1_Change()
FillCombo ComboBox1.ListIndex
End Sub
Private Sub UserForm_Initialize()
ComboBox1.List = Worksheets("Tabelle2").Range("A2:A5").Value
ComboBox1.ListIndex = 0
FillCombo ComboBox1.ListIndex
End Sub
But if I use the "default instantiation" by VBA which means that I change the FillMain Sub to:
Sub FillMain2()
Dim ComboForm2 As ComboTest2
Set ComboForm2 = New ComboTest2
'ComboForm2.Show
ComboTest2.Show
End Sub
Then everything is fine. Why is that so?
Best regards
It's because FillCombo is referring to the userform by name, and therefore to the default instance (you're actually creating a new instance of the form). If that code is not in the userform, and I'm not sure why you would have it outside the form, you should pass the combobox as an argument to it.
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.
I'm trying to do a User Form that allows to insert a number in a ListBox, in case this number matches with one of sheet names, to select this work sheet. In case there is not a match, to give a message box, that the number was not found.
But i have a problem with defining that the ListBox text must be compared with Sheet names.
It looks in a following way:
The code is following:
Private Sub CommandButton1_Click()
Option Explicit
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
'this line i could not manage
If ws.Name Like "Tel*" Then
Sheets("Tabella Riepilogativa").Select
End If
Else: MsgBox "Phone number was not found"
Next
End Sub
Private Sub Label1_Click()
End Sub
Private Sub ListBox1_Click()
End Sub
Can someone help with it, please?
The Option Explicit belongs at the top of the file.
To find a sheet named "Tel{whatever the user entered in a TextBox}":
Private Sub CommandButton1_Click()
Dim ws As Worksheet, wsFound As Worksheet, searchFor As String
searchFor = "TEL" & UCase$(Trim$(TextBox1.Text))
For Each ws In ThisWorkbook.Sheets
If UCase$(ws.Name) = searchFor Then
Set wsFound = ws
Exit For
End If
Next
If wsFound Is Nothing Then
MsgBox "Not Found (I should probably be a label to save the user an unnecessary click)"
Else
wsFound.Select
End If
End Sub