プロが教える店舗&オフィスのセキュリティ対策術

添付のような問題を解いているのですが、ForNextとLoopを用いて文字列を繰り返す方法がわかりません。
どなたか教えていただけないでしょうか。

REPT関数を用いた方法ならわかったのですが。。。
Private Sub UserForm_Activate()
Label1.Caption = WorksheetFunction.Rept("あ ", 10)
End Sub

「文字列を繰り返して表示する VBA」の質問画像

A 回答 (2件)

dim strOut as string


dim i as integer
strOut = ""
for i = 1 to 10
strOut = strOut & " あ "
next i
label1.caption = strOut

とか、

dim strOut as string
dim i as integer
strOut = ""
i = 0
Do while i < 10
strOut = strOut & " あ "
i = i+1
loop
label1.caption = strOut

など、出力する文字列をループを形成して作ってくださいということかな
    • good
    • 0
この回答へのお礼

strOut = strOut & " あ "が文字を増やすコードになるのですね。
ご丁寧にありがとうございます!

お礼日時:2022/01/20 12:02

> Label1 に "あ "という文字列を 10回繰り返して追加


という要件ですから

Dim i As Integer
Me.Label1.Caption = ""
For i = 1 To 10
  Me.Label1.Caption = Me.Label1.Caption & "あ "
Next i

ということでしょう。

Do While については応用です。
ご自身で考えてみてください。

直書きにつき、間違いがあったら御免。
    • good
    • 0
この回答へのお礼

Dim i As Integer
Label1.Caption = ""
Do While i < 10
Label1.Caption = Label1.Caption & "あ "
i = i + 1
Loop

でしょうか。ありがとうございます!

お礼日時:2022/01/20 12:12

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!