I am learning VBA and trying to piece together what I know to automate a spreadsheet.
I have a macro that pulls from two source sheets to develop a matrix on a Master spreadsheet. The macro runs based on selections from a drop down list. The macro is an index/match function that pulls the fields from the source sheets.
I need to update the Master spreadsheet when a change is made to either of the source sheets. The macro is below and is on the Master spreadsheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim n As Long
'here 5 is column number 5 which is column E
If (Target.Column <> 5) Then GoTo Exit Sub
Application.EnableEvents = False
thisrow = Target.Row
RiskInput = Cells(thisrow, 5)
If RiskInput = "" Then
'now you can comment out the code if it does not make sense
'but the idea is if the cell in column C has been cleared out,
'corresponding date and time should be cleared out too
Cells(thisrow, "D") = ""
Cells(thisrow, "F") = ""
Cells(thisrow, "G") = ""
Else
Cells(thisrow, 4) = Application.WorksheetFunction.Index(Sheets("Risks").Range("A2:A999"), Application.WorksheetFunction.Match(RiskInput, Sheets("Risks").Range("B2:B999"), 0), 0)
Cells(thisrow, 6) = Application.WorksheetFunction.Index(Sheets("Risks").Range("c2:c999"), Application.WorksheetFunction.Match(RiskInput, Sheets("Risks").Range("B2:B999"), 0), 0)
Cells(thisrow, 7) = Application.WorksheetFunction.Index(Sheets("Risks").Range("d2:d999"), Application.WorksheetFunction.Match(RiskInput, Sheets("Risks").Range("B2:B999"), 0), 0)
End If
Application.EnableEvents = True
End Sub
I have a macro that pulls from two source sheets to develop a matrix on a Master spreadsheet. The macro runs based on selections from a drop down list. The macro is an index/match function that pulls the fields from the source sheets.
I need to update the Master spreadsheet when a change is made to either of the source sheets. The macro is below and is on the Master spreadsheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim n As Long
'here 5 is column number 5 which is column E
If (Target.Column <> 5) Then GoTo Exit Sub
Application.EnableEvents = False
thisrow = Target.Row
RiskInput = Cells(thisrow, 5)
If RiskInput = "" Then
'now you can comment out the code if it does not make sense
'but the idea is if the cell in column C has been cleared out,
'corresponding date and time should be cleared out too
Cells(thisrow, "D") = ""
Cells(thisrow, "F") = ""
Cells(thisrow, "G") = ""
Else
Cells(thisrow, 4) = Application.WorksheetFunction.Index(Sheets("Risks").Range("A2:A999"), Application.WorksheetFunction.Match(RiskInput, Sheets("Risks").Range("B2:B999"), 0), 0)
Cells(thisrow, 6) = Application.WorksheetFunction.Index(Sheets("Risks").Range("c2:c999"), Application.WorksheetFunction.Match(RiskInput, Sheets("Risks").Range("B2:B999"), 0), 0)
Cells(thisrow, 7) = Application.WorksheetFunction.Index(Sheets("Risks").Range("d2:d999"), Application.WorksheetFunction.Match(RiskInput, Sheets("Risks").Range("B2:B999"), 0), 0)
End If
Application.EnableEvents = True
End Sub