|
A:B列にボタンを配置してマクロ実行ということですので、
前掲マクロを少し変えてみました。
御参考までに。
Sub MyShapeCmmdBttn()
Dim myTitle As String
Dim i As Long
'
Dim myCcAddr As String
Dim x As Long
Dim y As Long
Dim myLeft As Long
Dim myHeight As Long
'
Dim myShape As Shape
Dim myOnAction As String
'
myTitle = "MyShapeCmmdBttn"
'
For i = 2 To 50
Cells(i, "A").Select
myCcAddr = ActiveSheet.Columns(ActiveCell.Column).Address(False, False)
myCcAddr = Left(myCcAddr, InStr(myCcAddr, ":") - 1)
x = ActiveSheet.Columns("A:B").Width ' ActiveCell.Width
y = ActiveSheet.Rows("1:" & ActiveCell.Row).Height
y = y - ActiveCell.Height
'
myLeft = 0
myHeight = ActiveSheet.Rows(ActiveCell.Row).Height
'
Set myShape = ActiveSheet.Shapes.AddFormControl(xlButtonControl, myLeft, y, x, myHeight)
'
With myShape
.Name = ActiveCell.Address(False, False)
.TextFrame.Characters.Text = Cells(ActiveCell.Row, "C").Value
.AlternativeText = .Name
myOnAction = myTitle & "MyRun" & " "
myOnAction = myOnAction & Chr(&H22) & .Name & ChrW(&H22)
.OnAction = "'" & myOnAction & "'"
End With
Next ' i
End Sub ' MyShapeCmmdBttn ' *----*----* *----*----* *----*----* *----*----*
Sub MyShapeCmmdBttnMyRun(myAddress As String)
Rem *----*----* *----*----* *----*----* *----*----*
Rem コマンドボタンOnAction処理
Rem *----*----* *----*----* *----*----* *----*----*
Dim myWord As Variant ' Word.Application
Dim myWordDoc As Variant ' Word.Document
Dim myText As Variant
'
On Error Resume Next
Set myWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
Set myWord = CreateObject("Word.Application")
Set myWordDoc = myWord.Documents.Add
myWord.Visible = True
End If
On Error GoTo 0
'
Range(myAddress).Offset(0, 2).Select
myText = ActiveCell.Value & " : " & ActiveCell.Offset(0, 1).Value
myWord.Selection.TypeText myText & vbCrLf
'
myWord.WindowState = 2 ' wdWindowStateMinimize
End Sub ' MyShapeCmmdBttnMyRun ' *----*----* *----*----* *----*----* *----*----*
|
|