I'm very new to coding and VBA in general.
I'm trying to produce a macro that is looking up and mapping if elements (broken down by chainages or say distances with a start and end point) and then returning the risk ID for those elements. The code runs fine (I think) but when it loops through the elements it only returns the last iteration for that loop. I was thinking to potentially write a copy and paste function so that every time the condition is satisfied, it'll copy and paste it and move it onto a new row and this way it wouldn't over-ride for any risk IDs.
Option Explicit
Sub automated_gr_lookup()
Dim l As Variant
Dim gr As Variant
Dim st As Long
Dim en As Long
Dim c1 As Long
Dim c2 As Long
Dim c As Integer
Dim d As Integer
Sheets("Geotechnical Risk Register").Select
Application.ScreenUpdating = False
For c = 1 To 413
Sheets("Geotechnical Risk Register").Select
'gr = geotechnical risk'
Cells(8 + c, 2).Select
gr = Selection.Value
'For M002'
Cells(8 + c, 3).Select
l = Selection.Value
If l = "M002" Then
'Start Chainage for GRR ID'
Cells(8 + c, 4).Select
st = Selection.Value
'End Chainage for GRR ID'
Cells(8 + c, 5).Select
en = Selection.Value
Sheets("DES P14 M002").Select
For d = 1 To 74
'Start Chainage for DES ID'
Cells(2 + d, 3).Select
c1 = Selection.Value
'End Chainage for DES ID'
Cells(2 + d, 4).Select
c2 = Selection.Value
'Conditions 1 to 4 - Geotechnical Risk falling within the Design Element Extent'
If (en > c1 And en < c2) Or (st > c1 And en < c2) Or (st > c1 And st < c2) Or (st < c1 And en > c2) Then
Sheets("DES P14 M002").Select
Cells(2 + d, 8).Value = gr
End If
Next d
End If
Next c
End Sub
Also, I know this isn't too much of a future proof way of doing things and I should maybe think of using Tables and defined names / references - I'm open to all solutions. I've only done the best I can.