2016-05-13 99 views
0

是否有一種使用Excel VBA的方法,以便任何時候Excel電子表格中位於具有某些內容的行之間的特定行在其所有單元格中清空,較低的行向上複製。即假設第1-10行有內容,我刪除第5行的內容,然後第6行變成第5行,第7行變成第6行等到第10行變成第9行並且操作完成。任何建議將不勝感激。自動刪除行,並用Excel中的較低行替換Vba

回答

0

所以,你已經做了編輯,並嘗試它爲您的需要。您應該注意到底部的循環向上移動,Excel電子表格頂部有五條固定線。

Sub Delete_Empty_Lines() 
    Dim quotes As Integer 

    Application.ScreenUpdating = False 
    Dim usedRows As Long 
    Dim row As Long 

    '// usedRows = Sheets("Sheet-1").UsedRange.Rows.Count 
    usedRows = Sheets("Sheet-1").Cells(Rows.Count, 1).End(xlUp).row 

    For row = usedRows To 5 Step -1 
    If Sheets("Sheet-1").Cells(row, 1) = "" Then 
     quotes = MsgBox("Sheet-1 has empty lines - delete this line?", vbYesNo) 
     Select Case quotes 
     Case vbYes 
      Rows(row).EntireRow.Delete 
      '// Exit Sub 
     Case vbNo 
      Exit Sub 
     End Select 
    End If 
    Next 

    Application.ScreenUpdating = True 

End Sub 

編輯:代碼示例插入線:

Private Sub cmdFillData_Click() 
    '//--- clean and fill in some data ---------------------------------- 
    Sheets("AreaData").Select 
    Worksheets("AreaData").Cells.Clear 
    Range("A1").Select 
    Range("A1:C1") = Array("Germany", "Berlin", "Brandenburg Gate") 
    Range("A2:C2") = Array("South Africa", "Cape Town", "Waterfront") 
    Range("A3:C3") = Array("France", "Paris", "Eiffel Tower") 
    Range("A4:C4") = Array("Spain", "Madrid", "Plaza Mayor") 
    Range("A5:C5") = Array("Italy", "Rome", "Palazzo Senatorio") 

    '//--- search for value ------------------------------------- 
    Columns("A:A").Select 
    Cells.Find(What:="France", After:=ActiveCell, LookIn:=xlFormulas, _ 
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ 
    MatchCase:=False, SearchFormat:=False).Activate 

    '//--- Insert row above active cell 
    ActiveCell.EntireRow.Insert 
    ActiveCell.Select 

    '//--- Insert row below active cell 
    'ActiveCell.Offset(1).EntireRow.Insert 

    '//--- Insert column to the left of the active cell 
    'ActiveCell.EntireColumn.Insert 

    '//--- Insert column to the right of the active cell 
    'ActiveCell.EntireColumn.Offset(0, 1).Insert 

End Sub 
+0

@ help-info.de所以基本上行(行).EntireRow.Delete做這項工作?什麼確保將較高索引行復制到較低索引行? – IntegrateThis

+0

這是舊東西,是行(行).EntireRow.Delete 完成這項工作。複印作業與手動完成相同。 –

+0

在兩行之間添加行是否有類似的功能?非常感謝。 – IntegrateThis