Vba 运行时错误1004-Excel宏-早期代码运行正常
我有下面的代码在表中最后一行有值之后插入一个新行Vba 运行时错误1004-Excel宏-早期代码运行正常,vba,excel,Vba,Excel,我有下面的代码在表中最后一行有值之后插入一个新行 Private Sub CommandButton1_Click() Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer, additionalRow As Integer, additionalRowCounter As Integer, offsetRowCounter As Integer Dim currentRowValue As St
Private Sub CommandButton1_Click()
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer, additionalRow As Integer, additionalRowCounter As Integer, offsetRowCounter As Integer
Dim currentRowValue As String
'Dim Step5 As Range
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("2.Set Up")
ws.Activate
sourceCol = 3 'Entity Name is column C, which has a value of 6
additionalRow = 0
offsetRowCounter = 0
rowCount = ws.Cells(Rows.Count, sourceCol).End(xlUp).Row
'for every row starting from 4th row, find the first blank cell and select it
For currentRow = 4 To rowCount
currentRowValue = ws.Range(currentRow, sourceCol).Value
If IsEmpty(currentRowValue) Or currentRowValue = "" Then
Cells(currentRow, sourceCol).Select
Rows(currentRow).Insert shift:=xlShiftDown
Exit For
End If
additionalRow = additionalRow + 1
Next
End Sub
表格从C4开始
早些时候还不错,但从昨天开始,我开始
编译代码时出现运行时错误1004
我没有在床单上做任何更改
请说明可能存在的问题您正在使用索引号引用单元格,但同时希望使用范围表示法获取值。因此,改变这一行:
currentRowValue = ws.Range(currentRow, sourceCol).Value
致:
想知道吗?在哪一行中出现错误,错误消息是什么?请注意,对于处理行计数的所有变量,必须使用Long,因为Excel的行数超过Integer可以处理的行数。建议这样做,因为在VBA中使用Integer没有任何好处。另外,如果您的目标是在上次使用的行之后添加一行,那么为什么要循环所有行?+尝试避免使用。选择:错误在以下代码中:currentRowValue=ws.RangecurrentRow,sourceCol.Value。我还将整数改为Long,但仍然得到相同的错误发生错误时currentRow、sourceCol的值是多少?此外,如果要使用ws,请将操作包装在一个中,否则它将使用Activesheet for CellscurrentRow、sourceCol。选择RowscurrentRow。插入shift:=xlShiftDown
currentRowValue = ws.cells(currentRow, sourceCol).Value