简体   繁体   English

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

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

I have a lot of files and want to find the areas under portions of the curve. 我有很多文件,想找到曲线各部分下的区域。 I define how many curves, the X1 and X2. 我定义了X1和X2几条曲线。

The macro then finds the row x1 and x2 and then should give me the row number that they are contained in, but I keep getting an error at this step. 宏然后找到行x1和x2,然后应该给我它们包含的行号,但是在此步骤中,我一直遇到错误。

Then later on it uses these values to calcultae the area. 然后,稍后使用这些值对区域进行煅烧。

It worked once, I made a change to a variable and have never gotten it to work right since. 它曾经工作过,我对变量进行了更改,此后再也没有使它正常工作。

Here is the code: 这是代码:

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

You need to Set the lines below, since they are to be a range variable, and a range is an object. 您需要Set以下行,因为它们将是范围变量,而范围是一个对象。

Change this: 更改此:

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)

To: 至:

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)

This change will allow for the .Row property to be pulled from your range object. 此更改将允许从您的范围对象中拉出.Row属性。

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

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