excel VBA,用于根据在另一个表中输入的数据自动填充表

vc9ivgsu  于 2023-01-21  发布在  其他
关注(0)|答案(1)|浏览(330)

我是一名电气承包商,我做了一个工作表来帮助我投标项目。假设我投标给一个新房子布线。我已经将每个任务"插座"/"开关"分解为每个任务所需的材料和劳动力。然后这些材料乘以所需的数量,并自动填充3个不同的表格。
流程如下:(此项工作需要24个网点)
"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
我尝试做的是填充每个表中需要材料的行。基本上合并每个表中的数据,方法是删除数量为0的行,添加数量〉0的行,然后用所需材料填充行:每次在"截标表"中输入数据时更新
此代码在我运行代码后消除0值,但在我运行代码后不更新在"出价削减表"中输入的数据。此外,我希望将此代码嵌入工作簿中,这样我就不必每次使用工作簿时都运行代码。

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

末端子组件
This is what it looks like after running the code, it works one time but does not update.

cidc1ykv

cidc1ykv1#

如果我没理解错你的问题,你要找的是这样的东西:

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

测试一下,看看这是否符合你的要求。
或者,更明确一些并使代码更灵活可能是明智的做法。如果你的表实际上是formatted as tables,你也可以循环这些所谓的ListObjects。这样,如果你将来插入列/行,代码就不会中断。
为此,您可以使用如下代码:

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

根据您的评论进行编辑:
确保你的表实际上被格式化为一个表,并且被赋予了正确的名称!你也可以在代码中修改表的名称。同样,列名必须是正确的/你应该在代码中修改它们:colNames = Array("quantity_rough", "quantity_trim", "quantity_service")

相关问题