Search code examples
vbaexceltextboxcell

vba excel View cell but keep formula intact


I am using vba with excel to make a user interface what I have is 7 text boxes 6 of the grab the cells m10 - m16 and allow the user to change m17 does exactly the same but my problem is I want to only view m17 and keep the formula everytime I run the program and change a value use 1 of the other 6 boxes it changes the value but removes my formula how can I make it view the result but keep my formula

Private Sub nnn()

End Sub

Public Sub Frame1_Click()

End Sub

Private Sub Label12_Click()

End Sub

Private Sub Label13_Click()

End Sub

Private Sub Label5_Click()

End Sub

Private Sub Label6_Click()

End Sub

Private Sub TextBox12_Change()

End Sub

Private Sub TextBox3_Change()

End Sub

Private Sub TextBox15_Change()

End Sub

Private Sub UserForm_Click()

End Sub

Private Sub UserForm_Initialize()
TextBox15.Text = ThisWorkbook.Sheets("Sheet1").Range("F10").Value
End Sub

this is my code but I do not assign the cell in this cod I assign it to text_box4 by right clicking it in the visual editor within excel view properties and enter in as a controlsource


Solution

  • I have a feeling that you are using the .ControlSource property of the textbox to pull the values.

    enter image description here

    In such a case you cannot prevent the cell from getting over written. The best way to populate the text box is to populate it in the UserForm_Initialize event or the CommandButton1_Click event

    Private Sub UserForm_Initialize()
        TextBox1.Text = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
    End Sub
    

    or

    Private Sub CommandButton1_Click()
        TextBox1.Text = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
    End Sub