简体   繁体   中英

Excel VBA form, how to submit multiple rows ONLY if the value in a certain cell in each row is larger than > 0

I am currently trying to create a data entry like form within Excel 2016 with VBA code and some formulas that enables the user to choose a product from a dropdown to automatically calculate quantities and order amounts needed for each raw material that makes up the product.

My workbook is made up of 4 sheets titled

"iform" - A form that allows the user to select a product from a drop down, enter in a date and enter a qty. The form automatically then pulls the description of the product. Then in a table below all the required parts to make this product are listed with qty on hand, qty on order, allocated, qty needed for each 1 product and how many are required

"ordermaster" - A blank list that gets updated with requested parts and amounts whenever the user submits the form from "iform" ,

"bomitemqty" - a list containing all the final products and their parts. Other information included in this list: current quantity on hand, quantity allocated and qty required per 1 final produced product

"fgmaster" - a list containing all the final products

Everything seems to be working so far apart from the fact that when I submit the form from "iform" I cant seem to get it to only send rows where 'qty needed' > 0 to the list in "ordermaster". Screenshots and code provided below. The rows highlighted in yellow below are the ones im wanting to not move over to the list in "masterorder"

Screenshot: iform image

The cells containing the all the parts and qty's are populated using the below formula (this data is pulled from "bomitemqty" using the fg num, circled in red above, to filter the required rows:

=IFERROR(INDEX(bomitem[PartNum]:bomitem[qtyperunit], SMALL(IF(COUNTIF($G$6,bomitem), MATCH(ROW(bomitem), ROW(bomitem)), ""), ROWS(E$13:$E14)), COLUMNS(bomitemqty:$B$2,B3)),"--")

The qty needed is calculated using the below formula:

=IFERROR((($L$8*J13)+I13)-(G13+H13),"")

When the user hits the save button the following module is executed and data from "iform" is moved to a list in the sheet "masterorder" screenshot and code below (the rows highlighted in yellow in the below screenshot and the ones im wanting to not move across)

Screenshot: "ordermaster" sheet with sample data submitted

VBA Code:

Sub Save()

Set frm = ThisWorkbook.Sheets("iform")

Set Source = ThisWorkbook

Dim Var1 As Integer
Dim Var2 As Integer
Dim Var3 As Integer

Set Wks = Source.Worksheets("iform")

Var1 = Application.WorksheetFunction.CountIf(Wks.Range("E13:E29"), "--")

Var2 = 17 - Var1

Var3 = 30 - Var2 - 1


    Dim lr As Long, ws As Worksheet
    Dim arr As Variant, i As Long

    With Worksheets("iform")
        lr = .Cells(30, "E").End(xlUp).Row - Var3
        ReDim arr(1 To lr, 1 To 9)
        For i = LBound(arr, 1) To UBound(arr, 1)
            arr(i, 1) = ""
            arr(i, 2) = ""
            arr(i, 3) = .Cells(i + 12, "E").Value
            arr(i, 4) = ""
            arr(i, 5) = ""
            arr(i, 6) = .Cells(i + 12, "L").Value
            arr(i, 7) = .Cells(6, "L").Value
            arr(i, 8) = ""
            arr(i, 9) = [Text(Now(), "DD-MM-YYYY HH:MM:SS")]
        Next i
    End With

    With Worksheets("ordermaster")
        lr = .Range("C" & .Rows.count).End(xlUp).Row + 1
        .Cells(lr, "A").Resize(UBound(arr, 1), UBound(arr, 2)) = arr
    End With
    


End Sub




Option Explicit

Private Sub cmdSave_Click()

            Call Save

End Sub Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub

VBasic2008 posted an answer in the comments above that solved my problem:)...

Right after For i =... add the line If.Cells(i + 12, "L").Value > 0 Then and before Next i add the line End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM