昨天公司給我個任務讓做個倉庫物品管理的表格,第一次用excel中VBA寫了個簡單的小程序
1.需求
弄一個物品入庫清單和物品領用清單,根據這兩個清單自動生成一個剩余物品表
物品入庫清單:
物品領用表:
3.程序
初中寫過VB, 所以vba語法感覺很親切啊
Private Sub Worksheet_SelectionChange(ByVal Target As Range) ' 初始化數值 Dim RK_Row As Integer '入庫表中的行數 RK_Row = 3 Dim RK_Col As Integer '入庫表中的列數 RK_Col = 3 Dim LY_Row As Integer '領用表中的行數 LY_Row = 3 Dim LY_Col As Integer '領用表中的行數 LY_Col = 3 Dim Num As Integer '領用數量 Num = 0 Dim Recent As Date '最近領用日期 Recent = #1/1/2000# While Len(Sheet3.Cells(RK_Row, 3)) <> 0 '遍歷入庫表的每一行 Sheet1.Cells(RK_Row, 3) = Sheet3.Cells(RK_Row, 3) Sheet1.Cells(RK_Row, 4) = Sheet3.Cells(RK_Row, 4) Sheet1.Cells(RK_Row, 5) = Sheet3.Cells(RK_Row, 5) While Len(Sheet2.Cells(LY_Row, 3)) <> 0 '遍歷領用表中的每一行 If Sheet2.Cells(LY_Row, 3) = Sheet3.Cells(RK_Row, 3) Then Num = Num + Sheet2.Cells(LY_Row, 7) If IsDate(Sheet2.Cells(LY_Row, 2)) Then Recent = Sheet2.Cells(LY_Row, 2) End If End If LY_Row = LY_Row + 1 Wend If Num <> 0 Then Sheet1.Cells(RK_Row, 2) = Recent ElseIf Recent = #1/1/2000# Then Sheet1.Cells(RK_Row, 2) = "日期不明" Else Sheet1.Cells(RK_Row, 2) = "暫未借出" End If Sheet1.Cells(RK_Row, 6) = Sheet3.Cells(RK_Row, 6) - Num LY_Row = 3 Num = 0 RK_Row = RK_Row + 1 Wend End Sub
4.結果
6.總結
VBA其實還是挺簡單的,上面也就是個初中水平的程序。不過如果他們這個物品領用寫的太雜亂了有很多寫的和倉庫入庫表的名稱都不一樣,都沒有計算上。其實還是用VB或者其他語言做個倉庫管理程序比較好,但不給錢誰做呢【手動攤手】。