VBA Excel使用.row将变量设置为行号

时间:2015-04-08 19:19:50

标签: excel vba

我有很多文件,想要找到曲线部分下方的区域。 我定义了多少曲线,X1和X2。

然后宏找到行x1和x2,然后应该给我包含它们的行号,但是我在这一步仍然收到错误。

然后,它会使用这些值来计算该区域。

它工作过一次,我对一个变量进行了更改,从那时起就没有让它正常工作。

以下是代码:

Sub Macro1()

Dim myRange, TotalPeak, TotalBack, FoundCell_1, FoundCell_2, J, FileNumber, Point_1, Point_2, CPoint_1, CPoint_2

FileNumber = InputBox("Number of curves")
Point_1 = InputBox("Enter point one of curve")
Point_2 = InputBox("Enter point two of curve")

For J = 1 To FileNumber

FoundCell_1 = ActiveSheet.Columns(1).Find(What:=Point_1, LookIn:=xlValues, LookAt:=xlPart)
FoundCell_2 = ActiveSheet.Columns(1).Find(What:=Point_2, LookIn:=xlValues, LookAt:=xlPart)

CPoint_1 = FoundCell_1.Row
CPoint_2 = FoundCell_2.Row

myRange = ActiveSheet.Range(Cells(CPoint_1, J + 1), Cells(CPoint_2, J + 1))
TotalPeak = Application.WorksheetFunction.Sum(myRange)
TotalBack = ((Cells(CPoint_1, J + 1) + Cells(CPoint_2, J + 1)) / 2) * Abs((CPoint_2 - CPoint_1))
Worksheets("Sheet2").Cells(J, 1) = TotalPeak - TotalBack

Next

End Sub

1 个答案:

答案 0 :(得分:0)

您需要Set下面的行,因为它们是范围变量,范围是对象。

改变这个:

FoundCell_1 = ActiveSheet.Columns(1).Find(What:=Point_1, LookIn:=xlValues, LookAt:=xlPart)
FoundCell_2 = ActiveSheet.Columns(1).Find(What:=Point_2, LookIn:=xlValues, LookAt:=xlPart)

要:

Set FoundCell_1 = ActiveSheet.Columns(1).Find(What:=Point_1, LookIn:=xlValues, LookAt:=xlPart)
Set FoundCell_2 = ActiveSheet.Columns(1).Find(What:=Point_2, LookIn:=xlValues, LookAt:=xlPart)

此更改将允许从您的范围对象中提取.Row属性。

相关问题