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中沒有任何東西。VBA對錶列進行計算失敗
我想把列A和列B添加到列D ![這是我的表格,我想添加列A和ColumnB插入到列D中。但是當我運行宏我什麼也得不到]
我懷疑是以下行給我找麻煩。
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
我把glNumRows放在Main中,但循環仍然沒有運行 – user2634156
@ user2634156你可以編輯你的原始文章以包含你的更新代碼嗎?試圖將它作爲未格式化的評論塊來做它的正面或反面是一個熊。 – sous2817
@ user2634156我已根據您的修改更新了我的答案。 – sous2817