- ベストアンサー
VBAで縦一列に数字を入力したい
Private Sub CommandButton1_Click() Dim n As Integer Dim i As Integer Dim x As Integer For i = 1 To n x = i + 3 n = InputBox("層数の値を入力してください") Cells(3, 1).Value = ("基板") Cells(x, 1).Value = i x = x + 1 Next End Sub 上記のようなマクロを組んだのですが、4行目に1が入ってとまってしまいました。 これを完成させるにはどうしたらいいでしょうか?
- dadao
- お礼率79% (85/107)
- オフィス系ソフト
- 回答数3
- ありがとう数4
- みんなの回答 (3)
- 専門家の回答
質問者が選んだベストアンサー
Private Sub CommandButton1_Click() Dim n As Integer Dim i As Integer Dim x As Integer n = InputBox("層数の値を入力してください") 'For文の前に nの値が必要 If n = 0 Then Exit sub 'nに0が入ったときに強制終了 For i = 1 To n x = i + 3 Cells(3, 1).Value = ("基板") Cells(x, 1).Value = i x = x + 1 Next End Sub
その他の回答 (2)
- hana-hana3
- ベストアンサー率31% (4940/15541)
>For i = 1 To n この時点でnの値は0なので、ループ1回で終了です。 >n = InputBox("層数の値を入力してください") For文の中ではなくて、外(前:上の行)に置いて下さい。
お礼
なるほどわかりました。
- bilingual
- ベストアンサー率31% (10/32)
こんな感じに変更したらどうでしょう。 Private Sub CommandButton1_Click() Dim n As Integer Dim i As Integer n = InputBox("層数の値を入力してください") For i = 1 To n Cells(3, 1).Value = ("基板") Cells(3 + i, 1).Value = i Next End Sub
お礼
できました。ありがとうございました。
関連するQ&A
- 単純な掛け算なのにわけのわからない小数点が、、、
表題のとおりですが、EXCEL VBAでシートと複合させて計算したところ次のような結果が出ました。 マクロは以下のとおりです。 Private Sub CommandButton1_Click() Dim i As Integer Dim n As Integer Dim x As Integer Dim y As Single Dim d1 As Single Dim d2 As Single n = Range("L4").Value 'L4には現在399が入力されています。 y = Range("L2").Value 'L2には現在0.048が入力されています。 d1 = Range("D11").Value d2 = Range("E11").Value If n = 0 Then Exit Sub For i = 0 To n x = i + 6 Cells(x, 13) = i Cells(x, 14) = Cells(x, 13) * y Cells(x, 15) = Cells(x, 14) + d1 Cells(x, 16) = d2 - Cells(x, 14) x = x + 1 Next i End Sub こうするとCells(7, 14)に0.0480000004172325という数字が入り始め、 続きも同じように小数点の小さい桁にわけの分からない数字が出て来ます。いったい何が原因か分かりません。 よろしくお願いします。
- ベストアンサー
- オフィス系ソフト
- 値渡しについて
UserForm1で以下のように求めた値「R」を UserForm2に渡すにはどのようにすればよいのでしょうか。 **************************************** <UserForm1(コード)> Public R As Integer ----------------------------------- Private Sub CommandButton1_Click() Dim N As Integer : N = TextBox1.Text R= N * 2 + 3 : End Sub **************************************** <UserForm2(コード)> Private Sub CommandButton1_Click() Dim i As Integer : For i = 4 To R : Next i : End Sub **************************************** どなたか教えて下さい。
- ベストアンサー
- Visual Basic
- VBAを使った九九、及び合計、平均の表の作り方
Visual Basic初心者です。 Excel 2003を使って、九九表を表示させるプロジェクトを作りたいのですが、どうしても途中で詰まってしまい、九九だけどころか、合計と平均すら表示させることができない状態です。 私の途中まで組んだタグは以下の通りです。どなたかよろしくお願いします。 Private Sub CommandButton1_Click() Call Kuku End Sub Option Explicit Dim Sum(20) As Double Sub Kuku() Dim i As Double, j As Double, num As Integer num = InputBox("numの値を入力しなさい") Sheets("Date").Cells.Clear For i = 1 To num Sheets("Date").Cells(1, i + 1) = i Sheets("Date").Cells(1 + i, 1) = i Sum(i) = 0# For j = 1 To num Sheets("Date").Cells(i + 1, j + 1).Value = Sum(i) Sum(i) = i * j Next j Next i Call Heikin Call Hyoudai MsgBox ("処理が終了しました") Sheets("Date").Select End Sub Sub Heikin() Dim i As Double, num As Integer, n As Integer For i = 9 To num Sheets("Date").Cells(i + 1, n + 2).Value = Sum(i) Sheets("Date").Cells(i + 2, n + 1).Value = Sum(i) Next i End Sub Sub Hyoudai() Sheets("Date").Cells(1, 1) = "九九" Sheets("Date").Cells(1, 11) = "合計" Sheets("Date").Cells(1, 12) = "平均" End Sub
- ベストアンサー
- Visual Basic
- VBA Next For でのコピペについて
EXCEL VBA初心者です。 AシートEW44からGD44までをコピーしてBというシートの最終行へコピーしたいです。 今下記のように組んでいるのですが、うまく作動しません。 Private Sub CommandButton1_Click() Dim i As Integer For i = 153 To 186 row1 = Worksheets("B").Cells(Rows.Count, 27).End(xlUp).Row Worksheets("A").Cells(i, 44).Value = Worksheets("B").Cells(row1 + 1, 27).Value Next i End Sub アドバイスいただけませんでしょうか。
- ベストアンサー
- Excel(エクセル)
- VBA のメッセージボックスに・・・
Private Sub CommandButton1_Click() Dim intMsg As Integer Dim x As Double x = Val(Cells(3, 2).Value) / Val(Cells(2, 2).Value) ^ 2 Cells(2, 5) = x intMsg = MsgBox("指数は&x&" & vbCrLf & "&z&です。") Select Case x Case 0 To 18 MsgBox "やせぎみ" Case 19 To 25 MsgBox "普通" Case 26 To 30 MsgBox "太り気味" Case Else MsgBox "危険" End Select End Sub ってプログラムを組んだのですが メッセージボックスに結果が表示されません。 計算して出た値を、メッセージボックス内のxとzで表示させたいのですが・・・ &ではさむ?だけではだめなんですか?
- ベストアンサー
- Visual Basic
- マクロで文字列をブック全体に一括置換する
'マクロで一括置換する方法です。 'たくさんのシートに記入された文字列を複数のシートに渡って一括置換する方法を教えてください。 '置換前と置換後の文字をインプットボックスに記述しブック全体の文字を一括で置換(変更)する 'その後は戻せなくても結構です。 下のように記述しましたが、うまく動作しません よろしくお願いします。 Private Sub CommandButton11_Click() Dim BeforeStr As Integer Dim AfterStr As Integer Dim WS As Worksheet Dim N As Integer For N = 1 To Worksheets.Count Set WS = Worksheets(N) For i = 1 To 100 For j = 1 To 200 Before.Cells(i, j).Value = InputBox("置換前の文字列を入力してください。") After.Cells(i, j).Value = InputBox("置換後の文字列を入力してください。") For Each WS In Worksheets WS.Activate For Each s In WS.Cells If s.Name Like "InputBox*" Then s.Select Selection.Cells(i, j).Value = _ Replace(Selection.Cells(i, j).Value, BeforeStr, AfterStr) End If Next j Next i Next N Next End Sub
- 締切済み
- オフィス系ソフト
- エクセル2003(VBA)で違うフォームのチェックボックス配列を繰り返し処理したい
エクセル2003(VBA)で違うフォームのチェックボックス配列を 元に順番に繰り返し処理したいのですが、うまくいきません。 どうかお知恵をお貸し下さい。 よろしくお願いします。 ■<ユーザーフォーム1>(本体フォーム) Private Sub CommandButton1_Click() UserForm2.Show Dim n As Integer Dim ender As Integer ender = 1 i = 1 For n = 1 To 20 If i > 4 Then i = 1 '4を超えたら1に戻る If cb(i).Value = False Then 'チェックがなかったら飛ばす i = i + 1 '次の配列へ n = n - 1 'ループ回数をカウントしないように1引く Else Worksheets("sheet1").Cells(ender, 5).Select Worksheets("sheet1").Cells(ender, 5) = "■" & cb(i).Caption & " = " & cb(i).Value ender = ender + 1 i = i + 1 End If Next n End Sub ■<ユーザーフォーム2>(チェックボックスフォーム) Private Sub CommandButton1_Click() Const cb_num = 4 'チェックボックスを4個に設定 Dim cb(cb_num) As Control Dim ctrl As Control Dim i As Integer i = 0 For Each ctrl In Me.Controls 'コントロールの数だけループする If TypeName(ctrl) = "CheckBox" Then 'タイプがチェックボックスなら i = i + 1 If i > cb_num Then Exit For 'チェックボックスの数を超えたコントロールは無視 Set cb(i) = ctrl 'コントロールを配列に代入 End If Next ctrl End Sub
- ベストアンサー
- Visual Basic
- VBAのUserFormでサブルーチンを用いる
UserFormのコードに次のように書いてbuttomを押してみると コンパイルエラー:ByRef引数の型が一致しません。 と出てしまいます。 どこが間違っているのでしょうか?ご回答お願いします。 Private Sub buttom_Click() Dim i As Integer Dim name As String i = 1 name = "名前" Call test(i , name) End Sub ---------------------------------------------- Sub test(i As Integer, name As String) Cells(i , 1) = name End Sub
- ベストアンサー
- その他MS Office製品
- VBAでフォームにおけるコマンドが実行されません。
なんどもすいません。二列目に、二桁の整数の足し算を出題することができるたし算の作問プログラムと、続いて三列目に、足し算の解答をして、それの正誤を確かめるプログラムを、それぞれフォームで”問題”、”採点”とした時、一回フォームのウィンドウを閉じてしまうと”採点”のコマンドを押しても、うまく実行されません。 im Ans() As Long Dim n As Variant Private Sub monndai_Click() Dim x As Long, y As Long Columns("B:F").Clear n = InputBox("問題数は?") If Not IsNumeric(n) Then Exit Sub If n <= 0 Then Exit Sub ReDim Ans(n) As Long For i = 1 To n Randomize x = Int(Rnd * 100) Randomize y = Int(Rnd * 100) Ans(i) = x + y Cells(i, 2) = "(" & i & ") " & x & " + " & y & " = " Next i End Sub Private Sub saiten_Click() t = 0 Dim i As Long For i = 1 To n If Cells(i, 3) = Ans(i) Then Cells(i, 4) = "○": t = t + 1 Else Cells(i, 4) = "×": End If Next i tokutenn = "貴方の正答率は" & Int(t / n) & "%です" End Sub (注)tokutennはフォームのテキストボックスのオブジェクト名です。
- ベストアンサー
- オフィス系ソフト
- VBA
ExcelのVBAについて質問です。 1000以下の任意の整数nを画面入力から読み込んで、1+2+3+4+・・・+nの合計値をセルA1に表示させたいのですが、1とnの和が足されてしまいます。 どこがおかしいのでしょうか? 指摘お願いします。 以下テキストです。 スペースなどは気にせずテキストの部分でご指摘お願いします Sub () Dim i As Integer Dim n As Integer n = Application.InputBox(Prompt:="1000以下の整数を入力してください", Type:=1) If n <= 1000 Then For i = 1 To n iResult = i + 1 Cells(1, 1) = iResult Next i End Sub よろしくおねがいします。
- ベストアンサー
- オフィス系ソフト
お礼
そういうことでしたか? 実際のプログラムには:n=1 とは書いていましたが、これだと他にも応用できそうですね。