如何将期末余额从一张表中结转到新创建表的期初余额

时间:2018-10-14 01:05:01

标签: excel vba loops

我正在尝试编写一个代码,将每个产品的期末余额从一张纸中转移到新创建的纸张的起始余额单元中。 该代码适用于第一个产品,但是随后它抵消了列,因此未结转期末余额,而只有期初余额。以下是我的vba代码。每个产品的期末余额都位于第39行中,但偏移了4列。期初余额是第7行,且偏移量相同(4)。之所以存在该循环,是因为我将vba代码用于许多不同的工作簿,并且每个工作簿的产品数量也不同。

  

子newsheet()   '功能允许用户复制粘贴新表,   “清除了每个产品付款和预付款列中的所有内容,但格式保持不变。

Application.ActiveWorkbook.ActiveSheet.Copy After:=ActiveSheet
Range("C8:D38,H8:I38,M8:N38,R8:S38,W8:X38,AB8:AC38,AG8:AH38,AL8:AM38," & _
"AQ8:AR38 , AV8:AW38 , BA8:BB38 , BF8:BG38 , BK8:BL38 , BP8:BQ38 , BU8:BV38 , BZ8:CA38," & _
"CE8: CF38 , CJ8: CK38 , CO8: CP38 , CT8: CU38 , CY8: CZ38 , DD8: DE38 ").ClearContents

' Setting up new variables for the following procedure
' it will allow the user to copy paste ending balance of old sheet to beginning balance of the new sheet
Dim wks As Worksheet
Dim rng As Range
Dim rng2 As Range
Set wks = Application.ActiveWorkbook.ActiveSheet.Previous
Set rng = wks.Range("E39")
Set rng2 = Application.ActiveWorkbook.ActiveSheet.Range("E7")

Do While Not IsEmpty(rng)
' loops through the copied sheets facilities.
    rng.Copy
    rng2.PasteSpecial xlPasteValues

    Set rng = rng.Offset(0, 4)
    Set rng2 = rng2.Offset(0, 4)
Loop 'Loops back through the previous sheet until it doesnt find any product anymore.

结束子

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全理解您的问题,无论如何,我会尝试这样做:

Application.ActiveWorkbook.ActiveSheet.Copy After:=ActiveSheet
Range("C8:D38,H8:I38,M8:N38,R8:S38,W8:X38,AB8:AC38,AG8:AH38,AL8:AM38," & _
"AQ8:AR38 , AV8:AW38 , BA8:BB38 , BF8:BG38 , BK8:BL38 , BP8:BQ38 , BU8:BV38 , BZ8:CA38," & _
"CE8: CF38 , CJ8: CK38 , CO8: CP38 , CT8: CU38 , CY8: CZ38 , DD8: DE38 ").ClearContents

' Setting up new variables for the following procedure
' it will allow the user to copy paste ending balance of old sheet to beginning balance of the new sheet
Dim wks As Worksheet
Dim rng As Range
Dim rng2 As Range
Set wks = Application.ActiveWorkbook.ActiveSheet.Previous
Set rng = wks.Range("E39")
Set rng2 = Application.ActiveWorkbook.ActiveSheet.Range("E7")

Do While Not IsEmpty(rng)
' loops through the copied sheets facilities.

    rng2.Value2 = rng.Value2

    Set rng = rng.Offset(0, 4)
    Set rng2 = rng2.Offset(0, 4)
Loop 'Loops back through the previous sheet until it doesnt find any product anymore.

否则,请显示具有相同示例数据的工作表图像。