Nhờ các bác bổ sung code xóa nội dung trong ô của Word

Liên hệ QC

trungtamcnc

Thành viên hoạt động
Tham gia
5/4/10
Bài viết
124
Được thích
9
Em viết 1 đoạn code để đánh số thứ tự trong bảng, em để con trỏ ở dòng thứ 2, chạy code nó sẽ đánh đến ô cuối. Em nhờ các bác viết thêm cho em câu xóa nội dung đã có trong ô, để đánh STT mới. Mà bác nào bonus thêm cho em học tập thì càng tốt ạ! Em cảm ơn!
Mã:
Sub DanhSTT()
    Dim i As Integer
    dk = ActiveDocument.Tables(1).Rows.Count
    For i = 1 To dk - 1
    Selection.TypeText Text:=i
    Selection.MoveDown Unit:=wdLine, Count:=1
    Next i
End Sub
 
Em viết 1 đoạn code để đánh số thứ tự trong bảng, em để con trỏ ở dòng thứ 2, chạy code nó sẽ đánh đến ô cuối. Em nhờ các bác viết thêm cho em câu xóa nội dung đã có trong ô, để đánh STT mới. Mà bác nào bonus thêm cho em học tập thì càng tốt ạ! Em cảm ơn!
Mã:
Sub DanhSTT()
    Dim i As Integer
    dk = ActiveDocument.Tables(1).Rows.Count
    For i = 1 To dk - 1
    Selection.TypeText Text:=i
    Selection.MoveDown Unit:=wdLine, Count:=1
    Next i
End Sub
Thử code này xem sao.
Mã:
Sub DanhSTT()
    Dim i As Integer, dk As Integer
    Dim iSelectionRowStart As Long
    Dim iSelectionColumnStart As Long
    dk = ActiveDocument.Tables(1).Rows.Count
    iSelectionRowStart = Selection.Information(wdEndOfRangeRowNumber)
    iSelectionColumnStart = Selection.Information(wdEndOfRangeColumnNumber)
    For i = 1 To dk - 1
        Selection.Tables(1).Cell(i + iSelectionRowStart - 1, iSelectionColumnStart).Range.Text = i
    Next i
End Sub
Góp ý chút, theo tôi thì không nên đặt con trỏ tại vị trí cần đánh số thứ tự mà nên quét chọn những ô cần đánh số thì sẽ linh hoạt hơn. Ví dụ trong bảng tui cần đánh một số ô phía trên một vài ô phía dưới tui không muốn đánh, nếu muốn kiểu này bạn tham khảo code dưới đây.
Mã:
Sub DanhSTT()
    Dim i As Integer, lngStart As Integer, lngEnd As Integer
    Dim iSelectionRowStart As Integer, iSelectionRowEnd As Integer
    Dim iSelectionColumnStart As Integer, iSelectionColumnEnd As Integer
    If Selection.Information(wdWithInTable) = False Then
        MsgBox "Ban chua chon ô can danh so thu tu"
    Else
        lngStart = Selection.Range.Start
        lngEnd = Selection.Range.End
        
        iSelectionRowEnd = Selection.Information(wdEndOfRangeRowNumber)
        iSelectionColumnEnd = Selection.Information(wdEndOfRangeColumnNumber)

        Selection.Collapse Direction:=wdCollapseStart
        iSelectionRowStart = Selection.Information(wdEndOfRangeRowNumber)
        iSelectionColumnStart = Selection.Information(wdEndOfRangeColumnNumber)
        Selection.MoveEnd Unit:=wdCharacter, Count:=lngEnd - lngStart
         
        If iSelectionColumnStart < iSelectionColumnEnd Then
            MsgBox "Chuc nang nay chi ap dung cho 1 cot du lieu, ban da chon nhieu hon 1 cot"
        Else
            For i = iSelectionRowStart To iSelectionRowEnd
                Selection.Tables(1).Cell(i, iSelectionColumnStart).Range.Text = i - iSelectionRowStart + 1
            Next i
        End If
    End If
End Sub
 
Em cảm ơn bác giaiphap rất nhiều ạ! Sử lý được trong vùng chọn thì đúng là tuyệt vời.
 
Web KT
Back
Top Bottom