- ベストアンサー
可変する範囲の合計を出したい(マクロ)
下記のような表があります。 A … C D … G NO. 順位 社名 金額 1 1 A 800 2 2 B 700 3 3 C 600 4 3 D 600 5 4 E 500 : : : : : : : : 253 120 M 100 254 120 W 100 合計欄 (100位までの合計金額が入る) 254社まであり、1位から順に総金額を基準に順位がふってあります。 総金額が同じ会社は順位も同じになります。 なので、たとえば100位が10社ある場合もあります。 また、必ずしも100位までとは限りません。順位とNO.が連動している関係から、85位の次が112位という場合もあります。 このような表で、1位から100位以下の会社の合計金額をマクロで計算するにはどうすればよいのでしょうか? 順位は都度変わるので、合計する範囲も常に変わります。 ************************************************* Dim i As Integer For i = 7 To 254 Cells(i, "C").Select If Cells(i, "C") >= 101 then 'もし101以上だったら Cells(i, "C").Offset(1, 0).Select '一行下へ移動する※ ElseIf Cells(i, "C") <= 100 then 'もし100以下だったら End If Next End Sub ************************************************* ここまで書いて、次の作業に悩んでいます。 Elself~のあとに、 ActiveCell.Interior.ColorIndex = 3 ActiveCell.Offset(0, 4).Select ActiveCell.Interior.ColorIndex = 5 と入れると、C列とG列の100位以下の合計したい範囲に色がつきました。 これを利用して範囲指定すればいいのかな?と思いましたが、どうもうまくいきません。 都度変わる範囲を指定してSUM関数と組み合わせるにはどうすればよいのでしょうか?
- みんなの回答 (3)
- 専門家の回答
質問者が選んだベストアンサー
その他の回答 (2)
- Wendy02
- ベストアンサー率57% (3570/6232)
関連するQ&A
- マクロ:セルの範囲指定
エクセルマクロで困っています。 セルの範囲指定をしようとしています。 初心者過ぎて、よくわかりません。 現在のマクロ↓ Sub 済() If ActiveCell.Column = 21 Then Selection.FormatConditions.Delete '条件付き書式削除 With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With ActiveCell.Offset(0, 1).Select With Selection.Interior .ColorIndex = 16 .Pattern = xlSolid End With '色変え判定セル書き換え ActiveCell.Offset(0, 5).Select ActiveCell.FormulaR1C1 = "77" ActiveCell.Offset(0, -5).Select Else answer = MsgBox("U列を選択して下さい", vbCritical) End If End Sub やりたい事は、下記の通りです。 列Uがアクティブの時にU~ACの行を塗りつぶし。 列は変動します。 今は、やり方がよく分からなかったため オフセットで一つ一つ塗りつぶしてます。 マクロを組みすぎてファイルが重くなって困っています。 回答よろしくお願いいたします。
- ベストアンサー
- オフィス系ソフト
- マクロ 色が思うように、表示できない
下記のようなコードで部品の管理をしています。条件が多くて少し複雑になっています。 とりあえずは、うまくできました。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
- ベストアンサー
- Visual Basic
- Do Loop Until 条件停止後のセル位置について
こんにちは。いつもお世話になります。 ただ今、シート上の緑色のセルをカーソルで移動させるプログラムを 作っています。 停止の条件は[SHIFT]キーを押すと止まります。 一応は停止しますがセルの位置がズレてしまい、なんとか現在選択 している位置で停止できないものかと思い、アドバイス願います。 コードは下記になります。 Declare Function GetAsyncKeyState Lib "User32.dll" (ByVal vkey As Long) As Long Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Sub Key_Sample() Cells(1, 1).Select On Error Resume Next '繰返し開始 Do '上方向のキー入力判定 If GetAsyncKeyState(38) <> 0 Then Selection.Interior.ColorIndex = xlNone ActiveCell.Offset(-1, 0).Select Selection.Interior.ColorIndex = 4 Else Selection.Interior.ColorIndex = 4 End If '下方向のキー入力判定 If GetAsyncKeyState(40) <> 0 Then Selection.Interior.ColorIndex = xlNone ActiveCell.Offset(1, 0).Select Selection.Interior.ColorIndex = 4 Else Selection.Interior.ColorIndex = 4 End If '左方向のキー入力判定 If GetAsyncKeyState(37) <> 0 Then Selection.Interior.ColorIndex = xlNone ActiveCell.Offset(0, -1).Select Selection.Interior.ColorIndex = 4 Else Selection.Interior.ColorIndex = 4 End If '右方向のキー入力判定 If GetAsyncKeyState(39) <> 0 Then Selection.Interior.ColorIndex = xlNone ActiveCell.Offset(0, 1).Select Selection.Interior.ColorIndex = 4 Else Selection.Interior.ColorIndex = 4 ActiveCell.Select End If Sleep 100 Loop Until GetAsyncKeyState(16) <> 0 End Sub
- 締切済み
- Visual Basic
- エクセルVBAで範囲を都度指定したい
教えてください。 指定した範囲から先頭がhのセルについて 色を付けていますが、範囲を固定ではなく マウスで都度選択させたいのですが どう直せば良いのでしょうか。 ActiveCell にすると最初のひとつしか変わりません。 Sub TEST() Dim rg As Range For Each rg In Range("D7", "D14") If Left(rg.Value, 1) <> "h" Then rg.Interior.ColorIndex = 23 rg.Offset(, 1).Interior.ColorIndex = 24 End If Next rg End Sub
- ベストアンサー
- オフィス系ソフト
- ExcelVBAで、選択範囲内で同じ値が入力されたセルを調べる
選択範囲内(縦一列)で同じ値が入力されたセルの色を黄色にするプログラムを作りました。 Sub 選択範囲内で同じ値が入力されたセルを調べる_縦() Dim startrow As Byte Dim lasrow As Byte Dim i As Long Dim j As Byte Dim atai If TypeName(Selection) <> "Range" Then Exit Sub startrow = ActiveCell.Row '最初のセルの列番号を取得 lasrow = Selection.Rows(Selection.Rows.Count).Row '最終列番号を取得 '同じ値が入力されているセルを黄色にする For i = startrow To lasrow - 1 If ActiveSheet.Cells(i, ActiveCell.Column).Interior.ColorIndex = xlNone Then atai = ActiveSheet.Cells(i, ActiveCell.Column).Value For j = i + 1 To lasrow If atai = ActiveSheet.Cells(j, ActiveCell.Column).Value Then ActiveSheet.Cells(i, ActiveCell.Column).Interior.ColorIndex = 6 ActiveSheet.Cells(j, ActiveCell.Column).Interior.ColorIndex = 6 End If Next End If Next End Sub 但し、上記のプログラムでは選択範囲内に結合セルがあるとエラーになってしまいます。 どなたか、解決方法をご教授頂けませんでしょうか? 宜しくお願い致しますm(._.)m
- ベストアンサー
- Visual Basic
- エクセル マクロ 複数セルの色付けについて
マクロ初心者で苦戦しており、教えていただけると大変ありがたいです。 エクセルの複数のシートに、問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
- 締切済み
- Visual Basic
- この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
- ベストアンサー
- その他(プログラミング・開発)
- 全体を表示 マクロ 色が思うように、表示できない
「マクロ 色が思うように、表示できない」で質問したことへの追加になります。「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
- ベストアンサー
- Visual Basic
- エクセルマクロ 範囲内のTOP行位置等を知りたいです。
エクセルマクロで以下の事がしたいのですが、行き詰ってしましました。 どなかたご教授願います。 事前に セルのA1:B3 に名前をつけておきます。 名前は"board"とします。 (1)選択したセルが"board"内に含まれるかどうかをマクロで判定したい。 (2)"board"範囲のセルに対して一つづつある処理をしたいのです。 (たとえば、色を変えるとか、データをSETするとか。) FOR文をふたつ組み合わせて 順番に処理しようとしているのですが、"board"のTOPの行 と 最終行、及び 同じくTOPの桁位置 と 最終桁 を取得するにはどうすればいいでしょうか? ROW1 = 7 ← この4つの変数を動的に取得したいのです。 ROW2 = 17 COL1 = 9 COL2 = 21 For I = ROW1 To ROW2 For K = COL1 To COL2 Cells(I, K).Select If Selection.Interior.ColorIndex = COLKUROX Then Selection.Interior.ColorIndex = COLKURO ElseIf Selection.Interior.ColorIndex = COLSIROX Then Selection.Interior.ColorIndex = COLSIRO ElseIf Selection.Interior.ColorIndex = COLWAKUX Then Selection.Interior.ColorIndex = COLWAKU End If Next Next (3)これはマクロとは関係ないのですが・・・ "board"の名前を設定するときに間違って違うセル範囲につけてしまいました。 一旦つけた名前を削除したいのですが、やり方がわかりません。 以上 3 点 についてお願いします。 (1)は(2)を解決できれば、IF 文を使ってできそうなのですが、 Intersect を使ってできないでしょうか? Set myRange = Application.Intersect(??????, Range("board")) ↑ ?????のところに指定したセルを書けば、このマクロの結果がエラーになるかならないかで判定できないかな・・・・と。
- ベストアンサー
- オフィス系ソフト
- エクセル マクロ VBA について
以下はセルB2.C2.D2.E2.F2をアクティブセルから右方向へ入力しています。ここでの入力とは"=" + "セルB2" というものです。一つずつ入力している為マクロが長くなります。短くシンプルなものにしたいです。ご教示お願いします。 ActiveCell.FormulaR1C1 = "=R2C2" ActiveCell.Offset(0, 1).Select ActiveCell.FormulaR1C1 = "=R2C3" ActiveCell.Offset(0, 1).Select ActiveCell.FormulaR1C1 = "=R2C4" ActiveCell.Offset(0, 1).Select ActiveCell.FormulaR1C1 = "=R2C5" ActiveCell.Offset(0, 1).Select ActiveCell.FormulaR1C1 = "=R2C6"
- ベストアンサー
- オフィス系ソフト
お礼
早速の回答ありがとうございます! SUMIF関数、全然頭から抜けてました~~。 Application.WorksheetFunction のあとに関数を書くのですね。 ありがとうございました!!