• ベストアンサー

このVBAソースのどこが間違ってるか教えてください

Dim csp As Integer 'ストップ Dim css As Integer 'ストップorスタート Sub Quest2() If css = 0 Then css = 1 Quest2a Else cstp = 1 css = 0 End If End Sub Sub Quest2a() Sheets("Sheet1").Select Range("B1:J10").Select Selection.Interior.ColorIndex = x1None Range("a1").Select cstp = 0 Do r = 1: c = 5 For i = 0 To 15 If i < 9 Then Cells(r, c).Interior.ColorIndex = x1None r = r + 1 If i < 5 Then c = c + 1 Else c = c - 1 End If Cells(r, c).Interior.ColorIndex = 3 Else Cells(r, c).Interior.ColorIndex = x1None r = r - 1 If i < 13 Then c = c - 1 Else c = c + 1 End If Cells().Interior.ColorIndex = 3 End If 'タイミング For tm1 = 1 To 1000: For tm2 = 1 To 100: Next If cstp = 1 Then Exit For End If Next DoEvents If cstp = 1 Then Exit For End If If r = 3 And c = 5 Then Cells(r, c).Interior.ColorIndex = x1None End If Next DoEvents If cstp = 1 Then Exit Do End If Loop Cells(10, 9) = Cells(r, c) Cells(10, 9).Interior.ColorIndex = 8 End Sub

質問者が選んだベストアンサー

  • ベストアンサー
  • wuyan
  • ベストアンサー率51% (183/352)
回答No.2

とりあえず明らかに間違っているのは x1None (数字の一) ではなくて xlNone (アルファベットのエル) が正解。

phantom_cr
質問者

お礼

有難うございますそうだったんですか・・・ 色も動作もおかしなことになっているので、その点を修正確認してみます

その他の回答 (1)

回答No.1

こんにちは。 まず、何をしたいのかがわからない・・・。 変数も突然出現している(エラーではないが、望ましくない)。 タイミングのところのFor文の意味が不明。 ソースだけ載せるのもどうかと・・・。 (^^ゞ

phantom_cr
質問者

補足

申し訳ありません 送信完了してから補足文入れてなかったことに気がつきまして・・・ このVBAのソースは簡易ルーレット用のソースですよろしくお願いします。

関連するQ&A

  • VisualBasic compile error

    Sub Macro1r1() Dim r As Long, rr As Long Dim c As Long, cc As Long Dim w As Long, ww As Long Dim stopC As Long stopC = Range("IV1").End(xlToLeft).Column Range(Range("E1"), Cells(1, stopC)).DataSeries rowcol:=xlRows, Type:=xlDataSeriesLinear, Date:=xlDay, Trend:=True For r = 2 To Range("A65536").End(xlUp).Row Cells(r, "B").Resize(1, 3).ClearContents w = 0 For c = 5 To stopC If Cells(r, c).Interior.ColorIndex <> xlNone And Cells(r, c).Interior.ColorIndex <> xlAutomatic Then Cells(r, "B") = Cells(1, c).Value For cc = c To stopC If Cells(r, cc).Interior.ColorIndex = xlNone Or Cells(r, cc).Interior.ColorIndex = xlAutomatic Then Exit For End If w = w + 1 Next cc If cc = stopC Then Cells(r, "C") = Cells(1, cc - 1).Value + 5 Else For rr = cc To stopC If Cells(r, rr).Interior.ColorIndex <> xlNone And Cells(r, rr).Interior.ColorIndex <> xlAutomatic Then For ww = rr To stopC If Cells(r, ww).Interior.ColorIndex = xlNone Or Cells(r, ww).Interior.ColorIndex = xlAutomatic Then Exit For End If w = w + 1 Next ww Next rr <------------------------- 次のerrorメッセ-ジ next without for が表示される。 Cells(r, "C") = Cells(1, rr - 1).Value + 5 End If Cells(r, "D") = w * 7 Exit For End If Next c Next r End Sub

  • ExcelのVBAがオーバーフローに!?

    読んで頂きありがとうございます。 以前に質問しました時に回答を頂き凄く助かっていたのですが、何故か「オーバーフロー」と表示されてしまいます。 ひょっとしたら桁なのかも知れません。 下記のVBAを教えて頂きしようしていました。 Option Explicit Sub Test() Dim c(), p(), q(), i, j, l, r, k As Long r = Range("B2").End(xlDown).Row l = Range("D2").End(xlDown).Row ReDim c(r - 2), p(r - 2), q(r - 2) For i = 1 To r - 2 c(i) = Cells(i + 2, 2).Value p(i) = Cells(i + 2, 3).Value q(i) = i + 2 Next i For i = 1 To r - 3 For j = i + 1 To r - 2 If c(i) > c(j) Then k = c(i) c(i) = c(j) c(j) = k k = p(i) p(i) = p(j) p(j) = k k = q(i) q(i) = q(j) q(j) = k End If Next j Next i For i = 1 To r - 2 Cells(i + 2, 7).Value = c(i) For j = 3 To r If Cells(j, 4).Value = c(i) Then Cells(i + 2, 8).Value = p(i) - Cells(j, 5).Value If p(i) - Cells(j, 5).Value <> 0 Then Range("B" & q(i)).Interior.ColorIndex = 6 Range("D" & j).Interior.ColorIndex = 6 End If Exit For End If Next j Next i For i = 3 To r k = 0 For j = 3 To l If Cells(i, 2).Value = Cells(j, 4).Value Then k = 1 Exit For End If Next j If k = 0 Then Range("B" & i).Interior.ColorIndex = 35 End If Next i For i = 3 To l k = 0 For j = 3 To r If Cells(i, 4).Value = Cells(j, 2).Value Then k = 1 Exit For End If Next j If k = 0 Then Range("D" & i).Interior.ColorIndex = 35 End If Next i End Sub デパックのClickすると15行目辺りの「k = c(i)」が黄色くなります。 正直言ってマクロとか全く解りませんが、コピーして使っていました。 どこをどう変更すれば良いのか教えて頂ければ助かります。 よろしくお願いします。

  • VBA TimeValue関数が異常

    お願いします・・ 以下の関数が実行できません・・ Sub te() ’’セルには時刻9:59を入れています If Cells(1, 1).Value = TimeValue("9:59") Then Cells(1, 1).Interior.ColorIndex = 6 End If End

  • EXCEL VBA2010 MsgBox

    Sub 重複() Dim i As Long, j As Long For i = 6 To 500 For j = 3 To 3 If WorksheetFunction.CountIf(Range("C6:C500"), Cells(i, j)) > 1 Then Cells(i, j).Interior.ColorIndex = 6 End If Next j Next i End Sub このVBAに重複が何件ありますよというメッセージを出したいです。 MsgBoxの入れ方を教えてください。

  • VBAでの計算後のセルに2重線で囲む

    まだPC・VBA不慣れな為、実行できないので、教えてください。 c16セルに休日を入力すると無理つぶしは成功しましたが、c16セルに祭日を入力すると赤の2重線で囲みたいのですが、できませんので、方法をお願いします。 もう1点がCELLS・RANGEを使った2種類の方法をお願いします。 よろしくお願いします。 Sub 練習44() Dim kyuyo As Currency If Range("c16").Value = "祭日" Then Worksheets("練習1If~Then").Cells(16, 3).xlDouble.ColorIndex = 3 ElseIf Range("c16").Value = "休日" Then Worksheets("練習1If~Then").Cells(16, 3).Interior.ColorIndex = 5 Else Worksheets("練習1If~Then").Cells(16, 3).Interior.ColorIndex = 10 End If End Sub

  • Excel2003 VBA 文字を打ち込んだだけで色が変わる方法

    突然失礼いたします。 前回、ファイルを開いたときに、青文字は無視、マイナスのときは赤に塗りつぶし、プラスのときは緑に塗りつぶし、それ以外は無視というプログラムを作りました。 今回は、それを文字を打っただけで実行できるようにしたいのですが、良い案はありますでしょうか。 ボタンを押すと・・・って処理でも良いんですが、それだと部長が忘れる可能性が高いんで・・・(^^;) 前回のプログラム: Private Sub Workbook_Open() Dim 英語 As Integer Dim 数字 As Integer Dim sh1 As Worksheet 英語 = 1 数字 = 1 For 数字 = 1 To 17 For 英語 = 1 To 7 '選択セル内が、文字色が青だったら無視、数字がマイナスだったら赤、数字がプラスだったら緑、それ以外は無視 If Cells(英語, 数字).Font.ColorIndex = 5 Then Cells(英語, 数字).Interior.ColorIndex = 0 ElseIf Cells(英語, 数字) < 0 Then Cells(英語, 数字).Interior.ColorIndex = 7 ElseIf Cells(英語, 数字) > 0 Then Cells(英語, 数字).Interior.ColorIndex = 4 Else Cells(英語, 数字).Interior.ColorIndex = 0 End If Next 英語 Next 数字 End Sub

  • VBAで教えてください。

    以前ここで教えていただいたVBAで http://jisaku.155cm.com/src/1371930716_9b9006528605642980beed48a8998013b0731e4b.jpg のようにA列のテスト4をクリックしたときにC列のテスト4が一発で解るようにしたいです。 もちろん、テスト11をクリックしたときは、テスト4塗りつぶしは解除され、 テスト11が塗りつぶされるようにしたいです。 写真は塗りつぶししていますが、解るようにしたいだけなので、塗りつぶしにはこだわっていません。 あと、E、F、G列は解りやすく並べているだけで、実際はA、B、C列だけです。 それと、C列は関数を使って表示してあります。 という質問で Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'この行から Dim i As Long Range("C:C").Interior.ColorIndex = xlNone If Application.Intersect(Target, Range("A:A")) Is Nothing Or Target.Count <> 1 Then Exit Sub On Error Resume Next Application.ScreenUpdating = False ActiveSheet.Cells.interio.ColorIndex = xlNone For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row If Cells(i, "C") = Target Then Cells(i, "C").Interior.ColorIndex = 3 End If Next i Application.ScreenUpdating = True End Sub 'この行まで をシートのコードに張り付ければいいですよ。と教えてくれたものがあるのですが、 A列でクリックした文字をC列からすべて見つけて反転してくれないようです。何個か反転してくれない ものが出てきてしまいました。 C列が何百行とかなってしまうと、すべての同じ文字を検索してくれないのでしょうか? ちなみに列がここに掲載しているものと違うので Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'この行から Dim i As Long Range("R:R").Interior.ColorIndex = xlNone If Application.Intersect(Target, Range("B:B")) Is Nothing Or Target.Count <> 1 Then Exit Sub On Error Resume Next Application.ScreenUpdating = False ActiveSheet.Cells.interio.ColorIndex = xlNone For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row If Cells(i, "R") = Target Then Cells(i, "R").Interior.ColorIndex = 3 End If Next i Application.ScreenUpdating = True End Sub 'この行まで のCをRにAをBに変更して使ってます。 これがいけないのかな? よろしくお願いします。

  • 2つのVBAを組み合わせる方法

    お世話になります、2つのVBAを組み合わせる方法で迷っています。 1つ目が Private Sub Worksheet_Change(ByVal Target As Range) Dim i As Long, k As Long, myNum As Long If Intersect(Target, Range("C1,B9:B39")) Is Nothing Or Target.Count > 1 Then Exit Sub Application.EnableEvents = False With Target If .Column = 3 Then myNum = WorksheetFunction.Max(Range("B9:B39")) If IsDate(.Value) Then For i = 9 To 39 If Cells(i, "A").Value = "" Then Cells(i, "B").Value = "" Else Cells(i, "B") = myNum + i - 8 End If Next i End If Else i = .Row If .Value = "" Then Range(Cells(i + 1, "B"), Cells(39, "B")).ClearContents Else For k = i + 1 To 39 If Cells(k, "A").Value = "" Then Cells(k, "B").Value = "" Else Cells(k, "B") = Cells(k - 1, "B") + 1 End If Next k End If End If End With Application.EnableEvents = True End Sub です。 2つめが Private Sub Worksheet_Change(ByVal Target As Range)  Application.EnableEvents = True If Intersect(Target, Range("R8:R38")) Is Nothing Then Exit Sub Application.EnableEvents = False Range(Cells(Target.Row, 18), Cells(39, 18)).Value = Target.Value Application.EnableEvents = True End Sub です。2つのPrivate Sub Worksheet_Change(ByVal Target As Range)イベントのVBAですが、どのようにして組み合わせれば良いのでしょうか?

  • エクセル マクロ 複数セルの色付けについて

    マクロ初心者で苦戦しており、教えていただけると大変ありがたいです。 エクセルの複数のシートに、問2_1 、問2_2 、問2_10 (全部で問は2_28まであります)等の文字列があり、その文字に合わせて、そのセルだけ色を付けたいと考えております。 今書いているコードだと、ワークシートのA列にある場合は色が付けられるのですが、それ以外に色が付けられません。どこが間違っているでしょうか? どうぞご指導お願いいたします。 Sub 色つけ() Dim Maxrow As Long Dim i As Long Maxrow = Cells(Rows.Count, i).End(xlUp).Row For i = 1 To Maxrow If InStr(Cells(i, 1), "2_1 ") > 0 Then Cells(i, 1).Interior.ColorIndex = 6 If InStr(Cells(i, 1), "2_2 ") > 0 Then Cells(i, 1).Interior.ColorIndex = 6 If InStr(Cells(i, 1), "2_23 ") > 0 Then Cells(i, 1).Interior.ColorIndex = 23 If InStr(Cells(i, 1), "2_13 ") > 0 Then Cells(i, 1).Interior.ColorIndex = 4 If InStr(Cells(i, 1), "2_6 ") > 0 Then Cells(i, 1).Interior.ColorIndex = 7 Next i End Sub

  • 値によってセル塗りつぶし

    初心者です。 データの中から、セルC5からC88の中で数値が300以上はセルを赤に塗りつぶし、250以上300未満は青、200以上250未満は黄色、などとやりたいのですが、if...then...elseステートメントを組んでやってみると「型が一致しません。」となってしまいます。 Sub 選択範囲処理() Range("c5:c88").Select If Range("c5:C88") >= 300 Then Interior.ColorIndex = 3 ElseIf Range("c5:c88") <= 300 And Range("c5:C88") >= 250 Then Interior.ColorIndex = 5 ElseIf Range("c5:c88") <= 250 And Range("c5:c88") >= 200 Then Interior.ColorIndex = 6 ElseIf Range("c5:c88") <= 200 And Range("c5:c88") >= 150 Then Interior.ColorIndex = 20 Else Interior.ColorIndex = 10 End If End Sub ご教示頂けると大変助かります。 よろしくお願い致します。

専門家に質問してみよう