VBA初心者です。
下記のようなコードを書いてB11~BH41をソートしたいのですが「オブジェクトが必要です」というエラーが出て上手く動いてくれません。
B11~BH41は横のセルと結合しているものがいくつか含まれているため一度結合を解除して再び結合させており、B11の列には日付、L11の列には時間を入れています。
Sub sort2keys()
Dim KeyCells As Range
Dim i As Integer
Dim x As Integer, y As Integer
x = Target.Column
y = Target.Row
Set KeyCells = Range("B11:B41")
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
'日付が消去された場合は行に入力されているものも消去 start
If Cells(y, 2).Value = "" Then
Application.ScreenUpdating = False
Range(Cells(y, 8), Cells(y, 21)).ClearContents
Range(Cells(y, 26), Cells(y, 35)).ClearContents
Range(Cells(y, 40), Cells(y, 60)).ClearContents
Application.ScreenUpdating = True
End If
'日付が消去された場合は行に入力されているものも消去 endS
ActiveSheet.Unprotect
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Range("B11:BH41").UnMerge
Range("B11:BH41").sort key1:=Range("B11"), order1:=xlAscending, Key2:=Range("L11"), Order2:=xlAscending
i = 11
While i < 42
Range(Cells(i, 2), Cells(i, 3)).Merge
Range(Cells(i, 4), Cells(i, 5)).Merge
Range(Cells(i, 6), Cells(i, 7)).Merge
Range(Cells(i, 8), Cells(i, 11)).Merge
Range(Cells(i, 12), Cells(i, 16)).Merge
Range(Cells(i, 17), Cells(i, 21)).Merge
Range(Cells(i, 22), Cells(i, 25)).Merge
Range(Cells(i, 26), Cells(i, 30)).Merge
Range(Cells(i, 31), Cells(i, 35)).Merge
Range(Cells(i, 36), Cells(i, 39)).Merge
Range(Cells(i, 40), Cells(i, 41)).Merge
Range(Cells(i, 42), Cells(i, 45)).Merge
Range(Cells(i, 46), Cells(i, 49)).Merge
Range(Cells(i, 50), Cells(i, 60)).Merge
If Worksheets(1).Cells(i, 2) <> "" And Worksheets(1).Cells(i, 8).Value = "" Then
Application.Goto Reference:=Worksheets(1).Cells(i, 8), Scroll:=False
End If
i = i + 1
Wend
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
ActiveSheet.Protect
End If
End Sub
試しにSheet2のB11~BH41に適当な数値を入れて
Sub test()
Range("B11:BH41").sort key1:=Range("B11"), order1:=xlAscending, Key2:=Range("L11"), Order2:=xlAscending
End Sub
として実行した場合はちゃんと並び替えが出来ました。
解決方法ご教示よろしくお願いいたします。
No.1ベストアンサー
- 回答日時:
こんにちは
>sortしようとしたときに「オブジェクトが必要です」というエラーが出る
それって本当にSortメソッドで出ているエラーですか??
ざっと見たところ、
>x = Target.Column
で、エラーになりそうですけれど・・・?
単に、変数「Target」が未定義なためにエラーが起きているだけではないでしょうか?
それとも、グローバルな変数として定義されているのでしょうか?
もしそうであるなら、実行時のTargetの内容はどうなっていますか?
こんにちは、ご教示ありがとうございます。
以前、
Range("B11:BH41").sort key1:=Range("B11"), order1:=xlAscending, Key2:=Range("L11"), Order2:=xlAscending
だった部分は
Call Range("B11:BH41").Sort(key1:=Range("B11"), order1:=xlAscending)
でした。その時は「オブジェクトが必要です」と出ていなかったのでそこがおかしいのかと思っていたのですが、一行目の
Sub sort2keys()
の部分も
Private Sub Worksheet_Change(ByVal Target As Range)から変更していたのを忘れていました。
1行目の部分を
Private Sub Worksheet_Change(ByVal Target As Range)
に戻したら正常に動いたのでSortは関係ありませんでした。
すみませんでした。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
- ・漫画をレンタルでお得に読める!
- ・街中で見かけて「グッときた人」の思い出
- ・「一気に最後まで読んだ」本、教えて下さい!
- ・幼稚園時代「何組」でしたか?
- ・激凹みから立ち直る方法
- ・1つだけ過去を変えられるとしたら?
- ・【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集
- ・【あるあるbot連動企画】フォロワー20万人のアカウントであなたのあるあるを披露してみませんか?
- ・映画のエンドロール観る派?観ない派?
- ・海外旅行から帰ってきたら、まず何を食べる?
- ・誕生日にもらった意外なもの
- ・天使と悪魔選手権
- ・ちょっと先の未来クイズ第2問
- ・【大喜利】【投稿~9/7】 ロボットの住む世界で流行ってる罰ゲームとは?
- ・推しミネラルウォーターはありますか?
- ・都道府県穴埋めゲーム
- ・この人頭いいなと思ったエピソード
- ・準・究極の選択
- ・ゆるやかでぃべーと タイムマシンを破壊すべきか。
- ・歩いた自慢大会
- ・許せない心理テスト
- ・字面がカッコいい英単語
- ・これ何て呼びますか Part2
- ・人生で一番思い出に残ってる靴
- ・ゆるやかでぃべーと すべての高校生はアルバイトをするべきだ。
- ・初めて自分の家と他人の家が違う、と意識した時
- ・単二電池
- ・チョコミントアイス
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
実行時エラー 438になった時の...
-
エクセルエラー13型が一致しま...
-
VBAがブレークモードになっ...
-
Excel2007でWebサービスを利用...
-
実行時エラー3001「引数が間違...
-
なぜこんな初歩的なVBAのIf文で...
-
[Delphi] データセットは閉じて...
-
OLEDB.NETで接続できない
-
VBで構造体を使うさ際の64k...
-
ExcelVBA Range クラスの Page...
-
VBAでのエラー
-
Outlook.ApplicationをCreateOb...
-
VBS実行時エラー オブジェクト...
-
マクロについて教えてください...
-
VBAでESCキーを無効にしたいの...
-
VB6のDateDiff関数が、お客さん...
-
【Excel VBA】マクロをボタンに...
-
Access2000での未定義関数repla...
-
1列目の何行目に検索文字がある...
-
テキストボックスに記述した数...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
実行時エラー 438になった時の...
-
エクセルエラー13型が一致しま...
-
VBAがブレークモードになっ...
-
なぜこんな初歩的なVBAのIf文で...
-
【Excel VBA】マクロをボタンに...
-
VBSで変数の宣言はできないので...
-
VBS実行時エラー オブジェクト...
-
実行時エラー3001「引数が間違...
-
ExcelVBA Range クラスの Page...
-
ExcelVBAで、ユーザー定義型は...
-
EXCEL VBAマクロ中断でデバッグ...
-
マクロについて教えてください...
-
プロシージャ名の取得
-
ADODB.Streamを使用してUTF-8を...
-
実行時エラー -'-2147417848
-
Outlook.ApplicationをCreateOb...
-
VBAでのエラー
-
なぜエラーになるのでしょうか...
-
VBAのコードがエラーになっ...
-
[Delphi] データセットは閉じて...
おすすめ情報