尝试通过对excel VBA中其他两列中的值求和来创建新列
我正在寻找一些帮助来创建一个新列,并用行中其他四列的总和填充行值。我已尝试使用以下代码,但在尝试在行上运行时,我不断收到类型不匹配问题:尝试通过对excel VBA中其他两列中的值求和来创建新列,excel,vba,Excel,Vba,我正在寻找一些帮助来创建一个新列,并用行中其他四列的总和填充行值。我已尝试使用以下代码,但在尝试在行上运行时,我不断收到类型不匹配问题: 结果(i)=SForehandRange(i,1)+SBackhandRange(i,1)+RForehandRange(i,1)+RBackhandRange(i,1) 我不确定这是否是确切的问题,如果您能提供帮助,我将不胜感激。 代码在我已经创建的表上运行,其中的值是数字。有些单元格将为空,但我尝试用值填充所有单元格,但仍然收到错误 Sub sumShot
结果(i)=SForehandRange(i,1)+SBackhandRange(i,1)+RForehandRange(i,1)+RBackhandRange(i,1)
我不确定这是否是确切的问题,如果您能提供帮助,我将不胜感激。
代码在我已经创建的表上运行,其中的值是数字。有些单元格将为空,但我尝试用值填充所有单元格,但仍然收到错误
Sub sumShotsInRally()
'Set rawData sheet as active
Dim sht1 As Worksheet
Set sht1 = Sheets("rawData")
sht1.Activate
'Find the Columns to Add
Dim serverForehandColNum As Integer
serverForehandColNum = ActiveSheet.Rows(1).Find(what:="Serving player forehand", lookat:=xlWhole).Column
Dim serverBackhandColNum As Integer
serverBackhandColNum = ActiveSheet.Rows(1).Find(what:="Serving player backhand", lookat:=xlWhole).Column
Dim returnerForehandColNum As Integer
returnerForehandColNum = ActiveSheet.Rows(1).Find(what:="Returning player forehand", lookat:=xlWhole).Column
Dim returnerBackhandColNum As Integer
returnerBackhandColNum = ActiveSheet.Rows(1).Find(what:="Returning player backhand", lookat:=xlWhole).Column
'Insert new column for the sum total
ActiveSheet.Columns(serverForehandColNum + 1).Insert
' Add New col heading
ActiveSheet.Cells(1, serverForehandColNum + 1).Value = "Rally Count"
Dim rallyCountColNum As Integer
rallyCountColNum = ActiveSheet.Rows(1).Find(what:="Rally Count", lookat:=xlWhole).Column
'Define the range to iterate over
Dim SForehandRange As Range
Dim SBackhandRange As Range
Dim RForehandRange As Range
Dim RBackhandRange As Range
Dim rallyRange As Range
With ActiveSheet
Set SForehandRange = .Range(.Cells(2, serverForehandColNum), .Cells(.UsedRange.Rows.Count, serverForehandColNum))
Set SBackhandRange = .Range(.Cells(2, serverBackhandColNum), .Cells(.UsedRange.Rows.Count, serverBackhandColNum))
Set RForehandRange = .Range(.Cells(2, returnerForehandColNum), .Cells(.UsedRange.Rows.Count, returnerForehandColNum))
Set RBackhandRange = .Range(.Cells(2, returnerBackhandColNum), .Cells(.UsedRange.Rows.Count, returnerBackhandColNum))
Set rallyRange = .Range(.Cells(2, rallyCountColNum), .Cells(.UsedRange.Rows.Count, rallyCountColNum))
End With
Dim results()
'You redimension the results array to the number of entries in your table
ReDim results(1 To SForehandRange.Rows.Count)
'You loop over your table and sum the values from count and restocked
For i = 1 To SForehandRange.Rows.Count
results(i) = SForehandRange(i, 1) + SBackhandRange(i, 1) + RForehandRange(i, 1) + RBackhandRange(i, 1)
Next i
'You write the array to the range count and delete the values in restocjed
rallyRange = Application.Transpose(results)
End Sub
如果您将表转换为实际表(
ListObject
),并使用带结构化引用的SUM
公式,我认为这会容易得多。未经测试,但我认为您需要SForehandRange.Cells(I,1)
而不是SForehandRange(I,1)
,并且对于声明为Range
的所有变量都是相同的<如果SForehandRange
被声明为数组,而不是范围,则code>SForehandRange(i,1)将成为数组。@BigBen感谢您的回复,我确实将该表作为实际表。我将如何执行您在vba中的建议?您只需将公式写入您创建的列表列
的.DataBodyRange
。@FoxfireAndBurnsAndBurns感谢您的回复,但仍然是相同的错误。