ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1506|回复: 8

[求助] 投降了,实在琢磨不出, 库存先进先出的vba代码,谁帮看看

[复制链接]

TA的精华主题

TA的得分主题

发表于 2014-1-28 14:31 | 显示全部楼层 |阅读模式

具体见附件。

弄了二天,实在是太绕脑袋了,脑袋现在全是浆糊,给绕进去了。

查了论坛很多先进先出的代码,都不涉及到多个料号,我这里是有很多重不同的料号,
先进先出,不考虑日期因素。
谁帮我检查一下代码,该怎么改?多谢了。


Book2.zip (19.87 KB, 下载次数: 15)



  1. Option Explicit

  2. Private Sub CommandButton1_Click()
  3. Dim item, xx, i As Integer
  4. Dim inputqty1, inputqty2, inputamt1, inputamt2, outputqty, outputqtyttl, outputamtttl As Long
  5. Range("j2:j65536").ClearContents

  6. 10: i = 2
  7.     xx = 2
  8.     inputqty1 = 0
  9.     inputqty2 = 0
  10.     inputamt1 = 0
  11.     inputamt2 = 0
  12.     outputqty = 0
  13.     outputqtyttl = 0
  14.     outputamtttl = 0

  15. 11: If Len(Cells(i, 10)) > 0 Then
  16.     i = i + 1
  17.         If i > Range("i65536").End(xlUp).Row Then
  18.         Exit Sub
  19.         End If
  20.     GoTo 11
  21.     End If

  22. 20: item = Cells(i, 5)
  23. 30: If Cells(i, 5) <> item Then
  24.     i = i + 1
  25.         If i > Range("i65536").End(xlUp).Row Then
  26.         GoTo 10
  27.         End If
  28.     GoTo 30
  29.     End If

  30.     outputqty = Cells(i, 9)
  31.     outputqtyttl = outputqtyttl + outputqty

  32. 50:     inputqty2 = inputqty1
  33.         inputamt2 = inputamt1

  34. 60:     With Sheets("INPUT")
  35.         If .Cells(xx, 4) = item Then
  36.         inputqty1 = inputqty1 + .Cells(xx, 8)
  37.         inputamt1 = inputamt1 + .Cells(xx, 9)
  38.         Else
  39.         xx = xx + 1
  40.             If xx > .Range("H65536").End(xlUp).Row Then
  41.                 i = i + 1
  42.                 GoTo 30
  43.             End If
  44.         GoTo 60
  45.         End If

  46.         If outputqtyttl <= inputqty1 Then
  47.             Cells(i, 10) = (1 - (inputqty1 - outputqtyttl) / .Cells(xx, 8)) * .Cells(xx, 9) + inputamt2 - outputamtttl
  48.             outputamtttl = outputamtttl + Cells(i, 10)   'actually cells(i,10)=0 currently
  49.             i = i + 1
  50.             xx = xx + 1
  51.                 If i > Range("i65536").End(xlUp).Row Then
  52.                     GoTo 10
  53.                 End If
  54.             GoTo 30
  55.         Else
  56.         xx = xx + 1
  57.         GoTo 50
  58.         End If
  59.         End With
  60. GoTo 10

  61. End Sub

复制代码


TA的精华主题

TA的得分主题

发表于 2014-1-28 16:00 | 显示全部楼层
see if help you

Book2 v1.rar

25.45 KB, 下载次数: 42

TA的精华主题

TA的得分主题

 楼主| 发表于 2014-1-28 16:10 | 显示全部楼层
KCFONG 发表于 2014-1-28 16:00
see if help you

大神,谢谢您了.

这个太搞脑子了,,,晕头转向的...
还好您帮忙,,多谢多谢!!!!!!!!!!!

TA的精华主题

TA的得分主题

发表于 2014-1-28 16:15 | 显示全部楼层
Did my macro help you and fix your problem ??

TA的精华主题

TA的得分主题

 楼主| 发表于 2014-1-28 16:39 | 显示全部楼层
KCFONG 发表于 2014-1-28 16:15
Did my macro help you and fix your problem ??

yes, terrific ,,,,

thanks so much !!!

TA的精华主题

TA的得分主题

 楼主| 发表于 2014-1-29 08:52 | 显示全部楼层
KCFONG 发表于 2014-1-28 16:15
Did my macro help you and fix your problem ??

sorry , i checked the code later ,and put special case for running , the result is mistake and don't match the expectation ,

i have to review all codes ,

will be highly appreciate if you here and can help ,

thanks at advanced.

TA的精华主题

TA的得分主题

 楼主| 发表于 2014-1-29 08:56 | 显示全部楼层
please find attachment and unzip it , originally use your code ,but incorrect result ,

example.zip

22.5 KB, 下载次数: 9

TA的精华主题

TA的得分主题

 楼主| 发表于 2014-1-29 10:31 | 显示全部楼层

自己解决了,
下面是最终代码:

  1. Private Sub CommandButton1_Click()
  2. On Error Resume Next
  3. Dim i, x As Integer
  4. Dim Item As String
  5. Dim itemQty, OutQty, OutAmt, InQty, InAmt As Double
  6. Range("j2:J65536").ClearContents

  7. For i = 2 To Range("I65536").End(xlUp).Row
  8.     Item = Cells(i, 5)
  9.     itemQty = Cells(i, 9)
  10.     OutQty = Application.WorksheetFunction.SumIf(Range("E1:I" & i - 1), Item, Range("i1:i" & i - 1))
  11.     OutAmt = Application.WorksheetFunction.SumIf(Range("E1:J" & i - 1), Item, Range("j1:J" & i - 1))
  12.    
  13.     With Sheets("INPUT")
  14.     For x = 2 To .Range("H65536").End(xlUp).Row
  15.         If .Cells(x, 4) = Item Then
  16.         InQty = InQty + .Cells(x, 8)
  17.         InAmt = InAmt + .Cells(x, 9)
  18.             If InQty >= OutQty + itemQty Then
  19.             Cells(i, 10) = (InAmt - (.Cells(x, 9) * (InQty - OutQty - itemQty) / .Cells(x, 8))) - OutAmt
  20.             Exit For
  21.             End If
  22.         End If
  23.     Next
  24.     End With
  25.     InQty = 0
  26.     InAmt = 0
  27. Next
  28. End Sub
复制代码

ok.zip

21.46 KB, 下载次数: 55

TA的精华主题

TA的得分主题

发表于 2014-1-29 11:12 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
透英文,呵呵
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-25 22:35 , Processed in 0.043700 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表