Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

excel - Writing a VBA code for a button that is supposed to update inventory

This is a VBA code that supposed to update inventory, but the error message "Objected required keeps popping up.

(Only cellOut4 is the only cells containing numbers)

Sub updateInventory()

'List of variable as ranges corrosponding with each column in each sheets

' Parts Sign Out:
Dim rngOut1, rngOut2, rngOut3, rngOut4 As Range
Dim cellOut1, CellOut2, cellOut3 As Range
Dim cellOut4 As Variant

'In Stock Parts:
Dim rngIn1, rngIn2, rngIn3, rngIn4, rngIn5, rngIn6, rngIn7, rngIn8, rngIn9 As Range
Dim cellIn1, cellIn2, cellIn3, cellIn4, cellIn5, cellIn6, cellIn7, cellIn8, cellIn9 As Range

'Search matching loop ending at last row in column:
Dim lastRowA As Long
lastRowA = Sheets("Parts Sign Out").Range("A" & Rows.Count).End(xlUp).Row
Set rngOut1 = Worksheets("Parts Sign Out").Range("A4:A" & lastRowA)

Dim lastRowB As Long
lastRowB = Sheets("Parts Sign Out").Range("A" & Rows.Count).End(xlUp).Row
Set rngOut2 = Worksheets("Parts Sign Out").Range("B4:B" & lastRowB)

Dim lastRowC As Long
lastRowC = Sheets("Parts Sign Out").Range("A" & Rows.Count).End(xlUp).Row
Set rngOut3 = Worksheets("Parts Sign Out").Range("D4:D" & lastRowC)

Dim lastRowD As Long
lastRowD = Sheets("Parts Sign Out").Range("A" & Rows.Count).End(xlUp).Row
Set rngOut4 = Worksheets("Parts Sign Out").Range("E4:E" & lastRowD)

Dim lastRowE As Long
lastRowE = Sheets("In Stock Parts").Range("A" & Rows.Count).End(xlUp).Row
Set rngIn1 = Worksheets("In Stock Parts").Range("A3:A" & lastRowE)

Dim lastRowF As Long
lastRowF = Sheets("In Stock Parts").Range("A" & Rows.Count).End(xlUp).Row
Set rngIn2 = Worksheets("In Stock Parts").Range("B3:B" & lastRowF)

Dim lastRowG As Long
lastRowG = Sheets("In Stock Parts").Range("A" & Rows.Count).End(xlUp).Row
Set rngIn3 = Worksheets("In Stock Parts").Range("Q3:Q" & lastRowG)

Dim lastRowH As Long
lastRowH = Sheets("In Stock Parts").Range("A" & Rows.Count).End(xlUp).Row
Set rngIn4 = Worksheets("In Stock Parts").Range("W3:W" & lastRowH)

Dim lastRowI As Long
lastRowI = Sheets("In Stock Parts").Range("A" & Rows.Count).End(xlUp).Row
Set rngIn5 = Worksheets("In Stock Parts").Range("AC3:AC" & lastRowI)

Dim lastRowJ As Long
lastRowJ = Sheets("In Stock Parts").Range("A" & Rows.Count).End(xlUp).Row
Set rngIn6 = Worksheets("In Stock Parts").Range("AI3:AI" & lastRowJ)

Dim lastRowK As Long
lastRowK = Sheets("In Stock Parts").Range("A" & Rows.Count).End(xlUp).Row
Set rngIn7 = Worksheets("In Stock Parts").Range("AQ3:AQ" & lastRowK)

Dim lastRowL As Long
lastRowL = Sheets("In Stock Parts").Range("A" & Rows.Count).End(xlUp).Row
Set rngIn8 = Worksheets("In Stock Parts").Range("AU3:AU" & lastRowL)

Dim lastRowM As Long
lastRowM = Sheets("In Stock Parts").Range("A" & Rows.Count).End(xlUp).Row
Set rngIn9 = Worksheets("In Stock Parts").Range("BA3:BA" & lastRowM)

'Match Found:
For Each cellOut1 In rngOut1
    If IsEmpty(cellOut1.Value) Then Exit For

For Each CellOut2 In rngOut2
    If IsEmpty(CellOut2.Value) Then Exit For

For Each cellOut3 In rngOut3
    If IsEmpty(cellOut3.Value) Then Exit For

For Each cellOut4 In rngOut4
    If IsEmpty(cellOut4.Value) Then Exit For

For Each cellIn1 In rngIn1
    If IsEmpty(cellIn1.Value) Then Exit For
    If cellIn1 = cellOut1 Then Exit For
        cellIn3.Offset(0, 2) = cellOut4 + cellIn3.Offset(0, 2)

For Each cellIn2 In rngIn2
    If IsEmpty(cellIn2.Value) Then Exit For
    If cellIn2 = CellOut2 Then Exit For
        cellIn3.Offset(0, 2) = cellOut4 + cellIn3.Offset(0, 2)

For Each cellIn3 In rngIn3
    If IsEmpty(cellIn3.Value) Then Exit For
    If cellIn3 = cellOut3 Then Exit For
        cellIn3.Offset(0, 2) = cellOut4 + cellIn3.Offset(0, 2)

For Each cellIn4 In rngIn4
    If IsEmpty(cellIn4.Value) Then Exit For
    If cellIn4 = cellOut3 Then Exit For
        cellIn3.Offset(0, 2) = cellOut4 + cellIn3.Offset(0, 2)

For Each cellIn5 In rngIn5
    If IsEmpty(cellIn5.Value) Then Exit For
    If cellIn5 = cellOut3 Then Exit For
        cellIn3.Offset(0, 2) = cellOut4 + cellIn3.Offset(0, 2)

For Each cellIn6 In rngIn6
    If IsEmpty(cellIn6.Value) Then Exit For
    If cellIn6 - cellOut3 Then Exit For
        cellIn3.Offset(0, 2) = cellOut4 + cellIn3.Offset(0, 2)

For Each cellIn7 In rngIn7
    If IsEmpty(cellIn7.Value) Then Exit For
    If cellIn7 = cellOut3 Then Exit For
        cellIn3.Offset(0, 2) = cellOut4 + cellIn3.Offset(0, 2)

For Each cellIn8 In rngIn8
    If IsEmpty(cellIn8.Value) Then Exit For
    If cellIn8 = cellOut3 Then Exit For
        cellIn3.Offset(0, 2) = cellOut4 + cellIn3.Offset(0, 2)

For Each cellIn9 In rngIn9
    If IsEmpty(cellIn9.Value) Then Exit For
    If cellIn9 = cellOut3 Then
        cellIn3.Offset(0, 2) = cellOut4 + cellIn3.Offset(0, 2)
    End If
Next cellIn9

Next cellIn8
    
Next cellIn7

Next cellIn6

Next cellIn5

Next cellIn4

Next cellIn3

Next cellIn2

Next cellIn1

Next cellOut4

Next cellOut3

Next CellOut2

Next cellOut1

End Sub
question from:https://stackoverflow.com/questions/65890063/writing-a-vba-code-for-a-button-that-is-supposed-to-update-inventory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...