Anbuselvam K
Member
Hi
The attached sheet has a below VBA code to calculate the expected results. Here I want to understand each line of the code what it does and where it is looking the data.
Because I want to rewrite the code according to my other requirements which are the similar worksheet or nearby same worksheet.
If I post that worksheet here, then someone can modify easily. but, I want to understand each line of codes to write my own in the future.
Thanks in advance.
The attached sheet has a below VBA code to calculate the expected results. Here I want to understand each line of the code what it does and where it is looking the data.
Because I want to rewrite the code according to my other requirements which are the similar worksheet or nearby same worksheet.
If I post that worksheet here, then someone can modify easily. but, I want to understand each line of codes to write my own in the future.
Thanks in advance.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("D5:F5")) Is Nothing Then
If Target.Count > 3 Then Exit Sub
Range("A8:E19").Value = ""
If Range("D5").Value = "" Or Range("F5").Value = "" Then Exit Sub
'
pName = Range("D5").Value
nOrig = Range("F5").Value
existe = False
k = 8
Set h = Sheets("Formulation")
Set r = h.Columns("A")
Set b = r.Find(pName, LookAt:=xlWhole, LookIn:=xlValues)
If Not b Is Nothing Then
celda = b.Address
Do
'detalle
If LCase(h.Cells(b.Row, "B").Value) = LCase(nOrig) Then
existe = True
uc = h.Cells(4, Columns.Count).End(xlToLeft).Column
For j = 3 To uc
If h.Cells(b.Row, j).Value <> "" Then
Cells(k, "A").Value = h.Cells(4, j).Value
Cells(k, "B").Value = h.Cells(5, j).Value
Cells(k, "E").Value = h.Cells(b.Row, j).Value
k = k + 1
End If
Next
Exit Do
End If
Set b = r.FindNext(b)
Loop While Not b Is Nothing And b.Address <> celda
Else
MsgBox "Prodcut not exists"
End If
If existe = False Then
MsgBox "Relation Prodcut - Origin not exists"
End If
End If
End Sub
Attachments
Last edited by a moderator: