nina
nina

Reputation: 37

Calling one function from the other function in VBA (Excel)

I have two VBA functions, but i am unable to call the other from the first function.

Function 1:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Dim lastRow As Long
  With ActiveSheet
    lastRow = .Cells(.Rows.count, "A").End(xlUp).Row
    Dim I, J As Integer
    For I = 1 To lastRow
      If Cells(I, "C").Value = "" Then
        MsgBox "Please Enter Business Type Value", vbOKOnly
        Exit Sub
      End If
    Next I
  End With
End Sub

And the 2nd function:

Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.Cells.Count > 1 Then Exit Sub
  Application.EnableEvents = False

  If Not Application.Intersect(Target, Me.Columns(3)) Is Nothing Then
    ActiveSheet.Unprotect
    Select Case Target.Value
      Case Is = "CNS"
        Target.Offset(0, 4).Locked = True
      Case Is = "cns"
        Target.Offset(0, 4).Locked = True
      Case Is = "APL"
        Target.Offset(0, 4).Locked = False
      Case Is = "apl"
        Target.Offset(0, 4).Locked = False
      Case Else
        MsgBox "Value not covered by the program", vbInformation + vbOKOnly
    End Select
    ActiveSheet.Protect
  Else
  End If
  Application.EnableEvents = True
End Sub

please help somebody.. thanks in advance..

Upvotes: 0

Views: 4782

Answers (1)

user3131905
user3131905

Reputation:

In the same module you just call nameoffunction
You can make function public

public sub function

But it's a poor (sometime good) solution. You should structure your code

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 call modul1.function1 ( Target ) ' As Range)
End sub

Private Sub Worksheet_Change(ByVal Target As Range)
 call modul1.function1 ( Target  ) ' as range
 call modul1.function2 ( Target  )
end sub

edit ok ugly way

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
....
 call Worksheet_change ( Target)
End sub

Upvotes: 1

Related Questions