I have sheet "DataCompile" which I have columns I to L that I would like to vlookup data from sheet "WW", the data are compile from week to week, so I need to vlookup the new lines added ( as picture shown rows 6 to 9). And at column M, I need to divide G2/E2 for yield calculation, follow by column N, with if formula [=IF(M5<=0.3,"=<30% Yield",IF(M5<=0.6,"=<60% Yield", "60%><=100% Yield"))]. I can manually added this with excel formula, but fail to run it with macro, need helps to rectify the coding as I tried to input for the vlookup and divider, but I failed to insert the IF formula for column N, as the coding totally cannot loop and recognize the cell to insert the value at all, with my coding below.
And I tried to loop through rows, to add the divider formula, for column M, it's also failed with following coding.
Really need help to correct the coding and advise what is the best coding to apply.
VBA Code:
Sub UpdateLData()
Application.ScreenUpdating = False
Dim LR As Long
ThisWorkbook.Worksheets("DataCompile").Select
With Sheets("DataCompile").Range("A2", Sheets("DataCompile").Cells(Rows.Count, "A").End(xlUp))
.Offset(, 8).Formula = "=VLOOKUP(B" & .Row & ",'WD_WW'!$A:$H,4,FALSE)"
.Offset(, 8).Value = .Offset(, 8).Value
End With
With Sheets("DataCompile").Range("A2", Sheets("DataCompile").Cells(Rows.Count, "A").End(xlUp))
.Offset(, 9).Formula = "=VLOOKUP(B" & .Row & ",'WD_WW'!$C:$F,5,FALSE)"
.Offset(, 9).Value = .Offset(, 9).Value
End With
With Sheets("DataCompile").Range("A2", Sheets("DataCompile").Cells(Rows.Count, "A").End(xlUp))
.Offset(, 10).Formula = "=VLOOKUP(B" & .Row & ",'WD_WW'!$C:$F,7,FALSE)"
.Offset(, 10).Value = .Offset(, 10).Value
End With
With Sheets("DataCompile").Range("A2", Sheets("DataCompile").Cells(Rows.Count, "A").End(xlUp))
.Offset(, 11).Formula = "=VLOOKUP(B" & .Row & ",'WD_WW'!$C:$F,8,FALSE)"
.Offset(, 11).Value = .Offset(, 11).Value
End With
With Sheets("DataCompile").Range("A2", Sheets("DataCompile").Cells(Rows.Count, "A").End(xlUp))
.Offset(, 12).Formula = "=G2/E2"
.Offset(, 12).Value = .Offset(, 12).Value
End With
Range("A1").Select
Application.ScreenUpdating = True
End Sub
And I tried to loop through rows, to add the divider formula, for column M, it's also failed with following coding.
VBA Code:
Sub divide()
Dim max As Long, i As Long, cell As Range, last As Double
last = .Cells(.Rows.Count, "M").End(xlUp).Row
Set cell = Range("M" & last + 1)
max = Range("A" & Rows.Count).End(xlUp).Row
Do
i = i + 1
If (cell.Offset(i, 0).Value <> "") Then
cell.Value = Cells(Rows.Count, "G").Value / Cells(Rows.Count, "E").Value
Set cell = cell.Offset(i, 0)
i = 0
End If
If cell.Row = max Then Exit Sub
Loop
End Sub
Really need help to correct the coding and advise what is the best coding to apply.