• 締切済み

エクセルのマクロ、VBAに関する質問

エクセルのマクロ、VBAに関する質問です。 下記コード3行目の"ここヘルプ!"のところを ”「選択範囲左上のセル Selection(1)」を「A1形式」で「行だけ絶対参照 xlAbsRowRelColumn」したものに10足した値” にしたいです。 例:選択範囲がA1:D5なら”A$1+10”   選択範囲がB4:H78なら”B$4+10” もうちょっとで出来る気がするので教えて下さい。 Sub 基準値+1o以上をハッチング() Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="ここヘルプ!" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 52479 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False End Sub

みんなの回答

回答No.1

合っているかどうか分かりませんが、回答します。 http://officetanaka.net/excel/vba/tips/tips111.htm http://www.nurs.or.jp/~ppoy/access/excel/xlA010.html Selection(1).Offset(10,0) これでできるかな・・?と思いますが、どうでしょうか。

関連するQ&A

  • EXCELでマクロの記録を繰り返して

    EXCELでマクロの記録を繰り返して重複した行は分かる範囲でまとめたのですが、もう少しまとめられないかと思っています。 withで、まとめられなくても、順番を入れ替えたらまとまるものとか、逆にまとめようが無いものとか教えて頂けたらと思います。 よろしくお願いします。 With Selection .Phonetics.Visible = False 'フリガナ表示をオフに .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = False End With With Selection .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(NOT(ISBLANK($A3)),ISBLANK($L3))" .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority End With With Selection.FormatConditions(1).Interior .Pattern = xlSolid .PatternColorIndex = 0 .Color = 65535 .TintAndShade = 0 .PatternTintAndShade = 0 End With With Selection .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(NOT(ISBLANK($A3)),NOT(ISBLANK($D3)))" .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .TintAndShade = 0 .PatternTintAndShade = 0 .Color = 5296274 End With With Selection .FormatConditions.Add Type:=xlExpression, Formula1:="=AND(NOT(ISBLANK($A3)),ISBLANK($R3))" .FormatConditions(Selection.FormatConditions.Count).SetFirstPriority End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent5 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False

  • Excelマクロの記述をもっとわかりやすくしたいのですが、どなたかご教

    Excelマクロの記述をもっとわかりやすくしたいのですが、どなたかご教示お願いします。 Excel2007使用。下記はマクロを記録として作成しました。省略できるものは省略して見やすくしたいのですが、どこを修正していいのか困っています。 範囲は「A2:A1000、B2:B1000」としていますが、「値が入っている範囲」としたい場合はどのように記述したらよいのでしょうか。 マクロ現在勉強中です。よろしくお願いいたします。 Sub CHK() Range("A2:A1000").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=COUNTIF($B$2:$B$1000,A2)>0" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.599963377788629 End With Selection.FormatConditions(1).StopIfTrue = False Range("B2:B1000").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=COUNTIF($A$2:$A$1000,B2)>0" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.599963377788629 End With Selection.FormatConditions(1).StopIfTrue = False End Sub

  • エクセルについてです。マウスで選択した範囲に、条件付き書式設定で80点

    エクセルについてです。マウスで選択した範囲に、条件付き書式設定で80点以上のセルの背景を赤にする、というマクロを作成したいと思います。選択する範囲は毎回異なります。以下のマクロはマクロの記録で作成したものです。このRangeの部分をマウスで選択した範囲に変更したいのですが、どうしたら良いのでしょうか?範囲の取得はマクロを実行して、途中で「マウスで範囲を指定してください」というメッセージを表示させてからにしたいのですが。 Sub 背景を赤() Range("B13:E19").Select Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _ Formula1:="=80" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Font .Color = -16383844 .TintAndShade = 0 End With With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 13551615 .TintAndShade = 0 End With End Sub

  • VBA 選択範囲の中で、更に一番上の行を指定したい!

    タイトルの通りです。 選択範囲に罫線を引くマクロを作成しました。 外に太い枠線、中は点線を引き、選択範囲の一番上の行を灰色の塗りつぶしにしたいのです。 しかし、罫線はうまくいきましたが、一番上の行の指定がうまくできません。 Sub 罫線をひく() ' With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlHairline End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlHairline End With ActiveWindow.SmallScroll Down:=12  ←ここを変えたいのですが、どうすればよいかわかりません。 With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorDark1 .TintAndShade = -0.499984740745262 .PatternTintAndShade = 0 End With End Sub どうすればよいか、もしご存知の方がいらっしゃいましたら、ご指導ください。 よろしくお願いたします。

  • 保護されているシートでマクロ実行するとエラー

    Excel2010で勤務表を作っています。 A列にとある文字列(承認)と入力すると、その行が保護されるマクロを、下記URLからコピペして使わせて頂いてます。 http://questionbox.jp.msn.com/qa3277541.html 勤務表なので、土日祝日は網掛けになるよう条件付き書式を使っています。 休暇取得した場合は網掛けを付けて、休日出勤した場合には網掛けなしにしたり出来るようマクロをマクロの自動記録で作りました。 ところが、どこかの行が保護されている状態で、セルの網掛けを変更するマクロを実行すると 「実行時エラー'1004'アプリケーション定義またはオブジェクトの定義エラーです」と出てしまいます。 保護されている行ではなく、まだ保護はされていない行に実行しています。 エラーが出ているのは網掛けマクロから出ています。 以下、今エクセルファイルにあるマクロの構文になります。 網掛けマクロは全部で4つ作りました。 1)休日出勤した際に条件付き書式をクリアして網掛けなしにするマクロです。 Sub 休日出勤() ' 条件書式クリア Selection.FormatConditions.Delete End Sub ※エラーになっている部分です。 2)平日休んだ日に網掛けをするマクロです。 Sub 休日() ' 網掛け With Selection.Interior .ColorIndex = 0 .Pattern = xlGray16 .PatternColorIndex = xlAutomatic End With End Sub 3)2)のマクロで休日にしたけど、やっぱり出勤したという時に、1)だと網掛けなしにならなかったので、網掛けなしにするマクロを作りました。 Sub網掛けなし() ' 網掛けなし With Selection.Interior .Pattern = xlNone .TintAndShade = 0 .PatternTintAndShade = 0 End With End Sub 4)ごちゃごちゃいじってしまって最初の状態に戻したいと思ったので条件付き書式を再設定するマクロを作りました。が、2)の休日マクロを実行したセルは元に戻らないので仕方なく3)の網掛けなしマクロを実行しなければなりません。 Sub 書式クリア() ' 条件書式再設定 Range("A6:K36").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=WEEKDAY($B6,2)>=6" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .Pattern = xlGray16 .PatternColorIndex = xlAutomatic .ColorIndex = xlAutomatic End With Selection.FormatConditions(1).StopIfTrue = False Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=OR(WEEKDAY($B6)=1,COUNTIF(祝日,$B6))" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .Pattern = xlGray16 .PatternColorIndex = xlAutomatic .ColorIndex = xlAutomatic End With Selection.FormatConditions(1).StopIfTrue = False End Sub ※この中のSelection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=WEEKDAY($B6,2)>=6"の部分がエラーと出ています。 ※1)から3)は網掛けしたいところ、網掛けなしにしたいところを範囲選択してから実行しなければなりません。 その他、保護を解除する際にパスワード認証が欲しかったので、「保護解除」ボタンを押すためにパスワード認証させるマクロもあります。 これは特に問題なく動いています。 5)パスワード認証つき保護解除マクロ Sub password() Dim pw As Long pw = Application.InputBox( _ prompt:="パスワード入力", Type:=1) If pw <> "123" Then MsgBox "パスワードが違います" Exit Sub Else MsgBox "保護解除しました" ActiveSheet.Unprotect End If End Sub 6)行ごとに保護するマクロです。 Private Sub Worksheet_Change(ByVal Target As Range) Dim r, rng As Range Set rng = Intersect(Target, Columns(1)) If Not rng Is Nothing Then If ActiveSheet.ProtectContents = True Then ActiveSheet.Unprotect End If For Each r In rng If r.Value = "承認" Then r.EntireRow.Locked = True Else r.EntireRow.Locked = False End If Next r ActiveSheet.Protect DrawingObjects:=True, Contents:=True End If End Sub マクロに関してはド素人で、自動記録かWebで調べて見つけたマクロをちょっと加工して使う程度です。 どうか知恵をお貸しください。よろしくお願いします。

  • Excel VBAで罫線を引くマクロを書きたい

    Excel VBAで罫線を引くマクロを書きたいと思っています。 で、文末のコードを書きました。(というかマクロ記録したものほぼそのもの) これだとある程度動くのですが、内側線が無いような範囲を選択した場合にはエラーになってしまいます。 内側の線を引く際にIF文をかまさなければならないように思うのですが、イマイチわかりません。 この点について教えてください。 また、コードが冗長であるようにも思えます。もう少しスマートな書き方があればあわせて教えてください。 よろしくお願いします。 Sub 枠線基本() ' 周囲 With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With ' 内側 With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlHairline .ColorIndex = xlAutomatic End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlHairline .ColorIndex = xlAutomatic End With End Sub

  • エクセルVBA 行列の数を指定して罫線を引くマクロ教えてください

    下のようにSheet1に罫線を引く「開始セル(左右端)」「行」「列」が書いてあります。 この条件で罫線をSheet2に引くようにしたいのですが、変数の設定の仕方などが分かっていないようで、できません。教えていただけないでしょうか。マクロの記録をとったところ、下のようになりました。よろしくお願いします。 開始セル Sheet2!A1・・・Sheet1のB1セル 行 8・・・Sheet1のB2セル 列 2・・・Sheet1のB3セル Sub borders() Range("A1:B8").Select Selection.borders(xlDiagonalDown).LineStyle = xlNone Selection.borders(xlDiagonalUp).LineStyle = xlNone With Selection.borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With Selection.borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With End Sub

  • Excel2013,macroのcopy

    Excel2013でマクロ実行するのですが、最後のstepの「条件付き書式」のcopyが上手く行きません。どなたかご教授お願いします。 A~J列x約4000行(1行~5行まではTytle行)のsheetです。 A列は日付で、過去からの日付となって居ます。 このsheetを見易くする為にA6行に 「条件付き書式」 =mod(month(A6),2)=0 を設定し、下記のmacro式でA6~J4000にCOPYするのですが、結果は上手く行きません。 ※1:偶数月行のA列のみ正確に書式設定されるが、全列(A~J)にならない。 ※2:奇数月のところどころ(列)に書式が設定される。 の不具合が生じます。 考えられる原因は何でしょうか?、ご教授願えませんでしょうか?。 与えたmacro Range("A6").Select Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _ "=MOD(MONTH(A6),2)=0" Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 11200714 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = True Selection.Copy Range(Selection, Selection.End(xlDown)).Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlToRight)).Select Range(Selection, Selection.End(xlToRight)).Select Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _ SkipBlanks:=False, Transpose:=False Range("A6").Select End Sub

  • VBAでセルの色を変更するには

    ある範囲のあるセルの色のみ一気に変更したいのですが、自力で調べた結果はセルに色を付ける以下の記述までしか分かりませんでした。 例えば、現在のベージュ(40)を赤(3)に変更するにはどう記述すればよいのでしょうか? 宜しくお願いします。 Sub Macro1() Range("A1:N180").Select With Selection.Interior .ColorIndex = 40 .PatternColorIndex = xlAutomatic End With End Sub

  • エクセルのマクロ

    縦方向に連続したセルを選択状態にした時に、選択セルを基点として右横方向に連続してデータが入力されているセルを罫線で囲う、という処理をマクロで実行したいのですが。 例えば、以下の図で黒丸がデータ入力セルであるとすると、C1~C4を選択状態にして実行すると、C1~D1とC2~F2とC4が罫線で囲われます。  AB CD EFG 1○●●●○○○ 2●●●●●●○ 3○○○○○○○ 4●●●○○○○ 以下のマクロを実行すると、上図の3行目と4行目のところがかなり余計に罫線が引かれてしまいます。どう修正すればいいでしょうか? Sub test1()  yc = Selection.Rows.count  aa = ActiveCell.Address  For y = 0 To yc - 1  ActiveCell.Offset(y, 0).Select  Range(ActiveCell, ActiveCell.End _       (xlToRight)).Select        With Selection.Borders(xlEdgeLeft)       .LineStyle = xlContinuous       .Weight = xlThin       .ColorIndex = xlAutomatic    End With    With Selection.Borders(xlEdgeTop)       .LineStyle = xlContinuous       .Weight = xlThin       .ColorIndex = xlAutomatic    End With    With Selection.Borders(xlEdgeBottom)       .LineStyle = xlContinuous       .Weight = xlThin       .ColorIndex = xlAutomatic    End With    With Selection.Borders(xlEdgeRight)       .LineStyle = xlContinuous       .Weight = xlThin       .ColorIndex = xlAutomatic    End With    Range(aa).Select   Next y End Sub

専門家に質問してみよう