Hi,
Stuck on getting VBA to do something! I have a form with some values that need to be added to a table. On clicking the command button I need it to first add a new row into table procStep using the values in the form. Then, I need to loop through all the cells in the Product Name column of that table and check some conditions, if the conditions are met then add a new row to a different table called resTable.
I'm using the code below.
Private Sub CommandButton1_Click()
Dim ws As Worksheet
Dim tableName As String
Dim resTab As String
Dim loTable As ListObject
Dim resTabFull As ListObject
Dim resRow As ListRow
Dim lrRow As ListRow
Dim c As Range
Dim prodNameCol As Range
Set ws = Sheets("Inputs")
tableName = "procStep"
Set loTable = ws.ListObjects(tableName)
Set lrRow = loTable.ListRows.Add
Set prodNameCol = ws.Range("procStep[Product Name]")
resTab = "resTable"
Set resTabFull = ws.ListObjects(resTab)
Set resRow = resTabFull.ListRows.Add
'Add row to table with form values
With lrRow
.Range(2) = namePick.Value
.Range(3) = wipCode.Value
.Range(5) = materialCode.Value
.Range(7) = costType.Value
.Range(8) = umkg.Value
.Range(9) = loss.Value
End With
'Add new row in separate table if conditions are met
For Each cell In prodNameCol
If cell = namePick.Value And cell.Offset(0, 1) = materialCode.Value And cell.Offset(0, 5) <> costType.Value Then
With resRow
.Range(2) = namePick.Value
.Range(3) = wipCode.Value
.Range(5) = materialCode.Value
.Range(7) = cell.Offset(0, 5).Value
End With
End If
Next cell
End Sub
What's happening is that it is adding the new row to procStep table with the form values ok, but then it's just adding one blank row to the resTable table when there should be 4 rows that meet the conditions given.
Apologies if the code's a bit messy, VBA is not my forte!!
Any help would be appreciated though
Bookmarks