I am trying to update the userform TextBox2 "number of cells needed" with the value of C2. The user enters the number of parts in the TextBox1 and it updates the cell value A2, but I cant get it to pass the value of C2 to the other text box automatically. There is a simple formula in C2 =(A2*2)+1 but i dont think that should matter.
Private Sub TextBox1_Change()
ThisWorkbook.Worksheets("Sheet2").Range("A2").Value = TextBox1.Value
End Sub
Private Sub TextBox2_Change()
TextBox2.txtEcpNum.Text = CStr(Range("C2").Value)
TextBox2.Show
End Sub
The Textbox2_Change() event handler is not being called when Textbox1_Change() is being called. All you need to do is change Textbox2 after you change Textbox1, ie. in the same event handler. Namely:
Private Sub TextBox1_Change()
ThisWorkbook.Worksheets("Sheet2").Range("A2").Value = TextBox1.Value
TextBox2.txtEcpNum.Text = CStr(Range("C2").Value)
TextBox2.Show
End Sub
Related
I have been wondering about this one for a while now.
Let's say I have a formula in A1, Worksheet("Main")
=IF(B2="English";"Good morning";"Guten Morgan")
Then I have userform with code:
Private Sub TextBox1_Change()
ThisWorkbook.Worksheets("Main").Range("A1").Value = Me.TextBox1.Text
End Sub
Private Sub UserForm_Initialize()
Me.TextBox1.Text = ThisWorkbook.Worksheets("Main").Range("A1").Value
End Sub
How can I make it work so, that if I don't input anything into textbox, it will keep displaying functions result. If I will start to type text into textbox it will input my typed text to A1. Now if I open the userform it will overwrite A1 with the text in textbox and there will be no formula anymore. So if I change language in B2 result will no longer be interfaced into textbox.
Can be also some other approach with VBA. Everything is acceptable as long as logic will work.
I have tried to use textbox properly, something like linkedsource or similar, but it is crashing excel workbook sometimes. That's why I am trying to avoid it.
EDIT:
Thank you for suggestions! I have tried to implement this somehow but still don't get it. I am creating variable where I want to store result from ThisWorkbook.Worksheets("Other Data").Range("L49").Value then I would like to use it in Userform Me.TextBox14.Text to be displayed. Then once it is changed in Me.TextBox14.Text and Enter button has been pressed it should change also in ThisWorkbook.Worksheets("Other Data").Range("L49").Value.
Here is my current code I am trying to play with:
Private ProjectClass As String
Private Sub TextBox14_Enter()
ThisWorkbook.Worksheets("Other Data").Range("L49").Value = ProjectClass
End Sub
Private Sub UserForm_Initialize()
Me.TextBox14.Text = ProjectClass
End Sub
The TextBox.Enter event isn't fired when the user presses Enter, but when the control is entered - that is, when it gets the focus and a caret/cursor starts blinking inside it. You'll want to update the backing variable when the value is modified:
Private Sub TextBox14_Enter()
'runs when the control gets focus
End Sub
Private Sub TextBox14_Exit()
'runs when the control loses focus
End Sub
Private Sub TextBox14_Change()
'runs whenever the value changes (real-time)
End Sub
So in this case I'd go with the TextBox.Change event handler, and make it update the variable (not the worksheet):
Private ProjectClass As String
Private Sub TextBox14_Change()
ProjectClass = TextBox14.Text
End Sub
Now the problem is that the ProjectClass value needs to be accessible from outside the form, so that the caller can set an initial value. One way to do this could be to expose it as a property - one property (get+let) for each field you want to seed a value for:
Public Property Get ProjClass() As String
ProjClass = ProjectClass
End Property
Public Property Let ProjClass(ByVal value As String)
ProjectClass = value
ApplyModelProperties
End Property
Private Sub ApplyModelProperties()
TextBox14.Text = ProjectClass
'...
End Sub
Now from outside the form, at the call site (the code that's showing this dialog), you can seed the value from the worksheet, and the form never needs to know or care that a worksheet was involved:
With New UserForm1
.ProjClass = ThisWorkbook.Worksheets("Other Data").Range("L49").Value
.Show
MsgBox .ProjClass
End With
Note that because the value is exposed as a property, the calling code doesn't need to know about TextBox14 anymore.
I have Userform that have combobox. Combobox picks range from Workbook and inputs picked up result to cell C79 by this VBA:
Private Sub ComboBox1_Change()
ThisWorkbook.Worksheets("Other Data").Range("C79").Value = Me.ComboBox1.Value
End Sub
The problem is when I open Userform for the second time I can't see picked up result in combobox so I have to pick it up again. How to link cell C79 to Private Sub UserForm_Initialize() so that when I open UserForm, value from C79 will be visible in Combobox1?
I have tried:
Private Sub UserForm_Initialize()
ComboBox1.List = ThisWorkbook.Sheets("Other Data").Range("A79:A81").Value ' This one picks the range
'ThisWorkbook.Sheets("Other Data").Range("C79").Value = ReviewForm.ComboBox1.Value
End Sub
To populate a ComboBox control in a UserForm, use the following
Private Sub UserForm_Initialize()
Me.ComboBox1.Value = ThisWorkbook.Sheets("Other Data").Range("C97").Value
End Sub
Alternatively, you could update this value each time the UF is activated:
Private Sub UserForm_Activate()
Me.ComboBox1.Value = ThisWorkbook.Sheets("Other Data").Range("C97").Value
End Sub
Or, you could update the UF's combobox every time the cell value changes. This is not logical however, since you update the cell with the UF. It would activate itself.
I have the following Excel spreadsheet:
A
1 Time
2
3
I would like use the word written in Cell A1 as the text for a label on the UserForm. Therefore, I tried to go with this VBA:
Private Sub Label1_Click()
Label1.Caption = Sheet1.Range("A1").Value
End Sub
However, this code does not use the content from Cell A1 as text for the label. What do I have to change in my code to make it work?
The solution is changing Private Sub Label1_Click to Private Sub UserForm_Activate()
Private Sub UserForm_Activate()
Label1.Caption = Sheet1.Range("A1").Value
End Sub
I'm working in Excel. I'd like to press a button to cancel the value of the cell at the left of the button itself.
So the user experience should be the following:
When the user presses "button1", the cell at its left became 0. The same for "button2" and "button3"
How can I do?
By using a Macro?
Assign this macro to any and all buttons, and it'll delete the info. in the cell directly to the left.
Sub test()
Dim btnRow&, btnCol&
btnRow = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Row
btnCol = ActiveSheet.Shapes(Application.Caller).TopLeftCell.Column
Cells(btnRow, btnCol).Offset(0, -1).ClearContents
End Sub
Or, thanks to #Rory, this can be:
Sub test()
Activesheet.Shapes(Application.Caller).TopLeftCell.offset(0,-1).ClearContents
End Sub
Note: Make sure your shapes are well placed, as this uses wherever the top left of the shape is to determine the row/column. This macro reduces the need to run three different ones, depending on where, and minimizes any If/Then type statements as it uses the Caller to determine which shape, which determines where it is.
May this help...
'Add three buttons on the sheet
'And imaging that you want to delete B4 B5 B6
'You can discover which button is, by double cliking on the desing mode
'on your excel
Private Sub CommandButton1_Click() 'This is the button on the cell C4
Range("B4").ClearContents
'Here B4 is the range of the cell you want to delete...
'This could be acomplish just pressing the button DELETE
'Same by the two other buttons... just adding 1 to the number
'B4...B5...B6
'With this you only can delete de contents of the cell...
End Sub
Private Sub CommandButton2_Click() 'This is the button on the cell C5
Range("B5").ClearContents
End Sub
Private Sub CommandButton3_Click() 'This is the button on the cell C6
Range("B6").ClearContents
End Sub
Hyperlink method is what I've done before. Like it, because it look like it can click.
Add below code on the Worksheet module, when a hyperlink clicked, it will trigger the sub
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If LCase(Target.Range.Text) Like "delete*" Then
ActiveCell.Offset(0, -1).ClearContents
End If
End Sub
You can use below code to generate as much as hyperlink you want.
Sheets(XXX).Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
"", TextToDisplay:="Delete"
If your using command button
Option Explicit
Sub Button1_Click()
Range("A1") = 0
End Sub
or assign to worksheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("B1")) Is Nothing Then _
Range("A1") = 0
End Sub
I have one UserForm with 1 TextBox and 1 ComboBox.
I firstly write in the ComboBox (per exemple Sarah)
Private Sub ComboBox1_Change()
ThisWorkbook.Sheets("CalculSheet").Range("A2").Value = ComboBox1.Text
End Sub
Then it makes some calcul in A3 like (If D2=Sarah Then D3=1)
Private Sub UserForm_Active()
Application.ScreenUpdating = False
ThisWorkbook.Worksheets("CalculSheet").Activate
TextBox1 = Range("A3").Value
End Sub
And I want that the Result comes directly in my TextBox1. It means that I write Sarah in the ComboBox1 and directly comes 1 in the TextBox1.
Delete the Private Sub UserForm_Active() code. You don't need that. Replace ComboBox1_Change() with this.
Is this what you are trying? (Untested)
Private Sub ComboBox1_Change()
With ThisWorkbook.Sheets("CalculSheet")
.Range("A2").Value = ComboBox1.Text
DoEvents
TextBox1.Text = .Range("A3").Value
End With
End Sub