gpt4 book ai didi

excel - VBA 正在绕过我的 for 循环而不执行它

转载 作者:行者123 更新时间:2023-12-04 22:22:27 26 4
gpt4 key购买 nike

我正在尝试为可变长度数据集中的每个样本创建一个唯一的 ID。为此,我想使用名为 Name 和 Sample Type 的两个数据字符串的一部分。我希望我沿着列中的每一行向下走并取出每个字符串的片段并将它们放在一起,但是当我单步执行循环时,它永远不会进入我的循环,只会在它周围。有人能告诉我为什么吗?

Sheets("Data").Activate
setlastrow = Sheets("Data").Range("b5000").End(xlUp).Row
setlastcol = Sheets("Data").Cells(5, Columns.Count).End(xlToLeft).Column 'this is still assuming that row 5 has the header in it

colname = Rows(5).Find("Name", LookAt:=xlWhole).Column ' this can be repeated for any other columns we want to asign values to. These variables will make the rest of this much easier
colSampleText = Rows(5).Find("Sample Text", LookAt:=xlWhole).Column

For i = 6 To lastrow
Sheets("Data").Range(Cells(i, 1)) = workbookfunction.if(workbookfunction.CountIf(Range(Cells(6, colname), Cells(i, colname)), Cells(i, colname)) < 10, "0", "") & workbookfunction.CountIf(Range(Cells(6, colname), Cells(i, colname)), Cells(i, colname) & "-" & Left(Cells(i, colSampleText), 5))
'this should find the unique identifying infomation for each sample and analyte
Next i

最佳答案

您的代码中有两个主要错误 - 加上一个小错误。一是结构性的。您声明非您使用的变量。这就像在说,“既然我不会开车,我还不如闭上眼睛,我们加速前进”。这并非没有逻辑,但对于将您带到您想去的地方几乎没有什么帮助。

另一个是您希望 VBA 执行的工作表函数和您希望分配给 Excel 执行的单元格的函数之间的混淆。将复杂的公式写入单元格比让 VBA 计算复杂的公式更困难。对于该方法,如果要在 VBA 中创建公式,应首先将其分配给字符串,例如 MyFormula = "=COUNTIF(D6:D12, "MyName")"然后,在测试之后,将该字符串分配给单元格的 Formula属性,例如 Cells(R, ClmName).Formula = MyFormula" .在下面的代码中,我选择让 VBA 进行计算。由于并不完全清楚你想要什么(错误的代码永远不是展示你意图的好方法!)请修改它。在 VBA 中比在工作表函数中更容易。

Private Sub Test()

Dim LastRow As Long
Dim LastClm As Long
Dim ClmName As Long ' R use "col" for color, "clm" for column
Dim ClmSampleText As Long
Dim CountRng As Range
Dim Output As Variant
Dim R As Long ' R use R for row, C for column

Sheets("Data").Activate
LastRow = Sheets("Data").Range("b5000").End(xlUp).Row
' this is still assuming that row 5 has the header in it
LastClm = Sheets("Data").Cells(5, Columns.Count).End(xlToLeft).Column

' this can be repeated for any other columns we want to asign values to.
' These variables will make the rest of this much easier
ClmName = Rows(5).Find("Name", LookAt:=xlWhole).Column
ClmSampleText = Rows(5).Find("Sample Text", LookAt:=xlWhole).Column

For R = 6 To LastRow
'this should find the unique identifying infomation for each sample and analyte
Set CountRng = Range(Cells(6, ClmName), Cells(R, ClmName))
Output = WorksheetFunction.CountIf(CountRng, Cells(R, ClmName).Value)
If Output < 10 Then Output = 0
Cells(R, 1).Value = CStr(Output) & "-" & Left(Cells(R, ClmSampleText).Value, 5)
Next R
End Sub

“小”错误源于您对 Cell 缺乏了解。目的。一个单元格是 Range .它有许多属性,例如 Cell.RowCell.ColumnCell.Address ,以及其他属性,如 Cell.ValueCell.Formula . Value 属性是默认值。因此 CellCell.Value 相同 但是 不总是。在这个例子中,通过不考虑 Cell.Value你也忽略了 Cell.Formula ,并通过放置 Cell变成 WorksheetFunction您将 VBA 混淆为您的意思,单元格值或单元格范围。由于所有参与者都感到困惑,结果是可以预见的。

建议总是写 Cell.Value当您指的是单元格的值并使用 Cell仅当您指的是范围时。

关于excel - VBA 正在绕过我的 for 循环而不执行它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61378423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com