View Single Post
 
Old 06-21-2016, 09:16 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

If instead of using an ActiveX button, which, as Paul says, becomes the selection when you click it, you were to use a ribbon button to call the userform, you wouldn't have this problem, as the selection would remain in the table cell.

With an ActiveX button you would have to add the means of indicating to the macro which cell to process. This could, for example take the form of a pair of combo-boxes to indicate the row and column respectively. This works as long as there are no split or merged cells.

You can then refer to the selection in these boxes and create a range to the corresponding cell. e.g. as follows

Code:
Option Explicit
Private Sub UserForm_Initialize()
Dim oTable As Table
Dim i As Integer
    Set oTable = ActiveDocument.Tables(1)
    With Me.ComboRow
        For i = 1 To oTable.Rows.Count
            .AddItem i
        Next i
        .ListIndex = 0
    End With
    With Me.ComboColumn
        For i = 1 To oTable.Columns.Count
            .AddItem i
        Next i
        .ListIndex = 0
    End With
End Sub


Private Sub btnUpdateP_Click()
Dim oTable As Table
Dim oCell As Range
Dim PeopleChoice As String
Dim iRow As Integer, iCol As Integer
    Set oTable = ActiveDocument.Tables(1)
    iRow = Me.ComboRow.ListIndex + 1
    iCol = Me.ComboColumn.ListIndex + 1
    
    Select Case True
        Case Is = Me.optA0.Value: PeopleChoice = "A0"
        Case Is = Me.optA3.Value: PeopleChoice = "A3"
        Case Is = Me.optA5.Value: PeopleChoice = "A5"
        Case Else: PeopleChoice = "C5"
    End Select
    Me.Hide
    Set oCell = oTable.Rows(iRow).Cells(iCol).Range
    oCell.End = oCell.End - 1
    oCell.Text = PeopleChoice
    Unload Me
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote