我已经编写了将工作表上所有非空白单元格移动到其行中第一个可用空白单元格的代码。
我不想让代码针对整张表运行,我希望它忽略A和B列以及第1、2和3行。
为此,我将.Range(.Cells(1, 1)
更改为.Range(.Cells(4, 3)
,这在输入测试数据的最新excel表格上效果很好。该测试数据只是输入到各个单元中的随机分散的数据。
当我在实际工作表Set delrng = .Rows(rw).Cells.SpecialCells(xlCellTypeBlanks)
上运行代码时,出现错误Run-time error '1004': No cells were found.
并且无法执行任何操作。
Google搜集了一些有关错误处理No Cells were found
的主题,但我认为仅消除错误对我没有帮助。
谢谢。
Option Explicit
Sub Shift_Left()
Dim rw As Long, lr As Long, lc As Long, delrng As Range
With Worksheets("Application Maturity Tracker")
lr = .Cells.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
MatchCase:=False, SearchFormat:=False).Row
lc = .Cells.Find(what:="*", after:=.Cells(1, 1), LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious, _
MatchCase:=False, SearchFormat:=False).Column
With .Range(.Cells(1, 1), .Cells(lr, lc))
For rw = 1 To .Rows.Count
Set delrng = .Rows(rw).Cells.SpecialCells(xlCellTypeBlanks)
If Not delrng Is Nothing Then
delrng.Delete Shift:=xlToLeft
End If
Next rw
End With
End With
End Sub
答案 0 :(得分:1)
您需要跳过该错误(通常不建议这样做,但在这里不可避免)。请记住,一旦执行了该行,就重新设置错误检查。
With .Range(.Cells(1, 1), .Cells(lr, lc))
For rw = 1 To .Rows.Count
On Error Resume Next
Set delrng = .Rows(rw).SpecialCells(xlCellTypeBlanks).Cells
On Error GoTo 0
If Not delrng Is Nothing Then
delrng.Delete Shift:=xlToLeft
End If
Next rw
End With