• ベストアンサー

Excellマクロで数値0と空白部分の認識?

imaruyaの回答

  • imaruya
  • ベストアンサー率34% (63/181)
回答No.1

V = Cells(i, j).Value とセルの値をVに入れる前に、セルの値が空白かどうかを判定したらいかがでしょうか。

now2150
質問者

お礼

早速の回答、有難うございます。 IsEmpty 関数を使うのでしょうか? ヘルプの使用例を見ても具体的にどういう式にするか分かりません。

関連するQ&A

  • 塗る部分と塗らない部分を条件によってわけたいのですが・・・

    今回困っているのは下のマクロで (1)紺なら足跡を残し、青なら進んでいるセルだけ青にして前回の青の足跡は残さないで (2)セルが紺の足跡でいっぱいになるまで、これを続けます。 この条件をここに追加しようと思ったのですが、なかなかうまく行きません。ヒントだけでも、何か名案がありましたらご回答お願いします。 Const IMAX As Long = 30 '最大 i 座標 Const JMAX As Long = 20 '最大 j 座標 Const NMAX As Long = 50 '最大ステップ数 Const motigomi As Long = 5 'ゴミ「○歩につき●個捨てる」数 Dim ip(MMAX) As Long '人の i 座標 Dim jp(MMAX) As Long '人の j 座標 Dim occ(IMAX, JMAX) As Long Dim pre(IMAX, JMAX) As Integer Dim post(IMAX, JMAX) As Integer Sub ashiato() Randomize Cells.Clear For n = 1 To NMAX 'ステップを進める For m = 1 To 1   iprev = ip(m) '元いた位置 jprev = jp(m) '移動先 (i, j) を決める i = i - 1 '周期境界条件 If i > IMAX Then i = i - IMAX If i < 1 Then i = i + IMAX If j > JMAX Then j = j - JMAX If j < 1 Then j = j + JMAX '実際に移動 ip(m) = i jp(m) = j '色付けを更新 pre(i, j) = Int(motigomi * Rnd()) If pre(i, j) = 1 Then Cells(i, j).Interior.color = RGB(10, 50, 100) Else Cells(iprev, jprev).Clear Cells(i, j).Interior.color = RGB(40, 50, 400) End If Next Next End Sub

  • 全体を表示 マクロ 色が思うように、表示できない

     「マクロ 色が思うように、表示できない」で質問したことへの追加になります。「S1299792さん」から、全体を表示しないと回答がしずらいことの指摘がありました。すみませんでした。その通りだと思いましたので、再度質問させて頂きました。 また、「watabe007さん」からの回答からコピー貼り付けの部分を教えて頂いたものも使って、改めてコードを書き換えて示します。  現在コピー貼り付け・ソート・J;列以外はコード通りに出来上がっています。それにJ列の欠・合も指示通りにできています。不だけがピンク色になりません。 なお、なぜか一カ所だけピンクになっているところがあります。条件の「条件 合計」・「条件 不合格」最初のEの不のところの条件だけは、ピンクになっています。  下記のが全体のコードです。 Sub 条件つきソート色つけ() Dim LastRow As Long, i As Long   Application.ScreenUpdating = False   Application.Calculation = xlCalculationManual   With Sheets("sheet1")     LastRow = .Cells(Rows.Count, "A").End(xlUp).Row     .Range("A1:J" & LastRow).Copy Sheets("sheet2").Range("A1")   End With   Application.CutCopyMode = False   Sheets("Sheet2").Select   Range("A1:J" & LastRow).Sort Key1:=Range("H1"), _     Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, _     MatchCase:=False, Orientation:=xlTopToBottom, _     SortMethod:=xlPinYin, DataOption1:=xlSortNormal   Range("E2:J" & LastRow).Interior.ColorIndex = 0 '文言の詳細について '部品名と詳細---------------------------------------略称            'ghyu--------------------------------------←E列   'klub---------------------------------------←F列  'llpo----------------------------------------←G列  '合計個数(合計)-------------------------←H列  合計   '数量順位---------------------------------←I列   順位 '合格・不合格(合・不)欠品(欠)-----←J列  合・不・欠 For i = 2 To LastRow If Cells(i, "E").Value = "" Then Cells(i, "E").Resize(, 6).Value = "欠" 'E列 ElseIf Application.CountIf(Cells(i, "E").Resize(, 6), "欠") > 0 Then Cells(i, "J").Value = "欠" ElseIf Cells(i, "H") >= 1 And Cells(i, "H") <= 49 Then '条件 合計 Cells(i, "J") = "不" ElseIf (Cells(i, "E") >= 20 And Cells(i, "F") >= 6) And Cells(i, "G") >= 10 Then '条件 これ以上は合格 Cells(i, "J") = "合" ElseIf (Cells(i, "E") = 0 Or Cells(i, "F") = 0) Or Cells(i, "G") = 0 Then '条件 全て0で不合格 Cells(i, "J") = "不" ElseIf (Cells(i, "E") <= 19) Then '条件  不合格 Cells(i, "J") = "不" ElseIf (Cells(i, "F") <= 5) Then '条件  不合格 Cells(i, "J") = "不" ElseIf (Cells(i, "F") <= 10) Then '条件  不合格 Cells(i, "J") = "不" ElseIf (Cells(i, "E") >= 20 And Cells(i, "F") >= 6) And Cells(i, "G") <= 9 Then '条件 E=○ F=○ G=×  不合格 Cells(i, "J") = "不" ElseIf (Cells(i, "E") >= 20 And Cells(i, "F") <= 5) And Cells(i, "G") <= 9 Then '条件 E=○ F=× G=×  不合格 Cells(i, "J") = "不" ElseIf (Cells(i, "E") <= 19 And Cells(i, "F") >= 6) And Cells(i, "G") <= 9 Then '条件 E=× F=○ G=×  不合格 Cells(i, "J") = "不" ElseIf (Cells(i, "E") <= 19 And Cells(i, "F") <= 5) And Cells(i, "G") >= 10 Then '条件 E=× F=× G=○  不合格 Cells(i, "J") = "不" End If If Cells(i, "E") >= 1 And Cells(i, "E") < 20 Then Cells(i, "E").Interior.ColorIndex = 6 ' 6は  黄色 End If If Cells(i, "F") >= 1 And Cells(i, "F") < 6 Then Cells(i, "F").Interior.ColorIndex = 6 ' 6は  黄色  End If If Cells(i, "F") >= 6 And Cells(i, "F") < 10 Then Cells(i, "F").Interior.ColorIndex = 34 '34は  淡い青色 End If If Cells(i, "G") >= 1 And Cells(i, "G") < 10 Then Cells(i, "G").Interior.ColorIndex = 6 ' 6は  黄色  End If If Cells(i, "H") >= 1 And Cells(i, "H") <= 49 Then Cells(i, "H").Interior.ColorIndex = 4 ' 4は  うぐいす色  End If If Cells(i, "J") >= "不" Then Cells(i, "J").Interior.ColorIndex = 38 '38は ローズ    If Cells(i, "J") >= "合" Then Cells(i, "J").Interior.ColorIndex = 2 ' 2は  白色  ElseIf Cells(i, "J") = "欠" Then Cells(i, "J").Interior.ColorIndex = 45 '45は  薄いオレンジ色 End If For j = 5 To 9 'D-F If Cells(i, j).Value = 0 Then Cells(i, j).Interior.ColorIndex = 3 '3は    赤色        ElseIf Cells(i, j).Value = "欠" Then Cells(i, j).Interior.ColorIndex = 45 '45は  薄いオレンジ色     End If Next j For k = 5 To 9 'G-I If Cells(i, j).Value = "欠" Then Cells(i, j).Interior.ColorIndex = 45 '45は  薄いオレンジ色   End If Next k Next i Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub

  • このマクロ、どこがおかしいですか?

    i5とj5のセルの文字が同じ場合はセル同士を結合して一つ下の行へ行き、i5とj5のセルに何も書かれていないときはそのまま一つ下の列へ行き、行った先のセルでも同じように処理(i6とj6のセルの文字が同じ場合はセル同士を結合して一つ下の行へ行き、i6とj6のセルに何も書かれていないときはそのまま一つ下の列へ行き)を繰り返し、と言うことをi33とj33のセルまで続けたいと思っています。 Sub よろしくお願いします() Dim i As Integer For i = 5 To 33 Cells(i, 9).Select If Cells(i, 9).Value = Cells(i, 10).Value Then Range(Cells(i, 9), Cells(i, 10)).Merge Selection.Offset(i + 1).Select ElseIf Cells(i, 9).Value = "" Then Selection.Offset(i + 1).Select Next i End If End Sub と書いたのですが、『Nextに対応するForがありません』と言われてしまいます。どうすれば思い通りにできるでしょうか? 極めて初心者で、伝わりにくい点があるかもしれません。よろしくお願いします。

  • 行ごとに判定するマクロについて教えて下さい

    行ごとに判定するマクロについて教えて下さい。 下記のようなマクロで、添付ファイルのように、行ごとで E列からN列で違った数値がないか、入力されていないセルがないかを調べ 4つすべてのセルが同じ数値でない場合は塗りつぶしはされず O列にOKを表示しないようなマクロを組みたいのですが 現在のマクロだと、行ごとではなく、E3~N102セルまでの中で 同じ数値がないかを判断してしまっているため K11セルやK15セルのように数値が入力されていないにも関わらずO列の部分にOKが出てしまいます。 他の行に同じ数値が入っているのは関係なしにして 11行目なら11行目だけで 15行目なら15行目だけで、というように行ごに判定していくには どのようにすればいいでしょうか? Sub 判定マクロ回転() Dim i As Integer, j As Integer Range(Cells(3, 15), Cells(102, 15)).ClearContents For i = 3 To 102 For j = 5 To 14 Cells(i, j).Interior.ColorIndex = 0 If WorksheetFunction.CountIf(Range("E3:N102"), Cells(i, j)) > 3 Then If Cells(i, j).Row Mod 2 = 1 Then Cells(i, j).Interior.ColorIndex = 6 Cells(i, 15) = "OK" Else If Cells(i, j).Row Mod 2 = 0 Then Cells(i, j).Interior.ColorIndex = 40 Cells(i, 15) = "OK" End If End If End If Next j Next i If WorksheetFunction.CountIf(Range("O3:O102"), "OK") > 99 Then MsgBox "データチェックOK(^O^)b" End If End Sub

  • 判定してセルを塗りつぶすマクロについて

    判定してセルを塗りつぶすマクロについて教えて下さい。 現在下記のようなマクロがあります。 Sub オニオン判定() Dim i As Integer, j As Integer, r As Integer Dim k As Double Range(Cells(17, 9), Cells(26, 14)).Interior.ColorIndex = 0 Range(Cells(30, 9), Cells(39, 14)).Interior.ColorIndex = 0 k = Cells(5, 2) 'B5セルの値 For j = 9 To 14 For i = 17 To 26 For r = 30 To 39 If Abs(Cells(i, j).Value - Cells(r, j).Value) <= 0.05 Then Cells(r, j).Interior.Color = vbYellow Cells(i, j).Interior.Color = vbYellow End If Next  Next   Next End Sub 対象のIf Abs(Cells(i, j).Value - Cells(r, j).Value) <= 0.05 Then で、それぞれ見比べて、0.05以上のずれがあるとセルが塗りつぶされないというマクロなのですが これを、If Abs(Cells(i, j).Value - Cells(r, j).Value) = 0 Thenという条件も追加して その時はセルを青に塗りつぶし、逆に0.05以上のずれがあるセルは赤に塗りつぶす。 みたいなマクロを書きたいです。 If Abs(Cells(i, j).Value - Cells(r, j).Value) = 0 Thenは一度追加してみましたが 上手く機能しませんでした。 やりたい事 ・数値が動いているけど、0.05以内の時は黄色 ・数値変動が0の場合は青 ・数値変動が0.05以上の場合は赤 です。 宜しくお願いします。

  • エクセルマクロの質問です

    こんばんわ! 同様のデータの列を検索したいのですが、良い方法が思いつきません。 色々考えてできたのが下記の方法ですが、項目が増えると大変なので、簡易化したいです。 今回簡易化したいのは↓ Sub test() Dim test(1 To 10) As Integer Dim stok_date As String For i = 1 To 5 Step 1 stok_date = Cells(1, i) For j = 1 To 10 Step 1 If stok_date = Cells(3, j) Then If test(1) = Empty Then test(1) = j ElseIf test(2) = Empty Then test(2) = j ElseIf test(3) = Empty Then test(3) = j ElseIf test(4) = Empty Then test(4) = j ElseIf test(5) = Empty Then test(5) = j End If Exit For End If Next j Next i End Sub 一行目○列目のデータが三行目の何列目にあるのかをそれぞれtest(○)に入れたい。 といった具合です。 アドバイスの程よろしくお願いいたします。

  • マクロ 色が思うように、表示できない

     下記のようなコードで部品の管理をしています。条件が多くて少し複雑になっています。 とりあえずは、うまくできました。J列の結果だけが、うまくできません。 但し、J列の結果が(38はローズ )の表示がうまくいきません。(6の黄色)になってしまいます。 要するに、J列の結果が不がローズ色で、合が白色で、欠が薄いオレンジ色になればよいということなのです。 原因が分からず困ってしまって、お聞きする次第です。回答して頂けるものでしようか。 ご教授下されば幸いに存じます。よろしくお願いします。  Macro2 Macro マクロ記録日 : ' Sheets("sheet1").Select Columns("A:J").Select Selection.Copy Sheets("sheet2").Select Columns("A:J").Select ActiveSheet.Paste Application.CutCopyMode = False Selection.Sort Key1:=Range("H2"), Order1:=xlAscending, Header:=xlGuess, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod _ :=xlPinYin, DataOption1:=xlSortNormal Dim i As Long, LastRow As Long LastRow = Cells(Rows.Count, "A").End(xlUp).Row Range("J2:J" & LastRow).ClearContents '← E2:Jにすると、欠に全部なります、この設定もおかしいように思いますが? Range("E2:J" & LastRow).Interior.ColorIndex = 0 Application.ScreenUpdating = False Application.Calculation = xlCalculationManual '文言の詳細について '部品名と詳細-------------------------------------略称            'ghyu--------------------------------------←E列   'klub---------------------------------------←F列  'llpo----------------------------------------←G列  '合計個数(合計)-------------------------←H列  合計   '数量順位---------------------------------←I列   順位 '合格・不合格(合・不)欠品(欠)-----←J列  合・不・欠 If Cells(i, "E") >= 1 And Cells(i, "E") < 20 Then Cells(i, "E").Interior.ColorIndex = 6 ' 6は  黄色 End If If Cells(i, "F") >= 1 And Cells(i, "F") < 6 Then Cells(i, "F").Interior.ColorIndex = 6 ' 6は  黄色  End If If Cells(i, "F") >= 6 And Cells(i, "F") < 10 Then Cells(i, "F").Interior.ColorIndex = 34 '34は  淡い青色 End If If Cells(i, "G") >= 1 And Cells(i, "G") < 10 Then Cells(i, "G").Interior.ColorIndex = 6 ' 6は  黄色  End If If Cells(i, "H") >= 1 And Cells(i, "H") <= 49 Then Cells(i, "H").Interior.ColorIndex = 4 ' 4は  うぐいす色  End If If Cells(i, "J") >= "不" Then Cells(i, "J").Interior.ColorIndex = 38 '38は ローズ    End If If Cells(i, "J") >= "合" Then Cells(i, "J").Interior.ColorIndex = 2 ' 2は  白色  End If For j = 5 To 9 'D-F If Cells(i, j).Value = 0 Then Cells(i, j).Interior.ColorIndex = 3 '3は    赤色        ElseIf Cells(i, j).Value = "欠" Then Cells(i, j).Interior.ColorIndex = 45 '45は  薄いオレンジ色     End If Next j For k = 5 To 9 'G-I If Cells(i, j).Value = "欠" Then Cells(i, j).Interior.ColorIndex = 45 '45は  薄いオレンジ色   End If Next k Next i Application.Calculation = xlCalculationAutomatic Application.ScreenUpdating = True End Sub

  • エクセルのマクロで教えてください

    いつもお世話になっています。 先日、こちらのサイトでマクロをコピーさせて頂いて使っていました。 以下のようなマクロです ub test01() Dim sh1 As Worksheet Dim sh2 As Worksheet Set sh1 = Worksheets("統合表") j = 2 '統合表のデータ書きこみ開始行 For Each sh2 In ActiveWorkbook.Worksheets If sh2.Name = "統合表" Then Exit For '統合表は統合対象外 d = sh2.Range("A65536").End(xlUp).Row '各シートの最終行を知る ' MsgBox d For i = 63 To d '各シートの開始行2から最終行まで統合表に移す sh1.Cells(j, "C") = sh2.Cells(i, "C") sh1.Cells(j, "Q") = sh2.Cells(i, "Q") sh1.Cells(j, "T") = sh2.Cells(i, "T") sh1.Cells(j, "AM") = sh2.Cells(i, "AM") sh1.Cells(j, "AR") = sh2.Cells(i, "AR") sh1.Cells(j, "AU") = sh2.Cells(i, "AU") sh1.Cells(j, "BB") = sh2.Cells(i, "BB") sh1.Cells(j, "BI") = sh2.Cells(i, "BI") '列数だけこの後にコードを増やすこと j = j + 1 '統合表の直下行をポイント Next i Next End Sub このマクロでは2行目から以降を呼んでくることになっていますが、これを2行目から60行目までだけ呼んでくるようにしたいのです。 どうしたら良いか教えていただけないでしょうか? つたない文章ですみません。 よろしくお願いします。

  • エクセル2003 VBAマクロにて 背景色 白色の抽出

    エクセル2003のマクロでセル背景色にて抽出したいのですが 背景色が白色(空白)の抽出ができません。 背景色別に 他セルに文字を自動記入したいのですが、 背景色が白(collorindex=0)の認識をしてくれません。 カラーインデックスでは、白は「0」か「2」になっているので その値でマクロを組んでも認識してくれないようです。 どのようにすればよいのでしょうか? 以下に私(素人)のマクロ文(一部)です。ご指摘お願い致します。 Dim 行番号 As Integer 行番号 = 7 Do Until Cells(行番号, 1).Value = "" If Cells(行番号, 9).Interior.ColorIndex = 5 Then Cells(行番号, 14).Value = "3号機"   ElseIf Cells(行番号, 9).Interior.ColorIndex = 7 Then Cells(行番号, 14).Value = "4号機" ElseIf Cells(行番号, 9).Interior.ColorIndex = 0 Then Cells(行番号, 14).Value = "未加工" End If 行番号 = 行番号 + 1 Loop

  • VBA 実行時エラー1004 について

    いつもお世話になります。 作表をしていて、項目に色をつけたいのですが VBA 実行時エラー1004 Rangeメソッドは失敗しました。Globalオブジェクト というエラーが If Range(Cells(5, n - 4)).Interior.Color = RGB(252, 213, 180) Then のところででます。 If Range("Z5").Interior.Color = RGB(252, 213, 180) Then とすると、実行できます。 Sub カラー() Dim n As Long '列番号取得 '最終列取得 n = Cells(5, Columns.Count).End(xlToLeft).Column MsgBox "最終列は" & n   '= 今回は30です。 'セルの色を変える If Range(Cells(5, n - 4)).Interior.Color = RGB(252, 213, 180) Then Range(Cells(3, n - 3), Cells(5, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(39, n - 3), Cells(41, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(68, n - 3), Cells(70, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(104, n - 3), Cells(106, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(133, n - 3), Cells(135, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(169, n - 3), Cells(171, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(198, n - 3), Cells(200, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(234, n - 3), Cells(236, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(263, n - 3), Cells(265, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(299, n - 3), Cells(301, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(329, n - 3), Cells(331, n)).Interior.Color = RGB(230, 184, 183) Range(Cells(365, n - 3), Cells(367, n)).Interior.Color = RGB(230, 184, 183) Else Range(Cells(3, n - 3), Cells(5, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(39, n - 3), Cells(41, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(68, n - 3), Cells(70, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(104, n - 3), Cells(106, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(133, n - 3), Cells(135, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(169, n - 3), Cells(171, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(198, n - 3), Cells(200, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(234, n - 3), Cells(236, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(263, n - 3), Cells(265, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(299, n - 3), Cells(301, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(329, n - 3), Cells(331, n)).Interior.Color = RGB(252, 213, 180) Range(Cells(365, n - 3), Cells(367, n)).Interior.Color = RGB(252, 213, 180) End If End Sub どこが間違っているのか教えていただけないでしょうか? あと、スマートなコードの書き方もお願いします。