Logo Design by FlamingText.com
Logo Design by FlamingText.com

Jumat, 06 Oktober 2017

Cara Menghapus Sel Kosong Dalam Tabel Microsoft Word


Tutorial: Word 2007, 2010, 2013.
Tutorial ini menyajikan cara menggunakan macro VB untuk menghapus sel kosong dalam tabel Microsoft Word yang berjumlah banyak dan letaknya tidak berurutan.
Cara Menghapus Sel Kosong Dalam Tabel Microsoft Word

Rekomendasi: Cara Menggunakan Table di Word
Kita bisa menghapus sel kosong dengan fungsi DELETE dalam fitur Table Tools | Layout. Namun bila dokumen memiliki tabel yang panjang dan letak sel kosong tersebar di sana-sini, maka kita perlu menggunakan macro untuk menghapusnya.

Hal yang perlu diketahui:
  • Macro ini akan menghapus sel kosong dalam tabel dengan cara memindahkan isi sel yang ada di bawahnya. Bila setelah dipindahkan ada baris yang selnya kosong semua, maka baris tersebut akan dihapus.
  • Macro bisa digunakan untuk menghapus sel kosong pada seluruh tabel atau pada tabel yang dipilih saja.
  • Macro tidak berlaku untuk tabel  yang memiliki sel yang digabung (merged cell).

Langkah-Langkah

  1. Back up terlebih dahulu dokumen Word.
  2. Pilih tabel yang ingin dihapus sel kosongnya. Bila tidak ada tabel yang dipilih, maka macro ini akan menghapus sel kosong di seluruh tabel dalam dokumen.
    Gambar berikut adalah contoh tabel dengan sel yang kosong.
    Tampilan tabel dengan sel kosong
  3. Tekan tombol ALT + F11 untuk menampilkan layar editor Microsoft Visual Basic for Applications.
  4. Kemudian pada tab Insert, pilih Module. Akan muncul satu layar putih.
  5. Salin dan tempatkan kode di bawah ini pada layar putih tersebut. Option Explicit
    Sub MoveCells()
    Dim objTable As Table
    Dim intTblCount As Integer
    Dim n As Integer
    ‘turn off screen updating to increase speed
    Application.ScreenUpdating = False
    With ActiveDocument
    ‘if selection is in a table act only on this table,
    If Selection.Information(wdWithInTable) = True Then
    ‘single table option
    Set objTable = Selection.Tables(1)
    Call TableCellDelete(objTable)
    Else
    ‘else act on all tables
    intTblCount = .Tables.Count
    For n = 1 To intTblCount
    Set objTable = .Tables(n)
    Call TableCellDelete(objTable)
    Next n
    End If
    Set objTable = Nothing
    End With
    ‘restore screen updating
    Application.ScreenUpdating = True
    End Sub
    Sub TableCellDelete(OneTable As Table)
    ‘subroutine to handle cell deletes in one table
    Dim intCol As Integer
    Dim intRow As Integer
    Dim intEqual As Integer
    Dim blnMerged As Boolean
    Dim rngCell As Cell
    Dim intCols As Integer
    Dim blnCopyClear As Boolean
    Dim blnRowEmpty As Boolean
    Dim m As Integer
    Dim n As Integer
    Dim o As Integer
    ‘test that cell has no merged rows or columns
    ‘set merged cell flag to false
    blnMerged = False
    intEqual = OneTable.Rows(1).Cells.Count
    For n = 2 To OneTable.Rows.Count
    If OneTable.Rows(n).Cells.Count <> intEqual Then blnMerged = True
    Next n
    If blnMerged = True Then GoTo Merged
    intEqual = OneTable.Columns(1).Cells.Count
    For n = 2 To OneTable.Columns.Count
    If OneTable.Columns(n).Cells.Count <> intEqual Then blnMerged = True
    Next n
    If blnMerged = True Then GoTo Merged
    ‘look for empty cells in each row
    ‘table has no merged cells – so get column count
    intCols = OneTable.Columns.Count
    ‘loop through all rows except the last one
    For m = 1 To OneTable.Rows.Count – 1
    For n = 1 To intCols
    If CellData(OneTable.Rows(m).Cells(n)) = "" Then
    ‘copy text from same column in next row or below
    blnCopyClear = False
    For o = m + 1 To OneTable.Rows.Count
    If CellData(OneTable.Rows(o).Cells(n)) <> "" Then
    OneTable.Rows(m).Cells(n).Range.Text = _
    CellData(OneTable.Rows(o).Cells(n))
    ‘clear copied cell’s contents
    OneTable.Rows(o).Cells(n).Range.Text = ""
    ‘flag that we have done a copy & clear
    blnCopyClear = True
    End If
    ‘don’t look in any more rows if we have copied data
    If blnCopyClear = True Then Exit For
    Next o
    End If
    Next n
    Next m
    ‘remove any fully blank rows – start at last row
    For m = OneTable.Rows.Count To 2 Step -1
    blnRowEmpty = True
    For n = 1 To intCols
    If CellData(OneTable.Rows(m).Cells(n)) <> "" _
    Then blnRowEmpty = False
    Next n
    ‘delete empty row
    If blnRowEmpty = True Then
    OneTable.Rows(m).Delete
    End If
    Next m
    Exit Sub
    Merged:
    MsgBox "This routine does not work on tables with merged cells"
    End Sub
    Private Function CellData(contents As Cell)
    ‘removes end of cell marker from text in cell
    ‘and returns only the text
    CellData = Left(contents.Range.Text, Len(contents.Range.Text) – 2)
    End Function
  6. Tekan tombol F5 untuk menjalankan macro dan tekan tombol ALT + F11 untuk kembali ke layar dokumen Word. Hasilnya adalah seperti gambar berikut.
    Tampilan tabel setelah sel kosong dihapus

0 komentar:

Posting Komentar