Calling one function from the other function in VBA (Excel) - 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..

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

Related

Combine VBA Codes to Hide Multiple Rows

I'd like to combine the two Hide Row Codes, please help!
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Sheets("Re-Hire Questionnaire").Protect Password:="Testing2022!", UserInterFaceOnly:=True
If Range("B12").Value = False Then
Rows("9:11").EntireRow.Hidden = True
Else
Rows("9:11").EntireRow.Hidden = False
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Sheets("Re-Hire Questionnaire").Protect Password:="Testing2022!", UserInterFaceOnly:=True
If Range("B19").Value = False Then
Rows("16:16").EntireRow.Hidden = True
Else
Rows("16:16").EntireRow.Hidden = False
End If
End Sub
Tried to place them underneath each other but didn't work. I'm a novice btw lol
A Worksheet Selection Change: Hide Rows
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.CountLarge > 1 Then Exit Sub
Dim TargetCells: TargetCells = Array("B12", "B19")
Dim HideRows: HideRows = Array("9:11", "16:16")
Dim tCell As Range, hRows As Range, n As Long, WasSelected As Boolean
For n = LBound(TargetCells) To UBound(TargetCells)
Set tCell = Me.Range(TargetCells(n))
If Not Intersect(tCell, Target) Is Nothing Then
Set hRows = Me.Rows(HideRows(n))
hRows.EntireRow.Hidden = tCell.Value = False
WasSelected = True
End If
Next n
If WasSelected Then
Me.Parent.Sheets("Re-Hire Questionnaire").Protect _
Password:="Testing2022!", UserInterFaceOnly:=True
End If
End Sub

Preventing Rows from being hidden based on cells not empty

I am needing to prevent a range of rows from being hidden if any of the rows have text in cells in D column.
Private Sub Worksheet_Change(ByVal Target As Range)
Set rRng = Sheet1.Range("D35, D36, D37, D38, D39, D40")
If Target.Row >= 34 And Target.Row <= 40 Then
If IsEmpty("rRng.value") Then
Range("D35:D40").EntireRow.Hidden = False
Else: Range("D35:D40").EntireRow.Hidden = (Range("D34").Value = "")
End If
End If
End Sub
You might need to trigger your hide/unhide from the calculate event too so that any formulas/vba that changes the values also triggers and update. (Im not sure what the intended result is, so the code below isn't a dropin replacement)
Private Sub Worksheet_Calculate()
HideRows
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
HideRows
End Sub
Public Sub HideRows()
Dim R As Range: Set R = Sheet1.Range("D35:D40")
If Excel.WorksheetFunction.CountA(R) > 0 Then
R.EntireRow.Hidden = False
Else
R.EntireRow.Hidden = True
End If
End Sub
Sub SetAValue()
Sheet1.Range("D35").Value = "show"
End Sub

Add 2 private subs to 1 worksheet?

Hi how can i add both of these codes to 1 worksheet ?
The code auto copies data from 1 sheet to another
Thanks
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And Target.Cells.Count = 1 Then
If LCase(Target.Value) = "y" Then
With Target.EntireRow
.Copy
Sheets("sheet9").Range("A" & Rows.Count).End(xlUp)(2).PasteSpecial xlValues
End With
Application.CutCopyMode = False
End If
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 2 And Target.Cells.Count = 1 Then
If LCase(Target.Value) = "b" Then
With Target.EntireRow
.Copy
Sheets("sheet10").Range("A" & Rows.Count).End(xlUp)(2).PasteSpecial xlValues
End With
Application.CutCopyMode = False
End If
End If
End Sub
Your two subs do essentially the same thing, so you only need one:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As string
If Target.Column = 2 And Target.Cells.Count = 1 Then
Select Case LCase(Target.Value)
Case "y": ws = "Sheet9"
Case "b": ws = "Sheet10"
End select
If ws <> "" Then
Thisworkbook.sheets(ws).cells(rows.count,1).end(xlUp).offset(1,0).Entirerow.value = _
Target.EntireRow.Value
End If
End If
End Sub

Combine Two or More Private Sub in one worksheet

I would like to have two private sub as below (maybe more) in one sheet.
Each one works separately, but when I have both, only first one works. Could you please help me out.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Me.Range("f6:G19, j6:m19, f22:G35, j22:j35, L22:M35")) Is Nothing Then Exit Sub
If Not Target.MergeCells Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Else
If IsEmpty(Target.Cells(1, 1)) Then Exit Sub
End If
Cancel = True
Dim Lastrow As Long
Lastrow = Sheets("ShoppingCart").Cells(Rows.Count, "C").End(xlUp).Row + 1
Target.Cells(1, 1).Copy Sheets("ShoppingCart").Cells(Lastrow, 3)
End Sub
and
Private Sub Worksheet_BeforeDoubleClick_B(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Me.Range("h24:h25, h8:h9")) Is Nothing Then Exit Sub
If Not Target.MergeCells Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Else
If IsEmpty(Target.Cells(1, 1)) Then Exit Sub
End If
Cancel = True
Dim Lastrow As Long
Lastrow = Sheets("ShoppingCart").Cells(Rows.Count, "C").End(xlUp).Row + 1
Target.Cells(1, 1).Copy Sheets("ShoppingCart").Cells(Lastrow, 3)
Sheets("ShoppingCart").Cells(Lastrow + 1, 3).Value = "148H3124"
End Sub
thank you so much in advance.
Event handlers have specific names, it isn't recognizing the second sub as an event handler it just considers that a sub that happens to have a name that looks similar to the first one. You can either rename both and then create a new event sub and call them from that or combine them into a single sub.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
dblclick_a target, cancel
dblclick_b target, cancel
end sub
Private Sub dblclick_a(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Me.Range("f6:G19, j6:m19, f22:G35, j22:j35, L22:M35")) Is Nothing Then Exit Sub
If Not Target.MergeCells Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Else
If IsEmpty(Target.Cells(1, 1)) Then Exit Sub
End If
Cancel = True
Dim Lastrow As Long
Lastrow = Sheets("ShoppingCart").Cells(Rows.Count, "C").End(xlUp).Row + 1
Target.Cells(1, 1).Copy Sheets("ShoppingCart").Cells(Lastrow, 3)
End Sub
Private Sub dblclick_b(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Me.Range("h24:h25, h8:h9")) Is Nothing Then Exit Sub
If Not Target.MergeCells Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Else
If IsEmpty(Target.Cells(1, 1)) Then Exit Sub
End If
Cancel = True
Dim Lastrow As Long
Lastrow = Sheets("ShoppingCart").Cells(Rows.Count, "C").End(xlUp).Row + 1
Target.Cells(1, 1).Copy Sheets("ShoppingCart").Cells(Lastrow, 3)
Sheets("ShoppingCart").Cells(Lastrow + 1, 3).Value = "148H3124"
End Sub

Execute code if the value entered in the cell is not the same as the previous value

I have data validation as list for some cells (possible values are "Enrolled", "Waitlisted", "Cancelled"). I need to execute some code if the value of these cells changes, only if the new value is not the same as the existing one. Question is, how can I get Excel to compare the previous value of the cell with the current one.
I tried this solution (How do I get the old value of a changed cell in Excel VBA?) but it didn't work. What am I missing? Here is some sample code. Currently, it changes the cell colors even if I enter the same value.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim previous_value As String
previous_value = oval
Select Case Target.Value
Case Is = "enrolled"
If previous_value = Target.Value Then
MsgBox "you entered the same value"
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
Else
Target.Interior.Color = vbBlue
End If
Case Is = "waitlisted"
' (....etc.)
End Select
End Sub
Public Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim oval As String
If Selection.Cells.Count = 1 Then
oval = Target.Value
End If
End Sub
If you use something like this below code, you can save the most recent clicked instance in a named range and then check it against whatever the user entered. Obviously, this goes in the respective sheet code.
Private anOldValue As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count = 1 Then
If Target.Value = anOldValue Then
MsgBox "Same value!"
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count = 1 Then
anOldValue = Target.Value
End If
End Sub
Here is the final code. Thanks #PGCodeRider for the help!
Private anOldValue As Variant
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count = 1 Then
Select Case Target.Value
Case Is = "enrolled"
If Target.Value = anOldValue Then
MsgBox "Student already enrolled!"
Else 'code that needs to happen when "enrolled" is selected
Target.Interior.ColorIndex = 10
End If
Case Is = "waitlisted"
If Target.Value = anOldValue Then
MsgBox "Student already waitlisted!"
Else 'code that needs to happen when "waitlisted" is selected
Target.Interior.ColorIndex = 20
End If
Case Is = "cancelled"
If Target.Value = anOldValue Then
MsgBox "Student already cancelled!"
Else 'code that needs to happen when "cancelled" is selected
Target.Interior.ColorIndex = 30
End If
End Select
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count = 1 Then
anOldValue = Target.Value
End If
End Sub

Resources