ByVal vs ByRef VBA

后端 未结 4 1980
离开以前
离开以前 2021-02-09 21:25

I\'ve tried to attempt something that was answered by JaredPar ByRef vs ByVal Clarification

ByVal in VB.NET means that a copy of the provided

4条回答
  •  悲&欢浪女
    2021-02-09 22:08

    I would do it this way.

    Sub test1()
    
        Dim trythis As Boolean
    
        trythis = False
    
        If (Sheets("TESTING").Range("A1").value = 1) Then
            tr = testingRoutine(trythis)
            If tr Then
                Debug.Print "Value changed to 0"
                Sheets("TESTING").Range("A1").value = 0
            End If
        End If
    
    End Sub
    
    Private Function testingRoutine(ByRef trythis As Boolean)
    
        Debug.Print "Ran Function"
        testingRoutine = True
    
    End Function
    

    Because trythis is not a global variable, changing it in the function will do nothing in the sub. It will just define trythis as True in the function and stay in that scope. To get trythis to be affected by the function and read by the sub you have to assign it to a variable within the sub.

提交回复
热议问题