Sub AddNumbersOnOneRow
Const s_Field_ColA = "ColumnA"
Const s_Field_ColB = "ColumnB"
Const s_Field_ColC = "ColumnC"
Const s_Field_ColD = "ColumnD"
Dim iPsDepth As Integer
Dim iPsColA As Integer
Dim iPsColB As Integer
Dim iPsColC As Integer
Dim iPsColD As Integer
Dim lRow As Long
Dim dDepth As Double
Dim ColumnD As Double
'------------------------
'Determine the field positions of the necessary fields in the array.
If InitFieldsFnB(gs_Depth, iPsDepth, _
s_Field_ColA, iPsColA, _
s_Field_ColB, iPsColB, _
s_Field_ColC, iPsColC) _
Then
'One or more of the required fields missing from the table.
Exit Sub
End If
With gINTRules.GridData
iPsDepth = .FieldCol("Depth")
iPsColA = .FieldCol("ColumnA")
iPsColB = .FieldCol("ColumnB")
iPsColC = .FieldCol("ColumnC")
iPsColD = .FieldCol("ColumnD")
For lRow = 1 To glNumRows
ColumnD= CDbl(gsDataA(iPsColA, lRow))+ CDbl(gsDataA(iPsColB, lRow))
gsDataA(iPsColD, lRow)= CStr(ColumnD)
Next lRow
End With
End Sub
当我运行此宏时,我没有收到任何错误。但是,这个宏没有给我答案,D栏中没有。
我想将列A和ColumnB添加到columnD中 ![这是我的表,我想将列A和ColumnB添加到columnD中。但是当我运行宏时,我什么都没得到。]
我怀疑以下几行给我带来了麻烦:
For lRow = 1 To glNumRows
ColumnD= CDbl(gsDataA(iPsColA, lRow))+ CDbl(gsDataA(iPsColB, lRow))
gsDataA(iPsColD, lRow)= CStr(ColumnD)
Next lRow
非常感谢任何帮助!!!
我的主要内容是:
'#LibInclude "common procedures"
Option Explicit
Public Sub Main
Dim gsDataA As Variant
Dim glNumRows As Integer
With gINTRules.GridData
'Put the grid data into a working string data array.
gsDataA = .DataArray
glNumRows = UBound(gsDataA, 2)
'Put the modified data array back into the input grid.
.DataArray = gsDataA
'Success is True if there were no errors.
gINTRules.Success = CBool(.ErrorCol = 0)
End With
End Sub
答案 0 :(得分:0)
除非您在其他位置设置值,否则glNumRows在您发布的代码中未分配值,因此它的默认值为0,这意味着您的循环根本没有运行。
给glNumRows一个值,你应该看到一些结果。
根据您更新的问题:
变量glNumRows不在AddNumbersOnOneRow子的范围内。就像测试一样,尝试在AddNumbersOnOneRow子中将该行放在该循环之前(我认为我的语法正确,我还没有测试过):
glNumRows = UBound(gINTRules.GridData.DataArray, 2)
同样,重申...... glNumRows未在AddNUmbersOnOneRow子中定义。您可以将其添加为参数或将其设置在sub中,或者如果您真的喜欢冒险,甚至可以将其设置为全局变量。
如果您不想将我上面的代码添加为测试,您还可以在初始化循环的行上设置断点并检查glNumRows的值并报告VBA所说的值是什么...我的猜测它会告诉你它是0.