繁体   English   中英

VBA Excel将变量设置为带有.row的行号

[英]VBA Excel set variable to row number with .row

我有很多文件,想找到曲线各部分下的区域。 我定义了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

您需要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属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM