|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 纵鹤擒龙水中月 于 2022-5-12 11:41 编辑
初学VSTO,多次调试自身能力解决不了,特求助,看问题出在什么地方,谢了谢了
这个功能就是按耗用量拆分单据列表。VBA代码已在附件中
原VBA代码
- Sub 单据拆分() '
- On Error Resume Next
- Application.ScreenUpdating = False
- Application.Calculation = xlManual
- Dim r&, i&
- Dim arr, brr, crr, k1, t1
- Dim d As Object
- 'Dim d2 As Object
- Msg = MsgBox("提示:按确定执行,否则按取消退出!--------", vbOKCancel + vbDefaultButton2, "提示")
- '如果按“确定”键,那么执行下面代码
- If Msg = vbOKCancel Then
- Set d = CreateObject("scripting.dictionary")
- Sheets("拆分数量").Select
- wr = Cells(Rows.Count, 1).End(3).Row
- crr = Sheets("拆分数量").Range("A2:L" & wr).Value
-
- '遍历
- For i = 1 To UBound(crr)
- xm = crr(i, 1) '
- d(xm) = crr(i, 11) '
- Next
- 'd1 = d.keys
- 't1 = d.items
- Sheets("要拆分的单据").Select
- With Worksheets("要拆分的单据")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row '
- arr = .Range("A2:AW" & r).Value '
-
- ReDim brr(1 To UBound(arr), 1 To 1)
- For i = 1 To UBound(arr)
- xm = arr(i, 1) '
- If d.exists(xm) Then
- If d(xm) >= 0 Then
- If arr(i, 24) < d(xm) Then '
- '
- brr(i, 1) = arr(i, 24)
- Else
-
- brr(i, 1) = d(xm) '
- End If
- d(xm) = d(xm) + arr(i, 24) * -1
-
- End If
- End If
- Next
- Range("AW2:AW" & r).ClearContents '耗用数量 列
- .Range("AW2").Resize(UBound(brr), 1) = brr
- End With
- '否则
- Else
- '退出程序
- Exit Sub
- End If
- Application.Calculation = xlAutomatic
- Application.ScreenUpdating = True
- End Sub
复制代码 现在转VSTO的代码
- Sub 单据拆分() '
-
- On Error Resume Next
-
- xlApp.ScreenUpdating = False
-
- xlApp.Calculation = xlManual
-
- Dim r&, i&
-
- Dim arr, brr, crr, k1, t1
-
- Dim d As New Dictionary(Of String, String)
-
-
- Msg = MsgBox("提示:按确定执行,否则按取消退出!-------", vbOKCancel + vbDefaultButton2, "提示")
-
- '如果按“确定”键,那么执行下面代码
-
- If Msg = vbOKCancel Then
-
- xlApp.Sheets("拆分数量").Select
-
- wr = xlApp.Cells( xlApp.Rows.Count, 1).End(Excel.XlDirection.xlUp).Row
-
- crr = xlApp.Sheets("拆分数量").Range("A2:L" & wr).Value
-
-
-
- '遍历
-
- For i = 1 To UBound(crr)
-
- xm = crr(i, 1) '
-
- d(xm) = crr(i, 11) '
-
- Next
-
- 'd1 = d.keys
-
- 't1 = d.items
-
-
-
-
-
-
-
- xlApp.Sheets("要拆分的单据").Select
-
- With xlApp.Worksheets("要拆分的单据")
-
- r = .Cells(.Rows.Count, 1).End(xlUp).Row '
-
- arr = .Range("A2:AW" & r).Value '
-
-
-
- ReDim brr(0 To UBound(arr)-1, 0 To 0)
-
- For i = 1 To UBound(arr)
-
- xm = arr(i, 1) '
-
- If d.ContainsKey(xm) Then
-
- If d(xm) >= 0 Then
-
- If arr(i, 24) < d(xm) Then '
-
- '
-
- brr(i-1, 1) = arr(i, 24)
-
- Else
-
-
-
- brr(i-1, 1) = d(xm) '
-
- End If
-
- d(xm) = d(xm) + arr(i, 24) * -1
-
-
-
- End If
-
- End If
-
- Next
-
- xlApp.Range("AW2:AW" & r).ClearContents '耗用数量 列
-
- .Range("AW2").Resize(UBound(brr), 1) = brr
-
- End With
-
- '否则
-
- Else
-
- '退出程序
-
- Exit Sub
-
- End If
-
- xlApp.Calculation = xlAutomatic
-
- xlApp.ScreenUpdating = True
-
- End Sub
-
复制代码
附件.rar
(22.83 KB, 下载次数: 3)
|
|