ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[分享] 见证成长历程---我的答疑解难代码汇总

  [复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-4-24 18:56 | 显示全部楼层
用VBA代码条件求和
http://club.excelhome.net/thread-1341898-1-1.html
(出处: ExcelHome技术论坛)
  1. Sub 收发存汇总()
  2.    Application.ScreenUpdating = False
  3.    Set d = CreateObject("Scripting.Dictionary")
  4.    Set d1 = CreateObject("Scripting.Dictionary")
  5.    Set d2 = CreateObject("Scripting.Dictionary")
  6.    Range("o3:r" & Cells(Rows.Count, 18).End(3).Row).ClearContents
  7.    Arr = Sheet2.[a1].CurrentRegion
  8.    For i = 3 To UBound(Arr)
  9.       d(Arr(i, 11)) = d(Arr(i, 11)) + Arr(i, 15)
  10.       d1(Arr(i, 11)) = d1(Arr(i, 11)) + Arr(i, 19)
  11.    Next
  12.    Brr = Sheet3.[a1].CurrentRegion
  13.    For i = 3 To UBound(Brr)
  14.       d2(Brr(i, 9)) = d2(Brr(i, 9)) + Brr(i, 11)
  15.    Next
  16.    Crr = [a1].CurrentRegion
  17.    For i = 3 To UBound(Crr)
  18.        If d.exists(Crr(i, 8)) Then
  19.             Cells(i, 15) = d(Crr(i, 8))
  20.             Cells(i, 18) = d1(Crr(i, 8))
  21.             Cells(i, 16) = d2(Crr(i, 8))
  22.             Cells(i, 17) = Cells(i, 14) + Cells(i, 15) - Cells(i, 16)
  23.        End If
  24.    Next
  25.    Application.ScreenUpdating = True
  26. End Sub
  27. Sub 当前库存()
  28.    Application.ScreenUpdating = False
  29.    Set d = CreateObject("Scripting.Dictionary")
  30.    Range("n3:n" & Cells(Rows.Count, 14).End(3).Row).ClearContents
  31.    Arr = Sheet1.[a1].CurrentRegion
  32.    For i = 3 To UBound(Arr)
  33.       d(Arr(i, 8)) = d(Arr(i, 8)) + Arr(i, 17)
  34.    Next
  35.    Brr = [a1].CurrentRegion
  36.    For i = 3 To UBound(Brr)
  37.        If d.exists(Brr(i, 9)) Then
  38.             Cells(i, 14) = d(Brr(i, 9))
  39.        End If
  40.    Next
  41.    Application.ScreenUpdating = True
  42. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-4-25 22:11 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
帮忙修改一个数据引用的VB,谢谢v
http://club.excelhome.net/thread-1342252-1-1.html
(出处: ExcelHome技术论坛)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
    Set wb = GetObject(ThisWorkbook.Path & "\" & "信息.xls")
    Set d = CreateObject("scripting.dictionary")
    With Workbooks("信息.xls")
        arr = .Sheets(1).UsedRange
        For i = 2 To UBound(arr)
            d(arr(i, 1)) = arr(i, 2) & "/" & arr(i, 3) & "/" & arr(i, 4)
        Next
    End With
    If d.exists(Target.Value) Then
        s = Split(d(Target.Value), "/")
        Target.Offset(, 1) = s(1)
        Target.Offset(, 2) = s(2)
        Target.Offset(, 3) = s(0)
    Else
        MsgBox "对不起,没有此编号!"
    End If
    Set wb = Nothing
    Set d = Nothing
End If
End Sub

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-4-25 22:13 | 显示全部楼层
本帖最后由 lsc900707 于 2017-4-25 22:15 编辑

vba代码多条件求和
http://club.excelhome.net/thread-1342320-1-1.html
(出处: ExcelHome技术论坛)
  1. Sub lsc()
  2.      Set d = CreateObject("Scripting.Dictionary")
  3.       Range("c2:c" & Cells(Rows.Count, 1).End(3).Row).ClearContents
  4.       Arr = Sheet2.[a1].CurrentRegion
  5.       For i = 1 To UBound(Arr)
  6.           d(Arr(i, 1)) = d(Arr(i, 1)) + Arr(i, 2)
  7.      Next
  8.      Brr = [a1].CurrentRegion
  9.      For i = 2 To UBound(Brr)
  10.           Cells(i, 3) = d(Brr(i, 1)) + d(Brr(i, 2))
  11.      Next
  12. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-4-27 21:12 | 显示全部楼层
跨工作簿查询的:
如何用VBA来执行vlookup,是否用find来执行?
http://club.excelhome.net/thread-1342628-1-1.html
(出处: ExcelHome技术论坛)
  1. Sub lsc()
  2.     Set wb = GetObject(ThisWorkbook.Path & "" & "时间节点导出20170426214406.xlsx")
  3.     Set d = CreateObject("scripting.dictionary")
  4.     With Workbooks("时间节点导出20170426214406.xlsx")
  5.         arr = .Sheets(1).UsedRange
  6.         For i = 2 To UBound(arr)
  7.             d(arr(i, 1)) = arr(i, 2)
  8.         Next
  9.     End With
  10.     brr = [a1].CurrentRegion
  11.     For i = 2 To UBound(brr)
  12.         Cells(i, 2) = d(brr(i, 1))
  13.     Next
  14.     Set wb = Nothing
  15.     Set d = Nothing
  16. End Sub
复制代码

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-4-27 21:14 | 显示全部楼层
上一个求助帖子还做了一个工作表事件的,一并贴上来:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
    Set wb = GetObject(ThisWorkbook.Path & "\" & "时间节点导出20170426214406.xlsx")
    Set d = CreateObject("scripting.dictionary")
    With Workbooks("时间节点导出20170426214406.xlsx")
        arr = .Sheets(1).UsedRange
        For i = 2 To UBound(arr)
            d(arr(i, 1)) = arr(i, 2)
        Next
    End With
    If d.exists(Target.Value) Then
        Target.Offset(, 1) = d(Target.Value)
    Else
        MsgBox "对不起,没有此料号!"
    End If
    Set wb = Nothing
    Set d = Nothing
End If
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-4-27 21:18 | 显示全部楼层
修改并添加查询代码:
  1. Sub 送检制单()
  2.     Application.ScreenUpdating = False
  3.     lsc
  4.     Dim y As String, Dh As String, T As String, i
  5.     y = "SJ" & Format(Now, "yyyymm")
  6.     With Sheets("送检制单")
  7.         .Range("G3") = y
  8.         .Range("C7") = Format(Now, "yyyy/mm/dd hh:mm")
  9.         T = Application.WorksheetFunction.Text(.Range("H3"), "000")
  10.         Dh = .Range("G3") & T
  11.             If .Range("C5") = "" Then
  12.                 MsgBox "请选择送检人"
  13.                 End
  14.             End If
  15.     End With
  16.     If Sheets("送检制单").Range("B10").Value = "" Then
  17.         MsgBox "请输入零件号"
  18.         End
  19.     End If
  20.     If Sheets("送检制单").Range("E10").Value = "" Then
  21.         MsgBox "请输入数量"
  22.         End
  23.         Else
  24.     For i = 10 To 19
  25.         If Cells(i, 2) <> "" Then
  26.             With Sheets("送检记录")
  27.                 r = .Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1
  28.                 .Cells(r, 1) = Range("C7")   '发货日期
  29.                 .Cells(r, 2) = Cells(i, 2)   '零件简码
  30.                 .Cells(r, 3) = Cells(i, 3)   '零件名称
  31.                 .Cells(r, 4) = Cells(i, 4)   '图号
  32.                 .Cells(r, 5) = Cells(i, 5)   '送检数量
  33.                 .Cells(r, 6) = Cells(i, 6)   '炉号
  34.                 .Cells(r, 7) = Cells(i, 7)   '批号
  35.                 .Cells(r, 8) = Cells(i, 8)   '供应商
  36.                 .Cells(r, 9) = Dh            '单号
  37.                 .Cells(r, 10) = Range("C5")  '送检人
  38.             End With
  39.         End If
  40.     Next
  41.     End If
  42.     With Sheets("送检制单")
  43.         '.PrintOut Copies:=1
  44.         .Range("H3") = T + 1
  45.         .Range("C5") = ""
  46.         .Range("B10:H19").ClearContents
  47.     End With
  48.     'ActiveWorkbook.Save
  49.     Application.ScreenUpdating = True
  50.     'ActiveWorkbook.Close savechanges:=ture
  51. End Sub
  52. Sub lsc()
  53.     Set d = CreateObject("Scripting.Dictionary")
  54.     arr = Sheet1.[a1].CurrentRegion
  55.     For i = 2 To UBound(arr)
  56.         d(arr(i, 1)) = arr(i, 2) & "/" & arr(i, 3) & "/" & arr(i, 4)
  57.     Next
  58.     brr = Range("a10:h" & Cells(Rows.Count, 1).End(3).Row)
  59.     For i = 1 To UBound(brr)
  60.        If d.exists(brr(i, 2)) Then
  61.            s = Split(d(brr(i, 2)), "/")
  62.            Cells(i + 9, 3) = s(0)
  63.            Cells(i + 9, 4) = s(1)
  64.            Cells(i + 9, 8) = s(2)
  65.        End If
  66.    Next
  67.    Set d = Nothing
  68. End Sub
复制代码

VBA触发代码范围设置问题
http://club.excelhome.net/thread-1342563-1-1.html
(出处: ExcelHome技术论坛)

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-4-27 21:20 | 显示全部楼层
帮忙修改代码:
录入数据时出现了多余的空行
http://club.excelhome.net/thread-1342581-1-1.html
(出处: ExcelHome技术论坛)
  1. With Sheets("报表数据")
  2. x = .Cells(Rows.Count, 1).End(xlUp).Row + 1
  3. For i = 6 To 25 '商品信息分二十行,为了简化代码,使用循环语句
  4. If Cells(i, 2) <> "" Then
  5. .Cells(x + m, 7) = [g4] '制单日期,发料日期
  6. .Cells(x + m, 2) = [j2&k2]  '领料字号
  7. .Cells(x + m, 3) = [j3] '物资类别
  8. .Cells(x + m, 4) = Cells(i, 3)   '物资名称
  9. .Cells(x + m, 1) = Cells(i, 1) '物资编号
  10. .Cells(x + m, 5) = Cells(i, 5)  '规格型号
  11. .Cells(x + m, 6) = Cells(i, 6)  '单位
  12. .Cells(x + m, 13) = Cells(i, 7)  '数量
  13. .Cells(x + m, 11) = Cells(i, 8)  '单价
  14. .Cells(x + m, 14) = Cells(i, 9)  '合计金额
  15. .Cells(x + m, 16) = [q2] '出库方式
  16. m = m + 1
  17. End If
  18. Next
  19. End With
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-4-27 21:23 | 显示全部楼层
帮忙修改一个数据引用的VB,谢谢v
http://club.excelhome.net/thread-1342252-1-1.html
(出处: ExcelHome技术论坛)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
    Set wb = GetObject(ThisWorkbook.Path & "\" & "信息.xls")
    Set d = CreateObject("scripting.dictionary")
    With Workbooks("信息.xls")
        arr = .Sheets(1).UsedRange
        For i = 2 To UBound(arr)
            d(arr(i, 1)) = arr(i, 2) & "/" & arr(i, 3) & "/" & arr(i, 4)
        Next
    End With
    If d.exists(Target.Value) Then
        s = Split(d(Target.Value), "/")
        Target.Offset(, 1) = s(1)
        Target.Offset(, 2) = s(2)
        Target.Offset(, 3) = s(0)
    Else
        MsgBox "对不起,没有此编号!"
    End If
    Set wb = Nothing
    Set d = Nothing
End If
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-4-27 21:26 | 显示全部楼层
VBA赋植给多列的问题
http://club.excelhome.net/thread-1342412-1-1.html
(出处: ExcelHome技术论坛)
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. If Target.Column = 1 Then
  3.     Set d = CreateObject("scripting.dictionary")
  4.     arr = Sheet1.[a2].CurrentRegion
  5.     For i = 2 To UBound(arr)
  6.         d(arr(i, 2)) = arr(i, 3) & "/" & arr(i, 4) & "/" & arr(i, 7)
  7.     Next
  8.     If d.exists(Target.Value) Then
  9.         s = Split(d(Target.Value), "/")
  10.         Target.Offset(, 1) = s(0)
  11.         Target.Offset(, 2) = s(1)
  12.         Target.Offset(, 3) = s(2)
  13.     Else
  14.         MsgBox "对不起,没有此货号!"
  15.     End If
  16.     Set d = Nothing
  17. End If
  18. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2017-4-27 21:53 | 显示全部楼层
本帖最后由 lsc900707 于 2017-4-27 21:54 编辑

有条件的复制
http://club.excelhome.net/thread-1342742-1-1.html
(出处: ExcelHome技术论坛)
  1. Private Sub CommandButton1_Click()
  2.     Dim brr(1 To 1000, 1 To 4)
  3.     For Each sht In Worksheets
  4.         If sht.Name = Trim(TextBox1.Text) Then
  5.             Worksheets(sht.Name).Select
  6.         End If
  7.     Next
  8.     arr = Sheet4.[a1].CurrentRegion
  9.     For i = 2 To UBound(arr)
  10.         If arr(i, 7) <> 1 Then
  11.             k = k + 1
  12.             For j = 1 To 4
  13.                 brr(k, j) = arr(i, j)
  14.             Next
  15.         End If
  16.     Next
  17.     If k = 0 Then
  18.         MsgBox "没有找到相关数据!"
  19.     Else
  20.         ActiveSheet.[a2:d1000].ClearContents
  21.         ActiveSheet.[a2].Resize(k, 4) = brr
  22.     End If
  23. End Sub
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

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

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

GMT+8, 2024-4-20 09:33 , Processed in 0.042321 second(s), 7 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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