Userform: Iniialize until value changes in TextBox - excel

I have UserForm with TextBox. Then there is value in 'Other Data'!C6, in cell C6 on Other Data is formula =D6.
Then by this I am passing value to cell:
Private Sub TextBox3_Change()
ThisWorkbook.Worksheets("Other Data").Range("C6").Value = Me.TextBox3.Text
End Sub
Getting value from cell:
Private Sub UserForm_Initialize()
Me.TextBox3.Text = ThisWorkbook.Worksheets("Other Data").Range("C6").Value
End Sub
Private Sub UserForm_Activate()
Me.TextBox3.Text = ThisWorkbook.Worksheets("Other Data").Range("C6").Value
End Sub
Private Sub UserForm_Click()
Me.TextBox3.Text = ThisWorkbook.Worksheets("Other Data").Range("C6").Value
End Sub
The problem is that number from UserForm is overriding formula in 'Other Data'!C6 while opening UserForm. This is affecting other formulas and calculations in my Excel applivcation. How to prevent 'Other Data'!C6 override until I manually change value in TextBox on UserForm?

Everytime you change the value in the textbox you are triggering the change event and over writing C6. The trick is to stop the triggering in your initialise and other events by turning off EnableEvents while you make your change
Private Sub UserForm_Initialize()
application.EnableEvents = false
Me.TextBox3.Text = ThisWorkbook.Worksheets("Other Data").Range("C6").Value
application.EnableEvents = true
End Sub

Related

how to stay same cell after hit enter and move cell value to down

I need to stay at the same cell in the excel sheet and copy the cell value to down after hitting enter. need to keep the most recent value at the top and shift one by one down(the red arrow shows the current cell and the green arrow shows the most recent value). is there any way to do this with VBA programming?
If you add this code to the Worksheet_Activate() event procedure
Private Sub Worksheet_Activate()
Application.MoveAfterReturn = False
End Sub
so that the active cell will not change after data entry then you could code the Worksheet_Change() event for your data-entry tab as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$2" Then
With Application
.EnableEvents = False
Target.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
.EnableEvents = True
End With
End If
End Sub
and also add the code below the Worksheet_Deactivate() procedure
Private Sub Worksheet_Deactivate()
Application.MoveAfterReturn = True
End Sub
so that the normal active-cell-move-on-Enter behaviour is restored on other sheets.

Chosen result from Worksheet cell to Userform Combobox

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.

When I write in a TextBox with Calcul behind then comes directly the Result in another TextBox

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

Excel VBA: When I click the + beside a group of rows

When I click the + beside a group of rows, how do I get it to hide a row outside of the grouped rows.
I tried this but it didn't work.
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Rows(11).Hidden = True) Then
Rows(22).EntireRow.Hidden = False
Else
Rows(22).EntireRow.Hidden = True
End If
End Sub
Simply hiding or un-hiding a row will not trigger the Event, To use this Event, you must change a cell value.
EDIT#1
You can almost get what you want with the Worksheet_SelectionChange event.
Expand or collapse the Group of rows containing cell A11 and then click anywhere in the worksheet and row #22 will also expand/collapse. Put the following in a standard module:
Public Sub IsHiddenA11()
With Range("A11")
If .EntireRow.Hidden Then
Range("A22").EntireRow.Hidden = True
Else
Range("A22").EntireRow.Hidden = False
End If
End With
End Sub
and put this in the worksheet code area:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Call IsHiddenA11
Application.EnableEvents = True
End Sub

How to use VBA to read the values of a checkbox from an Excel userform

I have created a userform that contains two checkboxes. I would like to be able to do different things depending on whether each box is checked or unchecked. However, it seems like no matter what I do, it will always tell me the original value of the checkboxes (false and false). Here is the code attached to clicking CommandButton1:
Private Sub CommandButton1_Click()
ReadData
End Sub
And here ReadData:
Sub ReadData()
Dim myForm As UserForm
Set myForm = UserForms.Add("ComplaintEntryForm")
Debug.Print (myForm!CheckBox1.Name)
Debug.Print (myForm!CheckBox1.Value)
Debug.Print (myForm!CheckBox2.Name)
Debug.Print (myForm!CheckBox2.Value)
End Sub
No matter how the boxes are checked, the immediate window always shows this:
VBA.UserForms.Add("ComplaintEntryForm").Show
CheckBox1
False
CheckBox2
False
I have a screenshot of the whole operation but it won't let me upload it because I'm a new user.
Try this method to load and show the form (this goes in a normal module):
Sub main()
Dim myForm As ComplaintEntryForm
Set myForm = New ComplaintEntryForm
myForm.Show
Set myForm = Nothing
End Sub
In the UserForm's own module, add the following:
Private Sub CheckBox1_Change()
readData
End Sub
Private Sub CheckBox2_Change()
readData
End Sub
Private Sub UserForm_Initialize()
Me.CheckBox1.Value = True
Me.CheckBox2.Value = False
End Sub
Private Sub readData()
Debug.Print Me.CheckBox1.Name
Debug.Print Me.CheckBox1.Value
Debug.Print Me.CheckBox2.Name
Debug.Print Me.CheckBox2.Value
End Sub
I've initialized the two checkboxes to specific values in the Initialize event. This means we are certain about the state the form will start in

Resources