我有一个excel工作簿,用户将文本文件导入excel工作簿。从那里我们希望计算自动完成。我正在计算从初始减去当前数据的计算。请参见图中蓝色轮廓框是当前数据(当前数据量是动态的,取决于用户导入的文本文件数)。所以我想从红色列中减去蓝色列。
我希望计算中的值打印在一个标题为“Calculations1”的单独的工作表上,但到目前为止,当我在我的列中运行代码值时,它会打印#NAME?如下图所示。
这是我的代码
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
有关如何在Calculations1表上打印实际值的任何想法?任何提示/帮助将不胜感激。提前谢谢!
答案 0 :(得分:2)
NewRng3.Formula = "=NewRng1 - NewRng2"
应该是
NewRng3.Formula = "=" & NewRng1.Address() & "-" & NewRng2.Address()