-1

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 
  • Not sure what you are asking. If you have a Command button with the name `CommandButton1`, the Sub will be triggered when the button is pressed – FunThomas Jul 29 '22 at 12:32
  • It's because I'm very new to VBA, unfortunately. I mean I just need to link the two methods. CommandButton_Click() and another method which will make calculations of cellsю – Programmer1988 Jul 29 '22 at 12:42
  • 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 – Programmer1988 Jul 29 '22 at 12:47
  • Just call it with `testing`, not with `Sheet1.testing` - unless you put the `testing` routine into the code behind of `Sheet1` - which you shouldn't do. Either put the code for `testing` into the code of the form or into a regular module - I prefer the latter as the code of the form should be limited to events of the form, not business logic. – FunThomas Jul 29 '22 at 13:06
  • Thanks, @FunThomas, your advice help. I'm trying to link it to my sheet cell values – Programmer1988 Jul 29 '22 at 13:35
  • The routine can work on sheet of any workbook, no need to put it in the worksheet module for that. Just qualify the usage of `Range` or `Cells` - that means tell VBA which workbook and sheet you want to access: `ThisWorkbook.Sheets("Sheet1").Range("A1")` or `Workbooks("Test.xlsm").Sheets(1).Cells(1, 1)` – FunThomas Jul 29 '22 at 13:44

2 Answers2

0

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
0

@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