0

I'm an electrical contractor and I made a worksheet to help me bid projects. Say I'm bidding on wiring a new house. I have broken down each task "outlet"/"Switch" to materials and labor needed for each task. Those materials are then multiplied by the quantity needed and populate 3 different tables automatically.

Here is the process: (24 outlets are needed for this job)

"Bid Cut Sheet" Sheet where quantities of specific tasks are entered.

"Job List" Tasks are broken down into materials needed for that task, multiplied by the quantity entered in "Bid Cut Sheet"

"Material Sheet" Total of all material needed for the job in 3 different tables/stages of the project

What I am trying to do is populate rows in EACH table where materials are needed. Essentially consolidate the data in EACH table by eliminating with quantities of 0 and ADDING rows with quantities >0 and fill down rows with material needed: updating every time data is entered in the "Bid Cut Sheet"

This code eliminates values of 0 after I run the code, but does not update data entered in the "bid cut sheet" after I run the code. Also, I would like this to be imbedded in the workbook so I dont have to run the code each time I use the workbook.

Sub DeleteRowsBasedonCellValue()
'Declare Variables
Dim i As Long, LastRow As Long, Row As Variant
Dim listObj As ListObject
Dim tblNames As Variant, tblName As Variant
Dim colNames As Variant, colName As Variant
                'Names of tables
tblNames = Array("Rough_Material", "Trim_Material", "Service_Material")
colNames = Array("Rough", "Trim", "Service")
                

'Loop Through Tables
For i = LBound(tblNames) To UBound(tblNames)
    tblName = tblNames(i)
    colName = colNames(i)
    Set listObj = ThisWorkbook.Worksheets("MaterialSheet").ListObjects(tblName)
    'Define First and Last Rows
    LastRow = listObj.ListRows.Count
    'Loop Through Rows (Bottom to Top)
    For Row = LastRow To 1 Step -1
        With listObj.ListRows(Row)
            If Intersect(.Range, _
            listObj.ListColumns(colName).Range).Value = 0 Then
                .Delete
            End If
        End With
    Next Row
Next i

End Sub

This is what it looks like after running the code, it works one time but does not update.

Kooks
  • 3
  • 2

1 Answers1

0

If I understand your question correctly, what you are looking for is something like this:

Sub DeleteRowsBasedonCellValue()
    'Declare Variables
    Dim LastRow As Long, FirstRow As Long
    Dim Row As Long
    Dim columns As Variant, column As Variant
    
    columns = Array("A", "D", "G")
    
    With ThisWorkbook.Worksheets("Sheet1") '<- type the name of the Worksheet here
        'Define First and Last Rows
        FirstRow = 1
        LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
        
        'Loop Through Columns
        For Each column In columns
            'Loop Through Rows (Bottom to Top)
            For Row = LastRow To FirstRow Step -1
                If .Range(column & Row).Value = 0 Then
                    .Range(column & Row).Resize(1, 2).Delete xlShiftUp
                End If
            Next Row
        Next column
    End With
End Sub

Test it out and see if this does what you want.

Alternatively, it might be wiser to be more explicit and make the code more flexible. If your tables are actually formatted as tables, you can also loop over these so-called ListObjects. That way, if you insert columns/rows in the future, the code won't break.

To do this, you could use code like this:

Sub DeleteRowsBasedonCellValue()
    'Declare Variables
    Dim i As Long, LastRow As Long, Row As Variant
    Dim listObj As ListObject
    Dim tblNames As Variant, tblName As Variant
    Dim colNames As Variant, colName As Variant
                    'The names of your tables
    tblNames = Array("Rough_Materials", "Trim_Materials", "Service_Materials")
    colNames = Array("quantity_rough", "quantity_trim", "quantity_service")
                    'The name of the column the criterion is applied to inside each table
    
    'Loop Through Tables
    For i = LBound(tblNames) To UBound(tblNames)
        tblName = tblNames(i)
        colName = colNames(i)
        Set listObj = ThisWorkbook.Worksheets("Sheet1").ListObjects(tblName)
        'Define First and Last Rows             '^- the name of the Worksheet
        LastRow = listObj.ListRows.Count
        'Loop Through Rows (Bottom to Top)
        For Row = LastRow To 1 Step -1
            With listObj.ListRows(Row)
                If Intersect(.Range, _
                listObj.ListColumns(colName).Range).Value = 0 Then
                    .Delete 
                End If
            End With
        Next Row
    Next i
End Sub

Edit in response to your comment:

Make sure your table is actually formatted as a table and has been given the right name! You can also change the table names in your code to your liking in the line tblNames = Array("Rough_Materials", "Trim_Materials", "Service_Materials"). Also, the column names have to be correct/you should adapt them in the code: colNames = Array("quantity_rough", "quantity_trim", "quantity_service")

enter image description here

GWD
  • 3,081
  • 14
  • 30
  • Thank you very much for helping me with this GWD. I believe that we are on the right track but the coding above comes up with an error: Run-time error 9 Subscript out of range 'Loop Through Tables For i = LBound(tblNames) To UBound(tblNames) tblName = tblNames(i) colName = colNames(i) **Set listObj = ThisWorkbook.Worksheets("MaterialSheet").ListObjects(tblName)** 'Define First and Last Rows – Kooks Jan 20 '23 at 11:35
  • Hi @Kooks, the code works fine you just have to adapt everything to fit your specific worksheet. Please refer to my updated answer and the screenshot I included! – GWD Jan 20 '23 at 13:49
  • Hello @GWD, thank you for your time! I keep getting run-time error 9: script out of range.
    Here are links to the sheets in my workbk.
    Bid Cut Sheet = All quantities are entered here
    Bid Cut Sheet - [![All quantity information is entered here:][1][1](https://i.stack.imgur.com/dMewN.png)
    I have to screenshot separately because I dont know how to add more links
    – Kooks Jan 20 '23 at 15:49
  • Hi @Kooks, your tables are not actually formatted as tables. Am I right to assume all the tables you want to clean out are on the same worksheet? As already linked in my answer, please take a look at [this](https://support.microsoft.com/en-us/office/overview-of-excel-tables-7ab0bb7d-3a9e-4b56-a3c9-6c94334e492c#:~:text=To%20quickly%20create%20a%20table,row%2C%20and%20then%20click%20OK.). – GWD Jan 20 '23 at 16:23
  • Hello @GWD, you were absolutely correct. The tables were not formatted as tables. I ran the code and it worked perfectly BUT, I entered some more items in the bid cut sheet but the tables are not updating the materials needed. Any ideas? Thank you again – Kooks Jan 20 '23 at 16:51
  • 1
    Hello @GWD, I've updated the question with images of my workbook to help you understand what is happening. Your code worked but does not update when items are entered. If you would, please take a look at the revised question. Thank you again – Kooks Jan 20 '23 at 18:20
  • Hi @Kooks, I just saw your updated question. What you want to do is certainly possible and you might be lucky and find someone here who does it for you. I have to point out though, that this is not a freelancing website. If you want someone to do the work for you, check out places like fiverr.com. If you're interested in learning how to do this yourself, everything you need can be found online. Should you get stuck on a more specific issue, you are welcome to ask about it here. – GWD Jan 20 '23 at 21:20
  • Hello @GWD, I understand and thank you for helping me get this far! Just so you know, I have searched extensively (as in well over a week) on the internet to accomplish what the question states. You have certainly helped me get on the right track! I have built this program myself over the last year (with no excel background) but this last part of it needs vba and I am not very knowledgeable in it. Hence, I came here. If you would, could you answer this last question: Do I need to use a pivot table to accomplish the goal? Much appreciated for all of your help, take care. – Kooks Jan 20 '23 at 22:08
  • @Kooks, I have to think about it a little, I'm still not 100% sure I understand what exactly you want. It's certainly possible with macros and you could place a button in the bid cut sheet and run the macros on a button press whenever required. I can look into it tomorrow. – GWD Jan 20 '23 at 22:52