• ベストアンサー

エクセルマクロで数値の一致ができない

マクロで以下のコードを作成しましたが、うまくいきません。 If Left(Cells(3, 2).Value, 5) = Cells(3, 6).Value Then ……………………… End if Cells(3, 2),Cells(3, 6)の値が数値だと見かけでは一致していますが、 一致していないとされています。 ■うまくいかない Cells(3, 2)…123456789 Cells(3, 6)…12345 Cells(3, 2),Cells(3, 6)を文字に変えると問題なく動作します。 ■うまくいく Cells(3, 2)…abcdefgh Cells(3, 6)…abcde Cells(3, 2),Cells(3, 6)を数値でもうまくいくにはどうしたらいいのでしょうか。 よろしくお願いします。

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

  • ベストアンサー
  • hallo-2007
  • ベストアンサー率41% (888/2115)
回答No.1

Cells(3, 6).Value は数値のままですね。 以下の様にしてみては如何でしょうか Sub ボタン1_Click() If Left(Cells(3, 2).Text, 5) = Cells(3, 6).Text Then MsgBox ("同じです") Else MsgBox ("違います") End If End Sub

smorgas030
質問者

お礼

回答ありがとうございます。 Value を Text に変えるのですね。 早速やってみたらできました。 ありがとうございました。 ただ、処理速度が遅くなったようですが、Textに変えたせいでしょうか。 別にかまわないのですが。 ありがとうございました。

その他の回答 (1)

  • xls88
  • ベストアンサー率56% (669/1189)
回答No.2

Val関数を使えばどうでしょうか。 If val(Left(Cells(3, 2).Value, 5)) = Cells(3, 6).Value Then End If

smorgas030
質問者

お礼

回答ありがとうございます。 Val関数というのがあるのですね。 早速やってみたらできました。 ありがとうございました。

関連するQ&A

  • マクロ A1のセルの値を見て、B1に値を入力したい。

    エクセルのマクロでA1の値が1ならばB1にaを、2・3・4ならばbを、5ならばCを、それ以外は「該当無し」と入れたいのですが下のマクロではうまく行きません。ぜひご指導ください。 Sub If Left(Cells(1, 1).Value, 1) = 1 Then Cells(2,1).Text = "a" ElseIf Left(Cells(1,1).Value, 1) = 2 Or _ Left(Cells(1,1).Value, 1) = 3 Or _ Left(Cells(1,1).Value, 1) = 4 Then Cells(2,1).Text = "b" ElseIf Left(Cells(z, 37).Value, 1) = 5 Then Cells(2,1).Text = "c" Else: Cells(z, 40).Text = "該当無し" End If End Sub

  • エクセル マクロ

    よろしくお願いします。 エクセルのテキストに従って勉強していて コード抜けがないことも確認したのですが 「ifに対するend ifがありません」と表示されます。 デバックを開いてもブレークポイントが表示されていないので よくわかりません。 どこが問題かご享受ください。 Private Sub CommandOK_Click() Dim Row As Integer Row = Range("D1").Value + 3 If 会員登録画面.氏名カナ.Value = Empty Then MsgBox ("氏名カナが空欄です") Exit Sub End If If 会員登録画面.氏名漢字.Value = Empty Then MsgBox ("氏名漢字が空欄です") Exit Sub If Not IsDate(会員登録画面.年.Value & _ "/" & 会員登録画面.月.Value & _ "/" & 会員登録画面.日.Value) Then MsgBox ("生年月日の形式が正しくありません") Exit Sub End If Cells(Row, 1).Value = 会員登録画面.会員番号.Value Cells(Row, 2).Value = 会員登録画面.氏名カナ.Value Cells(Row, 3).Value = 会員登録画面.氏名漢字.Value If 会員登録画面.男.Value = True Then Cells(Row, 4).Value = "男" Else Cells(Row, 4).Value = "女" End If Cells(Row, 5).Value = DateValue(会員登録画面.年.Value & _ "/" & 会員登録画面.月.Value & _ "/" & 会員登録画面.日.Value) Cells(Row, 6).Value = 会員登録画面.都道府県.Value Cells(Row, 7).Value = 会員登録画面.電話番号.Value If 会員登録画面.スポーツ観戦.Value = True Then Cells(Row, 8).Value = "○" End If If 会員登録画面.映画鑑賞.Value = True Then Cells(Row, 9).Value = "○" End If If 会員登録画面.読書.Value = True Then Cells(Row, 10).Value = "○" End If If 会員登録画面.釣り.Value = True Then Cells(Row, 11).Value = "○" End If If 会員登録画面.ドライブ.Value = True Then Cells(Row, 12).Value = "○" End If If 会員登録画面.旅行.Value = True Then Cells(Row, 13).Value = "○" End If Range("D1").Value = Range("D1").Value + 1 Call 画面初期化 End Sub

  • エクセルのマクロについて

    Sub ア_Click() Dim i As Long With ActiveSheet For i = 5 To .Cells(Rows.count, "C").End(xlUp).Row If InStr(.Cells(i, "C"), "ア") = 0 Then If InStr(.Cells(i, "D"), "ア(半角)") = 0 and .Cells(i,"D")<>"" Then MsgBox i & "行目" Cells(i, 2).Offset(0, -1).Value = "★" End If End If Next i End With End Sub このエクセルマクロはC列に「ア」があって、D列に「ア(半角)」の文字がない場合はメッセージというマクロなのですが、このマクロをア~ンまで同じ作業をしたいのですが、ひとまとめにマクロを組む事はできるのでしょうか?できる場合どのようにすれば良いでしょうか? 下記のようにして見たのですができませんでした。 Sub ア_Click() Dim i As Long With ActiveSheet For i = 5 To .Cells(Rows.count, "C").End(xlUp).Row If InStr(.Cells(i, "C"), "[ア-ン]") = 0 Then If InStr(.Cells(i, "D"), "[ア-ン](半角)") = 0 and .Cells(i,"D")<>"" Then MsgBox i & "行目" Cells(i, 2).Offset(0, -1).Value = "★" End If End If Next i End With End Sub

  • エクセル2003のマクロが2010で使えない

    PC買い換えで、今まで使えていたマクロに下記のようなメッセージが表示されて 使えなくなりました。他人が作成したマクロでまた、私はVBAに詳しくありません。 !はこのマシンで利用できないため、オブジェクトをこのマシンで読み込めませんでした。 コンパイルエラー 変数が定義されていません。 以下記述の一部です。 Private Sub UserForm_Initialize()                    ←ここが黄色に Dim c As Control, i As Integer, j As Integer With data i = 1 Do Until .Cells(i + 1, 1).Value = "" i = i + 1 list01.AddItem .Cells(i, 2).Value For j = 1 To 6 list01.List(i - 2, j) = .Cells(i, j + 2).Value Next j list01.List(i - 2, 7) = .Cells(i, 1).Value Loop i = 1 Do Until .Cells(i + 1, 29).Value = "" i = i + 1 comb02.AddItem .Cells(i, 29).Value comb02.List(i - 2, 1) = .Cells(i, 30).Value comb02.List(i - 2, 2) = .Cells(i, 31).Value comb02.List(i - 2, 3) = .Cells(i, 32).Value comb02.List(i - 2, 4) = .Cells(i, 33).Value comb02.List(i - 2, 5) = Mid(.Cells(i, 29).Value, Len(.Cells(i, 29).Value) - 4, 2) comb02.List(i - 2, 6) = Right(.Cells(i, 29).Value, 2) Loop i = 1 Do Until .Cells(i + 1, 37).Value = "" i = i + 1 comb01.AddItem .Cells(i, 37).Value Loop cal01.Value = .Cells(2, 23).Value                   ← cal01が青く ymdStart = .Cells(2, 26).Value ymdEnd = .Cells(3, 26).Value Controls("opt0" & .Cells(3, 23)).Value = True chk01.Value = .Cells(4, 23).Value For Each c In Controls If Left(c.Name, 4) = "list" Or Left(c.Name, 4) = "text" Or Left(c.Name, 4) = "comb" Then c.ForeColor = .Cells(13, 25).Value c.BackColor = .Cells(16, 25).Value End If Next c End With With list01 If .ListCount = 0 Then If MsgBox("職員が登録されていません。", 48, ThisWorkbook.Name) = 1 Then End If Else ReDim GroupTable(.ListCount - 1, 1) i = 0 For j = 0 To .ListCount - 1 If .List(i, 0) <> .List(j, 0) Then i = j End If GroupTable(j, 0) = i Next j i = .ListCount - 1 For j = .ListCount - 1 To 0 Step -1 If .List(i, 0) <> .List(j, 0) Then i = j End If GroupTable(j, 1) = i Next j End If End With but07.ControlTipText = ThisWorkbook.Name & "の上書き保存" MsgMode = True Call cal01_Click Call opt04to05_Change End Sub どうしていいかわかりませんので、よろしくお願いします。 Windows7 Professional SP1 64

  • EXCELのマクロで数値比較

    「数量」「単価」「金額」のEXCELの表があり「金額」欄が「入力」であったり「計算式」であったりするため「金額」の不整合をチェックするマクロを作りました。(コードの一部は下記) For cntR = 5 To MaxRow If Cells(cntR, 8).Value <> _ Application.RoundDown(Cells(cntR, 6).Value * Cells(cntR, 7).Value, 0) Then Cells(cntR, 23) = "計算違い" End If Next 計算違いとは思えないのに”計算違い”となってしまいます。 テストした表の内容 数量:200 単価:20、20.1、20.3・・・・・20.9、21 金額:=数量*単価の計算式が入っている。 マクロ実行後”計算違い”となってしまうケース。 単価:20.1と20.4の時 ※金額に手入力してみる:”計算違い”になりません。 ※金額欄の計算式を変えてみる=Rounddown(数量*単価,0):”計算違い”になりません。 どこに問題があるのか特定できずにいます。 EXCEL2007・2000の両バージョン下でマクロに手を加えてます。

  • ExcelのマクロをWSHから実行させると「型が一致しません」のエラーがでます。

    WSHからExcel2000のマクロを実行させています。このマクロはセルの計算式の結果(セル(1,2))をif関数で比較するコードが入っています。 If Cells(1, 2).Value = "8月" Then hensu = 1 End If これをWSHから実行させると「型が一致しません」とエラーが出ます。 Debug.Print TypeName(Cells(1,2).Value)で型を表示させてみるとマクロを直接実行させたときはStringですが、WSHから実行させるとErrorとなっています。 なぜWSHから実行すると型がErrorとなるのかということと、解決策を教えていただければ幸いです。 ちなみにWSHでマクロを実行させている部分は以下のとおりです。 Set objExcelApp = CreateObject("Excel.Application") Set objWbk = objExcelApp.Workbooks.Open("ExcelBook.xls",True) objExcelApp.Run("ExcelBook.xls!macro1")

  • エクセルマクロで「1」を「01」に変換したい

    エクセルマクロで、セルに入力された数値を、 変換するマクロを以下のように作成したのですが、 もっと簡単な方法はないのでしょうか。 やりたいことは、 「1」で入力された値を「01」に変換することです。 よろしくお願いします。 c = Sheets("Sheet1").Cells(1, 1).Value If c = 1 Then c = "01" ElseIf c = 2 Then c = "02" ElseIf b = 3 Then c = "03" ElseIf c = 4 Then c = "04" ElseIf c = 5 Then c = "05" ElseIf c = 6 Then c = "06" ElseIf c = 7 Then c = "07" ElseIf c = 8 Then c = "08" ElseIf c = 9 Then c = "09" End If

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

    判定してセルを塗りつぶすマクロについて教えて下さい。 現在下記のようなマクロがあります。 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以上の場合は赤 です。 宜しくお願いします。

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

    エクセル2003でのマクロで質問です。 セル(2,22)には"1"という値が入っています。 セル(2,13)には"20100521"という値が入っています。 セル(3,13)には"20100521"という値が入っています。 セル(4.13)には"20100525"という値が入っています。 セル(5,13)には"20100525"という値が入っています。 このようなときに セル(3,22)の値は"1" セル(4,22)の値は"2" セル(5,22)の値は"2" となるように以下のようなマクロを作成しました。 Dim i As Integer, j As Integer, date1 As String i = 2 j = 3 Do While Cells(i, 1).Value <> "" If Cells(i, 13) = Cells(j, 13) Then Cells(j, 22) = Cells(i, 22) Else Cells(j, 22) = Cells(i, 22) + 1 End If i = i + 1 j = j + 1 Loop しかし、結果は セル(3,22)の値は"2" セル(4,22)の値は"3" セル(5,22)の値は"4" となってしまいます。 どこに原因があるのかわかりません。 助けてください。 宜しくお願いします。

  • セルの値が0はクリアするマクロ

    エクセル2003です。 ある集計表において 4行目のH列からAM列まで 数値データがあります。 最終行は常に変化します この表内にてセルの値が0のセルは セル内を空白にしたいです。 以下のマクロを作成しましたが If Cells(処理行, 8).Value = 0 Then Cells(処理行, 8).ClearContents をあと(処理行, 13)から(処理行,31) まで記述しなければなりません。 構文的にも処理的にも不利? と思うので、なにかいい方法を教えてください。 Sub 数字0クリア() '2012年2月3日節分 Dim 最終行 '最終列をG列で求めます 最終行 = Cells(Rows.Count, 7).End(xlUp).Row Application.ScreenUpdating = False For 処理行 = 4 To 最終行 If Cells(処理行, 8).Value = 0 Then Cells(処理行, 8).ClearContents End If If Cells(処理行, 9).Value = 0 Then Cells(処理行, 9).ClearContents End If If Cells(処理行, 10).Value = 0 Then Cells(処理行, 10).ClearContents End If If Cells(処理行, 11).Value = 0 Then Cells(処理行, 11).ClearContents End If If Cells(処理行, 12).Value = 0 Then Cells(処理行, 12).ClearContents End If If Cells(処理行, 13).Value = 0 Then Cells(処理行, 13).ClearContents End If Next 処理行 Application.ScreenUpdating = True MsgBox "終了しました" End Sub

専門家に質問してみよう