2014-12-02 281 views
1

我有一個500-600行的文件(具有很大的增長潛力)。它以xml文件開始,在自動讀取後,創建的表格在標題信息和其餘部分之間具有不同數量的空白單元格。每行存在相同數量的信息。我需要每行包含連續的數據。刪除特定行中的空白單元格,直到非空單元格(VBA/Excel)

enter image description here

想象的黃色方塊是細胞的信息。頂級網格是它對我的影響。中間是多麼迫切需要它。最後是一個理想,其餘的表格格式列消失了(也是我知道如何做的很容易)。

我將不勝感激任何和所有的幫助。

+0

所以,你只是想刪除所有有空白數據的單元格? – cheezsteak 2014-12-02 20:23:36

+0

Ish。要麼刪除單元格,直到有數據的單元格取代「C」的位置,或者如果C爲空,則到達該行中具有文本的下一個單元格,剪切三個單元格並將其粘貼到最初引用的C單元格中它不再是空白。 – Archaimot 2014-12-02 20:27:49

回答

0

不知道我理解真正的需求,但如果你使用類似的數據:

enter image description here

,並選擇在柱小區A你要處理並運行此行:

Sub FixLines() 
    Dim r As Range, N As Long, i As Long, RR As Long 
    For Each r In Selection 
     RR = r.Row 
     N = Cells(RR, Columns.Count).End(xlToLeft).Column 
     If N <> 1 Then 
      For i = N To 1 Step -1 
       If Cells(RR, i).Value = "" Then 
        Cells(RR, i).Delete (xlShiftToLeft) 
       End If 
      Next i 
     End If 
    Next r 
End Sub 

您將結束:

enter image description here

+0

這是很好的,除了在我的工作表中它拿出表中的每一列,其中有一個空白。所以我想要移動的數據不斷被追加 - 即使數據在正確的位置。我感謝您的幫助。 – Archaimot 2014-12-02 21:28:44

+0

編輯:它的工作! - 但不是在一張桌子。這是我可以解決的問題。謝謝。 – Archaimot 2014-12-02 21:35:19

0

以下是一種方法(您也可以使用代碼將內容移至左側)。

Option Explicit 

Function Delete_Blank_Cells() 
Dim lRow  As Long 
Dim lCol  As Long 
Dim LastRow  As Long 
Dim LastCol  As Long 

    LastRow = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row 
    LastCol = Cells.Find(What:="*", After:=Range("A1"), SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).column 

    For lRow = 1 To LastRow 
     For lCol = 1 To LastCol 
      If Cells(lRow, lCol) = "" Then 
        Cells(lRow, lCol).Delete Shift:=xlToLeft 
      End If 
     Next 
     If lRow > 5 Then 
      MsgBox "Exit code - remove after testing!!" 
      Exit Function 
     End If 
    Next 
End Function 
相關問題