我需要从F4向下删除F列中的空白单元格。每行将有0到很多空白单元格。
我对编码非常陌生,而我最成功的方法就是采用做类似的事情并将其吞噬的代码。
以下是适用于类似情况的代码:
Sub test()
Dim i As Long
With Worksheets("Sheet1")
For i = 4 To .Cells(.Rows.Count, "F").End(xlUp).Row
Do While .Cells(i, "F").Value2 = .Cells(1, "B").Value2 And Not
IsEmpty(.Cells(i, "F"))
.Cells(i, "F").Resize(1, 4).Delete shift:=xlToLeft
Loop
Next i
End With
End Sub
这是我尝试使用的方法:
Sub test()
Dim i As Long
With Worksheets("sheet1")
For i = 4 To .Cells(.Rows.Count, "F").End(xlUp).Row
Do While .Cells(i, "F").Value2 = ""
.Cells(i, "F").Resize(1, 4).Delete shift:=xlToLeft
Loop
Next i
End With
End Sub
运行它时没有出现任何错误,但是它也不起作用。
任何指导表示赞赏!
答案 0 :(得分:1)
要删除空白单元格,只需将其向右移动即可。如果这些单元格也为空白,则什么都不会改变。如果要删除整个行,请将代码更改为此:
Sub test()
Dim i As Long
With Worksheets("sheet1")
For i = .Cells(.Rows.Count, "F").End(xlUp).Row to 4 Step-1
IF .Cells(i, "F").Value2 = ""
.Cells(i, "F").entirerow.Delete shift:=xlUp
Loop
Next i
End With
End Sub
答案 1 :(得分:0)
尝试一下:
Sub test()
Dim i As Long
Dim lastrow as integer
With Worksheets("sheet1")
lastrow= cells(rows.count, 6).end(xlup).row 'define your last filled row
For i = 4 To lastrow
if IsEmpty(cells(i,6))= TRUE then
Cells(i,6).Delete shift:=xlToLeft
Next i
End With
End Sub
但是,我通常发现从下至上删除单元格效果更好。所以....也许您也可以尝试以下方法:
Sub test()
Dim i As Long
Dim lastrow as integer
With Worksheets("sheet1")
lastrow= cells(rows.count, 6).end(xlup).row 'define your last filled row
For i = lastrow To 4 step -1
if IsEmpty(cells(i,6))= TRUE then
Cells(i,6).Delete shift:=xlToLeft
Next i
End With
End Sub
答案 2 :(得分:0)
假设 1.该代码的目的是仅消除F列中的空格 2.不需要删除整行。 3.删除单元格xlLeft会无意中将G列的内容(或空白单元格)放入F列。 可以尝试以下代码
Sub test()
Dim i As Long
With Worksheets("sheet1")
For i = .Cells(.Rows.Count, "F").End(xlUp).Row To 4 Step -1
If .Cells(i, "F").Value2 = "" Then
.Range(.Cells(i + 1, "F"), .Cells(.Cells(.Rows.Count, "F").End(xlUp).Row, "F")).Cut
.Cells(i, "F").Select
.Paste
End If
Next i
End With
End Sub