I am a new programmer. I am programing in VBA. I am practicing writing a sub and a function.
I want the function to return a value each time it is called from the sub and the function return become the cell value. This is the code:
Sub Practice1()
Dim wb As Workbook
Dim ws As Worksheet
Dim i As Integer
Dim contador As Integer
Dim salir As Boolean
Dim FilePath As String
Dim FileOpen As String
FilePath = IsWBOpen("\\uscnap60\Depts\Global Procurement Organization\Supporting Documents\User's Backup Files\Gaspar Gomez\GESTION COMPRA DE MATERIALES 2023 - 2.xlsm")
FileOpen = "GESTION COMPRA DE MATERIALES 2023 - 2.xlsm"
If FilePath = True Then
Workbooks("GESTION COMPRA DE MATERIALES 2023 - 2.xlsm").Activate
MsgBox "File is Open"
Else
Workbooks.Open (FileOpen)
End If
i = 1
Worksheets("Formulas").Activate
'MsgBox ("Worksheet Formulas")
Worksheets("Formulas").Range("a1").Select
'MsgBox ("rango A1 seleccionado")
Do While True
If Cells(i, 1).Value = "hola" Then
Worksheets("Formulas").Cells(i, 1).Select
MsgBox ("llegamos al final")
Exit Do
Else
contador = mathop(i, i)
'MsgBox ("Valor de Contador e I =" & contador & " " & i)
Worksheets("Formulas").Cells(i, 1).Select
Worksheets("Formulas").Cells(i, 1).Value = contador
End If
i = i + 1
Loop
End Sub
Function mathop(num1 As Integer, num2 As Integer) As Integer
num1 = i
num2 = num1 + 2
mathop = num1 + num2
'MsgBox ("Valor retornado de mathop = " & mathop)
End Function