• 締切済み

■Excel VBA グローバルな書き方■

Sub 跳ね返る() Dim time1 As Integer, time As Integer Dim X As Integer, Y As Integer Dim hyouji As String, yoko As String, tate As String hyouji = "●" X = 1 Y = 1 yoko = "右" tate = "上" Do Cells(X, Y).Value = hyouji '★ For time1 = 0 To 1000 For time2 = 0 To 1000 Next Next Cells(X, Y).Value = hyouji For time1 = 0 To 1000 For time2 = 0 To 1000 Next Next          '★ If yoko = "右" Then Y = Y + 1 Else Y = Y - 1 End If If Y = 30 Then yoko = "左" ElseIf Y = 1 Then yoko = "右" End If If tate = "上" Then X = X + 1 Else X = X - 1 End If If X = 20 Then tate = "下" ElseIf X = 1 Then tate = "上" End If Loop End Sub ************************************ 上記のプログラムを Dim a() EndSub Dim b() EndSub Dim c() endSub Sub main() a b c EndSub のような、mainを動かせばabcも動く グローバルな(ローカルでもいいのですが) プログラムにするにはどうしたらいいですか? ★印から★印までの間の動作が同じような動作で 二つあるので、それを一つにまとめ 尚且つ、表示と時間稼ぎと表示削除の 3つの動作を分けた形にしたいです。 質問が下手で申し訳ありません…;;

みんなの回答

回答No.1

こういうことでしょうか? Dim X As Integer, Y As Integer Dim Yoko As String, Tate As String Sub 座標移動() End Sub Sub 描画(ByVal 文字 As String) End Sub Sub 待機() End Sub Sub Main() X = 1: Y = 1 Yoko = "右": Tate = "上" Do 描画 "●" 待機() 描画 "" 待機() 座標移動() Loop End Sub 中身は既存コードをうまく使って埋めてください。 的外れならスルーで・・・

moguraaaaa
質問者

お礼

的中です! ありがとうございます!! でも私がプログラムをかくと 上手く動かないという…;;

関連するQ&A

  • ▲ExcelのVBA▼困っています

    何度もVBAで質問させてもらい助けてもらっています。 懲りずにまた質問ですが… 下のプログラムは"●"が跳ね返るものなのですが… ●の後を■と▲が追うようなプログラムにするには なにを追加すればいいのでしょうか…?; どなたか教えて下さい;;お願いします;; Dim time1 As Integer, time As Integer Dim X As Integer, Y As Integer Dim maru As String, yoko As String, tate As String Sub 描画() Cells(X, Y).Value = maru End Sub Sub 削除() Cells(X, Y).Value = "" End Sub Sub 待機() For time1 = 0 To 1000 For time2 = 0 To 1000 Next Next End Sub Sub 座標移動() If yoko = "右" Then Y = Y + 1 Else Y = Y - 1 End If If Y = 30 Then yoko = "左" ElseIf Y = 1 Then yoko = "右" End If If tate = "上" Then X = X + 1 Else X = X - 1 End If If X = 20 Then tate = "下" ElseIf X = 1 Then tate = "上" End If End Sub Sub main() maru = "●" X = 1 Y = 1 yoko = "右" tate = "上" Do 描画 待機 削除 待機 座標移動 Loop End Sub

  • ●Excel VBA 配列●教えて下さい

    a~tの文字が順々に文字を追っていくプログラムにしたいと思い 配列を使用したのですが…プログラムが稼動しません、 下記のプログラムでは何が足りないのでしょうか わかる方いたら教えて下さい; 配列の使い方についてアドバイスがあれば そちらも教えていただきたいです…。 '――ここから―― Dim time1 As Integer, time2 As Integer, n As String Dim X As Integer, Y As Integer Dim yoko As String, tate As String Dim suuji (19) As String Sub 描画() Cells(X, Y).Value = suuji End Sub Sub 削除() Cells(X, Y).Value = "" End Sub Sub 待機() For time1 = 0 To 1000 For time2 = 0 To 1000 Next Next End Sub Sub 座標移動() If yoko = "右" Then Y = Y + 1 Else Y = Y - 1 End If If Y = 30 Then yoko = "左" ElseIf Y = 1 Then yoko = "右" End If If tate = "上" Then X = X + 1 Else X = X - 1 End If If X = 20 Then tate = "下" ElseIf X = 1 Then tate = "上" End If End Sub Sub main() suuji (0) = a suuji (1) = b suuji (2) = c suuji (3) = d suuji (4) = e suuji (5) = f suuji (6) = g suuji (7) = h suuji (8) = i suuji (9) = j suuji (10) = k suuji (11) = l suuji (12) = m suuji (13) = n suuji (14) = o suuji (15) = p suuji (16) = q suuji (17) = r suuji (18) = s suuji (19) = t For n = 0 To 19 Cells(X,Y).Value = suuji (n) Next X = 1 Y = 1 yoko = "右" tate = "上" Do 描画 待機 削除 待機 座標移動 Loop End Sub '――ここまでです―― 何度も同じような質問をさせてもらってすみません;

  • ※VBA配列

    http://oshiete1.goo.ne.jp/qa5196795.htmlで 質問させてもらった者です。質問不足だったため 質問の内容を追加したかったのですが、追加の方法がわからず またこちらで質問させていただきました Dim time1 As Integer, time As Integer Dim X As Integer, Y As Integer Dim X1 As Integer, Y1 As Integer Dim X2 As Integer, Y2 As Integer Dim maru As String, yoko As String, tate As String Dim sankaku As String, shikaku As String Sub 描画() Cells(Y2, X2).Value = shikaku Cells(Y1, X1).Value = sankaku Cells(Y, X).Value = maru End Sub Sub 削除() Cells(Y2, X2).Value = "" End Sub Sub 待機() For time1 = 0 To 1000 For time2 = 0 To 1000 Next Next End Sub Sub 座標移動() X2 = X1 Y2 = Y1 X1 = X Y1 = Y If yoko = "右" Then X = X + 1 Else X = X - 1 End If If X = 30 Then yoko = "左" ElseIf X = 1 Then yoko = "右" End If If tate = "上" Then Y = Y + 1 Else Y = Y - 1 End If If Y = 20 Then tate = "下" ElseIf Y = 1 Then tate = "上" End If End Sub Sub main() maru = "●" sankaku = "▲" shikaku = "■" X = 1 Y = 1 X1 = 1 Y1 = 1 X2 = 1 Y2 = 1 yoko = "右" tate = "上" Do 描画 待機 削除 待機 座標移動 Loop End Sub a~tの文字が、上記のような動きをする プログラムを作成するにはどのように配列を活かせばいいですか? 配列がよくわかっておらず勉強したのですが…使えずにいます;;

  • Excel VBA ・・・教えてください

    何度も質問させて頂いてます。すみません、 下記のプログラムはこの場で教えて頂いたプログラムで、 実行すると●の後を▲や■が追いかける動きをします。 下記のプログラムをある程度使用して 1~20の数字が順々で追いかけっこする プログラムを作成するにはどのようにすればいいのでしょうか… できればプログラムは長めにならず 20の数字から簡単に増やすことのできるような そんなプログラムが作成したいです… どなたかアドバイスお持ちの方 教えて下さいお願いします... Dim time1 As Integer, time As Integer Dim X As Integer, Y As Integer Dim X1 As Integer, Y1 As Integer Dim X2 As Integer, Y2 As Integer Dim maru As String, yoko As String, tate As String Dim sankaku As String, shikaku As String Sub 描画() Cells(Y2, X2).Value = shikaku Cells(Y1, X1).Value = sankaku Cells(Y, X).Value = maru End Sub Sub 削除() Cells(Y2, X2).Value = "" End Sub Sub 待機() For time1 = 0 To 1000 For time2 = 0 To 1000 Next Next End Sub Sub 座標移動() X2 = X1 Y2 = Y1 X1 = X Y1 = Y If yoko = "右" Then X = X + 1 Else X = X - 1 End If If X = 30 Then yoko = "左" ElseIf X = 1 Then yoko = "右" End If If tate = "上" Then Y = Y + 1 Else Y = Y - 1 End If If Y = 20 Then tate = "下" ElseIf Y = 1 Then tate = "上" End If End Sub Sub main() maru = "●" sankaku = "▲" shikaku = "■" X = 1 Y = 1 X1 = 1 Y1 = 1 X2 = 1 Y2 = 1 yoko = "右" tate = "上" Do 描画 待機 削除 待機 座標移動 Loop End Sub

  • VBA マクロについて

    自作のカレンダーに自動で日付を判定、入力してくれる ロジックを作っていたのですが、 2、4、6、9、11月以外は31日分表示されるはずが。。。 表示されませんでした。 恐らくロジックがおかしくて i=31 が通っていないものと 思われますが、ちょっとよく分かりません。 初心者で低レベルな質問ですけど、どなたかお願いします。 Sub AutoCarender() '自動でカレンダーの日付を入力するプログラム Dim month, i As Integer '表示させたい月 month = 3 If (month = 2) Then i = 28 ElseIf (month = 4 Or 6 Or 9 Or 11) Then i = 30 Else i = 31 End If Dim tate, yoko As Integer Dim week As Integer week = (Weekday(2009 / month / 1, 2)) yoko = Choose(week, 1, 3, 5, 7, 9, 11, 13) tate = 3 For j = 1 To i '"シートの名前"を指定 Worksheets("Sheet1").Cells(tate, yoko).Value = j yoko = yoko + 2 If (yoko > 13) Then yoko = 1 tate = tate + 2 End If Next End Sub

  • ExcelのVBAについて

    ExcelのVBAを使って 縦20のセル、横30のセルの枠を作り 枠内で●が動き、枠に当たると跳ね返りの 繰り返しプログラムが作りたいんですが… プログラムはここまでできています↓ Sub 試作品() Dim hara As Integer, kyo As Integer Dim X As Integer, Y As Integer Dim V As String Dim hyouji As String hyouji = "●" 'hyoujiという変数に●を入れる X = 1 'Xに1を入れる Y = 1 V = "" V = "上" '------------------------------------------ Do Cells(X, Y).Value = hyouji 'XとYの位置にあるセルに●を表示 For hara = 0 To 10000 ' For kyo = 0 To 1000 'タイマーの役割 Next ' Next ' Cells(X, Y).Value = "" 'XとYの位置にあるセルに●を消す For hara = 0 To 10000 ' For kyo = 0 To 1000 'タイマーの役割 Next ' Next ' If V = "上" Then '上から来た●なら X = X + 1 'Xに1を足す Y = Y + 1 Else: V = "下" '下から来た●なら X = X - 1 'Xから1を引く Y = Y - 1 End If If X = 20 Then 'Xが20になったら V = "下" 'ループ外で定義した"上"を下に反転させる ElseIf X = 1 Then 'Xが1になったら V = "上" '下を上に反転 End If If Y = 30 Then V = "下" ElseIf Y = 1 Then V = "上" End If Loop '------------------------------------------ End Sub できたらこのプログラムに追加して 作成したいのですが… この際できれば嬉しいので これ以外のプログラムがあれば 教えて欲しいです。 VBA得意な方、お願いします;;

  • VBAで行列を作る方法

    次のようなプログラミングで1,0,-1の要素で作られる3×3行列を全通り調べています。 この場合3の9乗通り調べることができます。 これを4×4や5×5行列など数を大きくして調べたいのですが、このプログラムを配列を使うなどして 簡単にできる方法を教えてください。 よろしくおねがいします。 Sub test() Dim a As Integer '行 Dim b As Integer '列 Dim c As Integer, i As Integer, j As Integer, d As Integer, e As Integer Dim 内積 As Integer, step As Integer Dim f As Integer, g As Integer, h As Integer, l As Integer, m As Integer, n As Integer, k As Integer, x As Integer Dim sum As Integer, total As Integer Dim aa As Integer, aaa As Integer, aaaa As Integer, bb As Integer, bbb As Integer, bbbb As Integer a = 3 '行 b = 3 '列 c = 0 内積 = 0 con = 0 sum = 0 tatal = 0 aa = 0 aaa = 0 aaaa = 0 bb = 0 bbb = 0 bbbb = 0 x = 0 For n = 0 To 2 For m = 0 To 2 For l = 0 To 2 For k = 0 To 2 For h = 0 To 2 For g = 0 To 2 For f = 0 To 2 For e = 0 To 2 For d = 0 To 2 '要素がすべて1 For i = 1 To a For j = 1 To b Cells(i, j) = 1 Next j Next i If bbbb = 1 Then Cells(a - 2, b - 2) = 0 ElseIf bbbb = 2 Then Cells(a - 2, b - 2) = -1 End If If bbb = 1 Then Cells(a - 1, b - 2) = 0 ElseIf bbb = 2 Then Cells(a - 1, b - 2) = -1 End If If bb = 1 Then Cells(a, b - 2) = 0 ElseIf bb = 2 Then Cells(a, b - 2) = -1 End If If aaaa = 1 Then Cells(a - 2, b - 1) = 0 ElseIf aaaa = 2 Then Cells(a - 2, b - 1) = -1 End If If aaa = 1 Then Cells(a - 1, b - 1) = 0 ElseIf aaa = 2 Then Cells(a - 1, b - 1) = -1 End If If aa = 1 Then Cells(a, b - 1) = 0 ElseIf aa = 2 Then Cells(a, b - 1) = -1 End If If total = 1 Then Cells(a - 2, b) = 0 ElseIf total = 2 Then Cells(a - 2, b) = -1 End If If sum = 1 Then Cells(a - 1, b) = 0 ElseIf sum = 2 Then Cells(a - 1, b) = -1 End If If con = 1 Then Cells(a, b) = 0 ElseIf con = 2 Then Cells(a, b) = -1 End If con = con + 1 Next d con = 0 sum = sum + 1 Next e sum = 0 total = total + 1 Next f total = 0 aa = aa + 1 Next g aa= 0 aaa = aaa + 1 Next h aaa = 0 aaaa = aaaa + 1 Next k aaaa = 0 bb = bb + 1 Next l bb = 0 bbb = bbb + 1 Next m bbb = 0 bbbb = bbbb + 1 Next n End Sub

  • Excel VBAで検索(Win2000 Excel2000)

    現在、下記のようなコードを書いています。データテーブルの縦と横の検索値を探してその列数と行数を返したいのですが、下記の Sub検索1 と Sub検索2 を1つのSubで実行させるにはどうしたらよいのでしょうか?よろしくお願い致します。 ----------------------------------------- Sub 検索1() Worksheets("Data").Activate Dim x As Integer For x = 3 To 22 If Cells(2, x).Value >= 12 Then MsgBox x Exit Sub End If Next MsgBox "見つかりません" End Sub --------------------------------------------- Sub 検索2() Worksheets("Data").Activate Dim i As Integer For i = 4 To 42 If Cells(i, 2).Value = "A" Then MsgBox i Exit Sub End If Next MsgBox "見つかりません" End Sub

  • VBEについて

    例えば Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range) '***** 特定セルを選択するとイベントを実行する ***** Dim tate As Long Dim yoko As Long Dim 数量 As Long Dim 判定 As Integer tate = ActiveCell.Row yoko = ActiveCell.Column If yoko = 2 Then On Error GoTo skip01 数量 = InputBox("数量を入力してください。") 判定 = MsgBox("数量 = " & 数量 & " で間違いありませんか?", vbYesNo + vbInformation) If 判定 = vbYes Then Cells(tate, 2) = Cells(tate, 2) + 数量 End If End If skip01: End Sub ですが、上記コードでは2行目のみの物になりますが、例えば7と8行目にしたいときなどはどうすればいいのでしょうか?素人質問ですが宜しくお願い致します。

  • マインスイーパーもどき(VBA)

    ExcelのVBAでマインスイーパーもどきを作っていますが、どうも上手くいきません。9が地雷で0が何もないと言うことです。以下、ソースです。 Sub mine() Dim minefield(11, 13) As Integer Dim i As Integer, a As Integer, b As Integer, x As Integer x = 0 Randomize For i = 1 To 20 a = Int(Rnd * 10) + 1 b = Int(Rnd * 12) + 1 If minefield(a, b) = 9 Then x = x + 1 i = i - 1 End If minefield(a, b) = 9 Next i MsgBox "地雷が" & x & "回重複" countMine minefield, 10, 12 showInt minefield, 10, 12 show minefield, 10, 12 End Sub Sub show(f() As Integer, h As Integer, w As Integer)  Dim a As Integer, b As Integer For a = 1 To 10 For b = 1 To 12 If (f(a, b) = 9) Then CStr(f(a, b)) = "*" If (f(a, b) = 0) Then CStr(f(a, b)) = " " Next b Next a End Sub If (f(a, b) = 9) Then CStr(f(a, b)) = "*" If (f(a, b) = 0) Then CStr(f(a, b)) = " " の部分でコンパイル時に 「コンパイルエラー:修正候補 識別子」 とでます。ヘルプを見てもよくわかりませんでした。 テキストが長すぎるので途中は省きました。 よろしくお願いします。

専門家に質問してみよう