VBA:Filing Table Cells With Show Dialog Box

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

VBA:Filing Table Cells With Show Dialog Box

Post by Susanto3311 »

hi all..

i have table made in ms word (insert table). As you are working with tables in Word, you may want to fill the various cells in a table with a set value. For instance, you might want to copy something to the Clipboard, and then paste the contents of the Clipboard to each cell in a table. The following macro will do the trick (work properly): i found in this code from google

Code: Select all

Sub PasteToCellsEnd()
    Dim TargetRange As Range
    Dim oTargCell As Cell
    Dim PasteRange As Range

    If Selection.Cells.Count = 0 Then
        'Quit if no cells in selection
        MsgBox "No cells selected", vbCritical
        Exit Sub
    End If
    On Error Resume Next
    Set TargetRange = Selection.Range
    For Each oTargCell In Selection.Cells
        Set PasteRange = oTargCell.Range.Characters.Last
        PasteRange.Collapse wdCollapseStart
        PasteRange.Paste
    Next oTargCell
    TargetRange.Select
End Sub
but i want someone would modify this code with step like this:
1. At first run macro, i want to show message "select your table/cells........."
2. typing/input your text that you want to fill it into table/cell. --ok. (done)

anybody would help me out, thanks in advance

susant

User avatar
HansV
Administrator
Posts: 78359
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA:Filing Table Cells With Show Dialog Box

Post by HansV »

1. makes no sense. The user should select the cells before running the macro. It is not possible to do that while the macro is running.
2. Foe example:

Code: Select all

Sub PasteToCellsEnd()
    Dim strText As String
    Dim TargetRange As Range
    Dim oTargCell As Cell
    Dim PasteRange As Range

    If Selection.Cells.Count = 0 Then
        'Quit if no cells in selection
        MsgBox "No cells selected", vbCritical
        Exit Sub
    End If
    strText = InputBox("Enter the text")
    If strText = "" Then
        MsgBox "No text entered", vbCritical
        Exit Sub
    End If
    On Error Resume Next
    Set TargetRange = Selection.Range
    For Each oTargCell In Selection.Cells
        Set PasteRange = oTargCell.Range.Characters.Last
        PasteRange.Collapse wdCollapseStart
        PasteRange.Text = strText
    Next oTargCell
    TargetRange.Select
End Sub
Best wishes,
Hans

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA:Filing Table Cells With Show Dialog Box

Post by Susanto3311 »

hi hans,,,,WORKING GREAT!!!!!
thank you very much!!
:clapping:

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA:Filing Table Cells With Show Dialog Box

Post by Susanto3311 »

Hi hans..
In another situation, if table has content value not empty table, how to make code could replace the value that existing into cell/table?

User avatar
HansV
Administrator
Posts: 78359
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA:Filing Table Cells With Show Dialog Box

Post by HansV »

Code: Select all

Sub PasteToCells()
    Dim strText As String
    Dim TargetRange As Range
    Dim oTargCell As Cell
    Dim PasteRange As Range

    If Selection.Cells.Count = 0 Then
        'Quit if no cells in selection
        MsgBox "No cells selected", vbCritical
        Exit Sub
    End If
    strText = InputBox("Enter the text")
    If strText = "" Then
        MsgBox "No text entered", vbCritical
        Exit Sub
    End If
    On Error Resume Next
    Set TargetRange = Selection.Range
    For Each oTargCell In Selection.Cells
        Set PasteRange = oTargCell.Range
        PasteRange.Text = strText
    Next oTargCell
    TargetRange.Select
End Sub
Best wishes,
Hans

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA:Filing Table Cells With Show Dialog Box

Post by Susanto3311 »

hi hans, thanks a lot..

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA:Filing Table Cells With Show Dialog Box

Post by Susanto3311 »

hi hans...
i found new problem..
your code work if the table all selected range,,how if the cell/range is not all selected?
e.g. since i hold CTRL then select cell certainty (not all selected) using drag mouse than the running the code the result not fully work. Only last cell that change , word "test"
like my attachment file
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78359
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA:Filing Table Cells With Show Dialog Box

Post by HansV »

Word VBA does not support that. It only works with contiguous selections.
Best wishes,
Hans

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA:Filing Table Cells With Show Dialog Box

Post by Susanto3311 »

ok hans, thank you.