Excel 在第一列中插入新行iwth值错误
昨天我添加了一个帖子。我似乎无法更新,因此在此新帖子中粘贴最新错误: 目的就是这样。在活动单元格中,当我按下一个按钮时,我将记录最大值,并从表中按1递增(因此,如果表中显示8,我将插入9)。然后,我将向表中添加一个新行,并在该行的相应列(ShotNo)中插入值(9)。 With语句中的这一行出现错误。对象范围的方法范围失败Excel 在第一列中插入新行iwth值错误,excel,vba,row,Excel,Vba,Row,昨天我添加了一个帖子。我似乎无法更新,因此在此新帖子中粘贴最新错误: 目的就是这样。在活动单元格中,当我按下一个按钮时,我将记录最大值,并从表中按1递增(因此,如果表中显示8,我将插入9)。然后,我将向表中添加一个新行,并在该行的相应列(ShotNo)中插入值(9)。 With语句中的这一行出现错误。对象范围的方法范围失败 .Range(1) = WorksheetFunction.Max(table.ListColumns("ShotNo").Range) + 1 子记
.Range(1) = WorksheetFunction.Max(table.ListColumns("ShotNo").Range) + 1
子记录快照()
将表设置为列表对象
作为整数的Dim col
将最后一行变暗为范围
将值设置为整数
Set table=工作表(“gamesheets”).ListObjects(“SOGP1”)
'首先将下一个快照添加到活动单元格
ActiveCell.value=WorksheetFunction.Max(table.ListColumns(“ShotNo”).Range)+1
'在SOGP1表中添加相同的值。
'这是编辑SOGP1表的第一步。检查最后一行是否为空;如果没有,请添加一行
如果table.ListRows.Count>0,则
Set lastRow=table.ListRows(table.ListRows.Count).Range
如果Application.CountBlank(lastRow)
我正在尝试在线获取提示以创建此代码。。。我不太擅长VBA,但我承担了这项工作。非常感谢您的帮助问题在于
lastRow
是一个范围对象.Range
是范围对象的属性而不是方法。将.Range(1)
更改为.Value
.Range(1)
不是有效的范围,我不确定您试图用它指的是什么。行中的第一列?.range
是一个范围对象的属性。我在查看对象资源管理器时错过了它。谢谢@Warcupine.真是太棒了!非常感谢-只不过它会在每一列中插入值。我只希望它在第一列中插入值。你能推荐解决方案吗?更改为value将值添加到最后一行的每一列中,我只需要插入到最后一行的第一列中。thanks@kmo.columns(1).value
或单元格(1,1).value
或范围(“A1”).value
虽然这不是一个好方法,因为它不是指工作表上显示的A1。
Sub RecordShot()
Dim table As ListObject
Dim col As Integer
Dim lastRow As Range
Dim value As Integer
Set table = Worksheets("GameSheetH").ListObjects("SOGP1")
'First add the next shot to the Active Cell
ActiveCell.value = WorksheetFunction.Max(table.ListColumns("ShotNo").Range) + 1
'Add the same value in the SOGP1 table .
'Here is the first step in editing the SOGP1 table. Check if the last row is empty; if not, add a row
If table.ListRows.Count > 0 Then
Set lastRow = table.ListRows(table.ListRows.Count).Range
If Application.CountBlank(lastRow) < lastRow.Columns.Count Then
table.ListRows.Add
End If
End If
'Iterate through the last row and populate it with the next value in the row ShotNo
If table.ListRows.Count = 0 Then 'If table is totally empty, set lastRow as first entry
table.ListRows.Add Position:=1
Set lastRow = table.ListRows(1).Range
Else
Set lastRow = table.ListRows(table.ListRows.Count).Range
End If
With lastRow
.Range(1) = WorksheetFunction.Max(table.ListColumns("ShotNo").Range) + 1
End With
End Sub