关于excel:减去定义为Ranges VBA的两列

Subtract Two Columns defined as Ranges VBA

我有一个Excel工作簿,用户在其中导入文本文件到Excel工作簿。从那里我们希望自动完成计算。我正在计算从初始值中减去当前数据。请查看图片,其中蓝色轮廓框是当前数据(当前数据量是动态的,取决于用户导入的文本文件数)。所以我想从红色列中减去蓝色列。enter image description here

我想把计算中的值打印在一张单独的题为"calculations1"的纸上,但是到目前为止,当我在我的列中运行代码时,这些值应该放在哪里,它会打印出名称?如下图所示。enter image description here

这是我的密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
Sub Calculations1()

Dim LastCol As Integer
Dim LastRow As Integer
Dim i As Integer
Dim A As Integer
Dim B As Integer
Dim hiddenws As Worksheet
Dim calcws As Worksheet
Dim iRng As Range
Dim cel As Range
Dim Rng1 As Range
Dim Rng2 As Range
Dim Rng3 As Range
Dim Rng4 As Range
Dim Rng5 As Range
Dim Rng6 As Range
Dim NewRng1 As Range
Dim NewRng2 As Range

A = Worksheets("Information Sheet").Range("E12").Value
B = Worksheets("Hidden").Range("B2").Value

Set calcws = Worksheets("Calculations1")
Set hiddenws = Worksheets("Hidden 2")
Set iRng = hiddenws.Range(hiddenws.Cells(1, 1), hiddenws.Cells(1, hiddenws.Cells(1, Columns.Count).End(xlToLeft).Column))

For i = 1 To B
If i <> B Then
    For Each cel In iRng
        If cel.Value ="A0_" & i Then
        With hiddenws
            Set Rng1 = cel.EntireColumn.Find(What:="", LookIn:=xlValues, lookat:=xlPart)
            Debug.Print Rng1.FormulaR1C1
            Set Rng2 = hiddenws.Cells(Rng1.Row - 1, Rng1.Column)
            Debug.Print Rng2.FormulaR1C1
            Set Rng3 = Cells(cel.Row + 1, cel.Column)
            Debug.Print Rng3.FormulaR1C1
        End With
        End If

        If cel.Value ="A0_" & A Then
        With hiddenws
            Set Rng4 = cel.EntireColumn.Find(What:="", LookIn:=xlValues, lookat:=xlPart)
            Debug.Print Rng4.FormulaR1C1
            Set Rng5 = hiddenws.Cells(Rng4.Row - 1, Rng4.Column)
            Debug.Print Rng5.FormulaR1C1
            Set Rng6 = Cells(cel.Row + 1, cel.Column)
            Debug.Print Rng6.FormulaR1C1
        End With
        End If
    Next cel

    With hiddenws
    Set NewRng1 = .Range(Rng3.Address &":" & Rng2.Address)
    Debug.Print NewRng1.Address
    Set NewRng2 = .Range(Rng6.Address &":" & Rng5.Address)
    Debug.Print NewRng2.Address
    End With

    With calcws
    Sheets("Calculations1").Activate
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column
    If LastCol > 1 Then
    LastCol = LastCol + 1
    End If
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row
    Cells(1, LastCol) ="Incre_Disp_A_" & i
    Cells(1, LastCol + 1) ="Cumul_Disp_A_" & i
    End With

    With calcws
    Set Rng7 = Cells(2, LastCol)
    Debug.Print Rng7.Address
    Set Rng8 = Cells(LastRow, LastCol)
    Debug.Print Rng8.Address
    Set NewRng3 = .Range(Rng7.Address &":" & Rng8.Address)
    Debug.Print NewRng3.Address
    NewRng3.Formula ="=NewRng1 - NewRng2"
    End With

End If
Next i
End Sub

对于如何在计算1表上打印实际值有什么想法吗?任何提示/帮助都将不胜感激。事先谢谢!


1
NewRng3.Formula ="=NewRng1 - NewRng2"

应该的 </P >

1
NewRng3.Formula ="=" & NewRng1.Address() &"-" & NewRng2.Address()