I am tring to combine two macros with "ElseIf". both work on their own, however, i keeping getting an error with "ElseIf". My code is
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Target.Address = "$B$1" Then
Dim ws As Worksheet
Dim cWs As Worksheet
Set ws = ThisWorkbook.Sheets("test")
Set cWs = ThisWorkbook.Sheets("with macro")
cCol = 0
For i = 4 To ws.Range("IV1").End(xlToLeft).Column
If ws.Cells(1, i) = Target.Value Then
cCol = i
Exit For
End If
Next i
If cCol <> 1 Then
cWs.Range("A5:E100").ClearContents
Dim cRow
cRow = 5
For i = 2 To ws.Range("B65000").End(xlUp).Row
If InStr(1, ws.Cells(i, cCol), "yes") = 0 Then
cWs.Range("A" & cRow) = ws.Cells(i, 1)
cWs.Range("B" & cRow) = ws.Cells(i, 2)
cWs.Range("C" & cRow).Formula = "=VLOOKUP(A" & cRow & ",test!$A$1:$BK$25,3,0)"
cWs.Range("D" & cRow).Formula = "=VLOOKUP(A" & cRow & ",test!$A$1:$BK$25,MATCH($B$1,test!$A$1:$BK$1,0),0)"
cWs.Range("E" & cRow).Formula = "=VLOOKUP(A" & cRow & ",test!$A$1:$BK$25,63,0)"
cRow = cRow + 1
End If
Next i
For Each i In cWs.Range("C5:E" & cWs.Range("E60000").End(xlUp).Row)
If i.Value = 0 Then
i.Clear
End If
Next i
If cRow = 5 Then cWs.Range("A5") = "NO COMPANIES MISSING DATA"
End If
cWs.Columns("C:E").Select
Selection.Copy
cWs.Columns("C:E").Select
Selection.PasteSpecial PASTE:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
cWs.Columns("C:E").Select
Selection.WrapText = True
cWs.Range("E6").Select
Application.CutCopyMode = False
End If
ElseIf Target.Address = "$B$2" Then
Dim Rng As Range
Dim c As Range
Dim MyRange As Range
Set MyRange = cWs.Range("C5:C" & cWs.Range("C100").End(xlUp).Row)
Dim rgZeroCells As Range
Dim rgCell As Range
For Each rgCell In MyRange.Cells
If Not IsError(rgCell) Then
If rgCell.Value = cWs.Range("$B$2") Then
If rgZeroCells Is Nothing Then
Set rgZeroCells = rgCell '
Else
Set rgZeroCells = Union(rgZeroCells, rgCell)
End If
End If
End If
Next rgCell
If Not rgZeroCells Is Nothing Then
rgZeroCells.EntireRow.Delete
End If
Application.ScreenUpdating = True
End If
End Sub
Comment