回答編集履歴

2

コード微修正

2020/09/01 06:33

投稿

hatena19
hatena19

スコア33763

test CHANGED
@@ -32,6 +32,8 @@
32
32
 
33
33
  Private Sub myCmdBtn_Click()
34
34
 
35
+ If myLabel.Caption = "" Then Exit Sub
36
+
35
37
 
36
38
 
37
39
  With New MSForms.DataObject
@@ -42,11 +44,9 @@
42
44
 
43
45
  End With
44
46
 
45
-
47
+
46
48
 
47
49
  MsgBox "URLをクリップボードにコピーしました。"
48
-
49
-
50
50
 
51
51
  End Sub
52
52
 
@@ -70,19 +70,17 @@
70
70
 
71
71
  Option Explicit
72
72
 
73
+
74
+
73
75
  Dim myCol As Collection
74
76
 
75
77
  Dim myBtnLbl As CmdBtnWithLabel
76
78
 
77
79
 
78
80
 
79
-
80
-
81
81
  Sub UserForm_Initialize()
82
82
 
83
83
  Set myCol = New Collection
84
-
85
-
86
84
 
87
85
 
88
86
 

1

サンプルコード追記

2020/09/01 06:33

投稿

hatena19
hatena19

スコア33763

test CHANGED
@@ -6,4 +6,166 @@
6
6
 
7
7
 
8
8
 
9
- [VBA クラスモジュール イベント共通 \- Google 検索](https://www.google.com/search?q=VBA+%E3%82%AF%E3%83%A9%E3%82%B9%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB+%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E5%85%B1%E9%80%9A&oq=VBA+%E3%82%AF%E3%83%A9%E3%82%B9%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB+%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E5%85%B1%E9%80%9A)
9
+ [VBA クラスモジュール イベント共通 - Google 検索](https://www.google.com/search?q=VBA+%E3%82%AF%E3%83%A9%E3%82%B9%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB+%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E5%85%B1%E9%80%9A&oq=VBA+%E3%82%AF%E3%83%A9%E3%82%B9%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB+%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88%E5%85%B1%E9%80%9A)
10
+
11
+
12
+
13
+ ---
14
+
15
+ この質問の場合、コマンドボタンとラベルがワンセットなので、クラスモジュールもその2つをセットにしたものにするといいでしょう。
16
+
17
+
18
+
19
+ **クラスモジュール CmdBtnWithLabel**
20
+
21
+ ```vba
22
+
23
+ Option Explicit
24
+
25
+
26
+
27
+ Private WithEvents myCmdBtn As MSForms.CommandButton
28
+
29
+ Private myLabel As MSForms.Label
30
+
31
+
32
+
33
+ Private Sub myCmdBtn_Click()
34
+
35
+
36
+
37
+ With New MSForms.DataObject
38
+
39
+ .SetText myLabel.Caption 'ラベルのCaptionをDataObjectに格納する
40
+
41
+ .PutInClipboard 'DataObjectのデータをクリップボードに格納する
42
+
43
+ End With
44
+
45
+
46
+
47
+ MsgBox "URLをクリップボードにコピーしました。"
48
+
49
+
50
+
51
+ End Sub
52
+
53
+
54
+
55
+ Public Sub setBtnLbl(Cmdbtn As MSForms.CommandButton, lbl As MSForms.Label)
56
+
57
+ Set myCmdBtn = Cmdbtn
58
+
59
+ Set myLabel = lbl
60
+
61
+ End Sub
62
+
63
+ ```
64
+
65
+
66
+
67
+ **UserForm2 モジュール**
68
+
69
+ ```vba
70
+
71
+ Option Explicit
72
+
73
+ Dim myCol As Collection
74
+
75
+ Dim myBtnLbl As CmdBtnWithLabel
76
+
77
+
78
+
79
+
80
+
81
+ Sub UserForm_Initialize()
82
+
83
+ Set myCol = New Collection
84
+
85
+
86
+
87
+
88
+
89
+ Dim row As Long
90
+
91
+ row = ActiveCell.row '行番号取得
92
+
93
+
94
+
95
+ With Me
96
+
97
+ Dim n As Long
98
+
99
+ For n = 1 To 5
100
+
101
+ Dim lbl As MSForms.Label
102
+
103
+ Set lbl = .Controls.Add("Forms.Label.1", "url" & n, True)
104
+
105
+ With lbl
106
+
107
+ .Top = 34 * n 'Top位置(表示位置を移動する)
108
+
109
+ .Left = 70 'Left位置
110
+
111
+ .Height = 20 '高さ
112
+
113
+ .Width = 250 '幅
114
+
115
+ .BorderStyle = fmBorderStyleSingle '枠線
116
+
117
+ .BackColor = RGB(128, 128, 128) '背景色
118
+
119
+ .ForeColor = RGB(255, 255, 255) '文字色
120
+
121
+ .Font.Name = "メイリオ" 'テキストのスタイル
122
+
123
+ .TextAlign = 2 'テキストの位置
124
+
125
+ .FontSize = 16 'テキストのサイズ
126
+
127
+ .Caption = Cells(row, 5) '''ここにパスワードの題名を付ける
128
+
129
+ End With
130
+
131
+
132
+
133
+ Dim btn As MSForms.CommandButton
134
+
135
+ Set btn = .Controls.Add("Forms.CommandButton.1", "url" & n, True)
136
+
137
+ With btn
138
+
139
+ .Top = 34 * n
140
+
141
+ .Left = 10
142
+
143
+ .Height = 20
144
+
145
+ .Width = 50
146
+
147
+ .Caption = "copy"
148
+
149
+ End With
150
+
151
+
152
+
153
+ Set myBtnLbl = New CmdBtnWithLabel
154
+
155
+ myBtnLbl.setBtnLbl btn, lbl
156
+
157
+ myCol.Add myBtnLbl
158
+
159
+
160
+
161
+ row = row + 1
162
+
163
+ Next
164
+
165
+
166
+
167
+ End With
168
+
169
+ End Sub
170
+
171
+ ```