到目前为止,我对这段代码有疑问: 如果第2个单元格较大,它应该计算第2个单元格与第4个单元格的差值,并将其打印在原始值下方的第二行中(与第78行一样),然后写入该值(该值位于原始表的下方)在第4列中,直到“付清”第2个值,然后再处理其余文件。 到目前为止,我的代码如下:
Sub testro()
Const cSheet As String = "Procenty"
Const cRange As String = "A2:D71"
Const cel As Long = 4
Const cCol As Variant = "A"
Dim vntS As Variant
Dim vntT As Variant
Dim i As Long, r As Long
Dim emptyRow As Long
Dim kom As Double, komz As Double, kredyt As Double
Dim roz As Double, komr As Double, komn As Double
Dim dz As Date, dw As Date
vntS = ThisWorkbook.Worksheets(cSheet).Range(cRange).Value
ReDim vntT(1 To 2 * UBound(vntS), 1 To cel)
kredyt = 0
r = 1
For i = 1 To UBound(vntS)
dz = vntS(i, 1)
komz = vntS(i, 2)
dw = vntS(i, 3)
kom = vntS(i, 4)
vntT(r, 1) = dz
vntT(r, 2) = komz
vntT(r, 3) = dw
vntT(r, 4) = kom
r = r + 1
If komz > kom Then
komr = komz - kom
vntT(r, 1) = dz
vntT(r, 2) = komr
vntT(r, 3) = dw
vntT(r, 4) = kom
r = r + 1
ElseIf komz < kom Then
komn = kom - komz
vntT(r, 3) = dw
vntT(r, 4) = komn
r = r + 1
ElseIf komz = kom Then
vntT(r, 1) = dz
vntT(r, 2) = komz
vntT(r, 3) = dw
vntT(r, 4) = kom
End If
Next
With ThisWorkbook.Worksheets(cSheet)
emptyRow = .Columns(cCol).Find("*", , xlFormulas, _
xlWhole, xlByColumns, xlPrevious).Row + 1
'emptyRow = WorksheetFunction.CountA(.Columns(cCol)) + 1
.Cells(emptyRow, cCol).Resize(UBound(vntT), UBound(vntT, 2)) = vntT
.Cells(emptyRow, cCol) = kredyt
End With
End Sub
编辑:这是它的外观(我在这里手动完成)screenshot good EDIT2:这是在宏screenshot before之前的样子 编辑3:Screenshot now EDIT4:changed这是我完成您在上面所做的操作后的屏幕截图:在带黑色下划线的行中,有9.11.2015和828的应为30.11.2015和312,11,应检查该行是否与如果检查以下值,则结清帐单,依此类推
答案 0 :(得分:1)
我很难理解您要做什么。最终,我不得不尝试用英文名称替换您的变量kom,komz,komr,komn(抱歉,我是爱尔兰人,英语是我唯一的语言)
我可能完全错了,但是我的猜测是;
komz ... the TotalDebtBefore (the Debt Brought Forward)
kom .... the amount due or paid now - the installment
komr ... the RemainingDebt (Carried Forward)
komn ... An Overpaid Balance
有了这些含义,我就能理解您的代码
Sub TestRev2()
Const cSheet As String = "Procenty"
Const cRange As String = "A2:D71"
Const cel As Long = 4
Const cCol As Variant = "A"
' My best Guess of variable meanings
' DebtBFwd = k o m z - x9
' InstallmentAmt = k o m - x10
' DebtCFwd = k o m r - x3
' OverPaid = k o m n - x3
Dim vntS As Variant
Dim vntT As Variant
Dim i As Long, r As Long
Dim emptyRow As Long
Dim kom As Double, komz As Double, kredyt As Double
Dim roz As Double, komr As Double, komn As Double
Dim dz As Date, dw As Date
vntS = ThisWorkbook.Worksheets(cSheet).Range(cRange).Value
ReDim vntT(1 To 2 * UBound(vntS), 1 To cel)
kredyt = 0
r = 1
For i = 1 To UBound(vntS)
dz = vntS(i, 1)
komz = vntS(i, 2)
dw = vntS(i, 3)
kom = vntS(i, 4)
vntT(r, 1) = dz
vntT(r, 2) = komz
vntT(r, 3) = dw
vntT(r, 4) = kom
r = r + 1
If komz > kom Then
komr = komz - kom
vntT(r, 1) = dz
vntT(r, 2) = komr ' DebtCFwd ?
vntT(r, 3) = dw
vntT(r, 4) = kom ' InstallmentAmt ?
r = r + 1
End If
If komz < kom Then
komn = kom - komz
vntT(r, 3) = dw
vntT(r, 4) = komn ' Overpaid Amt ?
r = r + 1
End If
If komz = kom Then
vntT(r, 1) = dz
vntT(r, 2) = komz ' DebtBFwd ?
vntT(r, 3) = dw
vntT(r, 4) = kom ' InstallmentAmt ?
' Following Line Added
r = r + 1
End If
Next
With ThisWorkbook.Worksheets(cSheet)
emptyRow = .Columns(cCol).Find("*", , xlFormulas, xlWhole, xlByColumns, xlPrevious).Row + 1
.Cells(emptyRow, cCol).Resize(UBound(vntT), UBound(vntT, 2)) = vntT
.Cells(emptyRow, cCol) = kredyt
End With
End Sub
简而言之,您似乎错过了台词
r = r + 1
当komz = kom
无论您对变量的含义是对还是错,您都需要r = r + 1
您的If,if,if用法掩盖了该问题。所以我重新安排了它们,因为这些IF永远不会重叠
如果这不是导致问题的唯一原因,那么我现在已经很好地掌握了您的代码
================================================ =================== 附录-第3次修改后
因此,如果您的逻辑(在代码中)正确(并且可能不是),则问题出在代码的这一部分,并且只能是2行中的1行。
If komz > kom Then
komr = komz - kom
vntT(r, 1) = dz
vntT(r, 2) = komr ' DebtCFwd ?
vntT(r, 3) = dw
' Either the next line is wrong
vntT(r, 4) = kom ' InstallmentAmt ?
r = r + 1
End If
If komz < kom Then
komn = kom - komz
vntT(r, 3) = dw
' Or this next line is wrong
vntT(r, 4) = komn ' Overpaid Amt ?
r = r + 1
End If
我添加了2条额外的注释,以显示哪些行可能导致第4列不正确
但是,如果您向我解释kom,komn,komr和komz这4个变量的含义,我可以帮助您解决逻辑-我怀疑这也可能是错误的
================================================ =================== 附录2-允许代码告诉您它在做什么
首先,您需要按如下所示将vntT数组的大小增加1列
ReDim vntT(1 To 3 * UBound(vntS), 1 To cel + 1) ' Added the + 1 at end
然后,我们需要在以下各个位置将消息放入该额外的数组列中
vntT(r, 1) = dz
vntT(r, 2) = komz
vntT(r, 3) = dw
vntT(r, 4) = kom
vntT(r, 5) = " .. A"
r = r + 1
If komz > kom Then
komr = komz - kom
vntT(r, 1) = dz
vntT(r, 2) = komr ' DebtCFwd ?
vntT(r, 3) = dw
vntT(r, 4) = kom ' InstallmentAmt ?
vntT(r, 5) = " .. komz > kom"
r = r + 1
End If
If komz < kom Then
komn = kom - komz
vntT(r, 3) = dw
vntT(r, 4) = komn ' Overpaid Amt ?
vntT(r, 5) = " .. komz < kom"
r = r + 2
End If
If komz = kom Then
vntT(r, 1) = dz
vntT(r, 2) = komz ' DebtBFwd ?
vntT(r, 3) = dw
vntT(r, 4) = kom ' InstallmentAmt ?
vntT(r, 5) = " .. komz = kom"
' Following Line Added
r = r + 1
End If
上面有4行代码,每行代码将数据(msgs)写入vntT(r,5) 这将帮助您显示代码出了问题的地方。
================================================
以下是代码获取源数据的地方...将其放入变量
For i = 1 To UBound(vntS)
dz = vntS(i, 1)
komz = vntS(i, 2)
dw = vntS(i, 3)
kom = vntS(i, 4)
因此,vntS数组保存了源数据 vntT数组保存输出数据-在某些情况下是错误的
在将其放入“输出”数组之前,您可能需要一个新变量来获取其他日期或计算新值
正如我在评论中所说,我不知道您要实施的完整业务案例,也没有数据。