Home > Blockchain >  Combining 2 Commands in VBA
Combining 2 Commands in VBA

Time:06-28

I need some helps.

So I have 2 commands using VBA in Excel. The first one is removing rows when column G is online and the value in column Q is BBC. Here is the code

    Sub RemoveRows()
    Dim i As Integer, LastRow As Integer

    LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

    For i = LastRow To 1 Step -1
        If ActiveSheet.Cells(i, "G").Value = "Competi" And ActiveSheet.Cells(i, "Q").Value = "BBC" Then
            ActiveSheet.Rows(i).Delete
        End If
    Next i
End Sub

the second command is to rename cell in column H. Here is the code

Sub sentiment()
For Each cell In Range("H" & lastrow & ":H" & clastrow)
    Select Case cell.Value
        Case "sport"
            cell.Value = "(sport)"
        Case "politics"
            cell.Value = "(politics)"
        Case "weather"
            cell.Value = "(weather)"
    End Select
Next
End Sub

I wonder is there any ways to combine those commands? thank you in advance.

CodePudding user response:

You can call many sub in one command button. Even you can call other sub to inside sub. Just add Call YourOtherSubName before the line End Sub. See below example from your given code.

Sub RemoveRows()
    Dim i As Integer, LastRow As Integer

    LastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, 1).End(xlUp).Row

    For i = LastRow To 1 Step -1
        If ActiveSheet.Cells(i, "G").Value = "Competi" And ActiveSheet.Cells(i, "Q").Value = "BBC" Then
            ActiveSheet.Rows(i).Delete
        End If
    Next i

 Call sentiment 'Call the other sub.

End Sub
  • Related