2012-03-16 16 views
0

矩形範囲の値を選択する方法を知っている人はいますか?この範囲は固定されません。この特定の例では、B5〜G7を矩形で選択し、いくつかの色で追加する条件書式を設定します。矩形範囲の値を選択しようとするとランタイムエラー1004が発生する

私は誰もがなぜ知っていれば、それは私にこの部分

ActiveSheet.Cells(colorrow & "2", _ 
ActiveSheet.Cells(colorrow & "2").End(xlDown).End(xlToRight)).Select 

不思議でエラーを与えているcodebutを試みてきましたか?感謝します!

enter image description here

私はいくつかのコードを書く試みてきました。次のように

私のコードは次のとおりです。

Sub Macro2() 

Dim thevaluestocopy As Variant, colorCell as Range, colorrow as Long, thefirstcolorrow as Long 

colorrow = 1 

Do 

Set colorCell = Sheets("Sheet1").Cells(colorrow, 1) 
'check for test1-test6 if its around do nothing, else goes to the next row and next column 
If colorCell = "test1" Or colorCell = "test2" Or colorCell = "test3" _ 
Or colorCell = "test4" Or colorCell = "test5" Or colorCell = "test6" _ Then 
'Do nothing 
Else 
thefirstcolorrow = Sheets("Sheet1").Cells(colorrow, 2) 
'This statement gives me the error.. not sure why it cant work 
ActiveSheet.Cells(colorrow & "2", _ 
ActiveSheet.Cells(colorrow & "2").End(xlDown).End(xlToRight)).Select 
Exit Do 
End If 
colorrow = colorrow + 1 
Loop 


'add colors into cell 
ActiveCell.Select 
Selection.FormatConditions.AddColorScale ColorScaleType:=3 
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority 
Selection.FormatConditions(1).ColorScaleCriteria(1).Type = _ 
    xlConditionValueLowestValue 
With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor 
    .Color = 8109667 
End With 
Selection.FormatConditions(1).ColorScaleCriteria(2).Type = _ 
    xlConditionValuePercentile 
Selection.FormatConditions(1).ColorScaleCriteria(2).Value = 50 
With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor 
    .Color = 8711167 
End With 
Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _ 
    xlConditionValueHighestValue 
With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor 
    .Color = 7039480 
End With 

End Sub 
+0

私はそれを行うより良い方法があると思います。データが常にこの順番になることを確認できますか?つまり、「テスト1」は「落ち込み」や「ハードテスト」や「ソフトテスト」の後には表示されませんか? –

+0

ええ、3つのテストの後には起こりません...しかし、私は私がOKでなければならないと思います。私は数多くの例を試しましたが、実際にうまくいきます... – user1204868

+0

それでは、 ) –

答えて

0

へ 変更

ActiveSheet.Cells(colorrow & "2", _ ActiveSheet.Cells(colorrow & "2").End(xlDown).End(xlToRight)).Select 

私は道を見出しました。残りと共有したいと思います。

Sub Macro2() 

Dim colorCell As Range, colorrow As Long, thefirstcolorrow As Long, colorrow1 As Long, colorCell1 As Range, nvalue As Long 
Dim thelastcolorRow As Long, n As Long, LastColtocolor As Long 

colorrow = 1 

LastColtocolor = Sheets("Sheet1").Cells("1" & Columns.Count).End(xlToLeft).Column 

Do 

Set colorCell = Sheets("Sheet1").Cells(colorrow, 1) 

'Check if cell holds any value of test1 - test6, etc... 
If colorCell = "test1" Or colorCell = "test2" Or colorCell = "test3" _ 
Or colorCell = "test4" Or colorCell = "test5" Or colorCell = "test6" _ Then 
'Do nothing 
Else 
thefirstcolorrow = colorrow 

Exit Do 
End If 

colorrow = colorrow + 1 
Loop 

colorrow1 = 1 

Do 
'Look for last row that has values 
Set colorCell1 = Sheets("Sheet1").Cells(colorrow1, 1) 
If colorCell1 = "" Then 
thelastcolorRow = colorrow1 

Exit Do 
End If 
colorrow1 = colorrow1 + 1 
Loop 

For nvalue = 1 To colorrow1 - 1 - colorrow 

Sheets("Sheet1").Range(Cells(thefirstcolorrow, 2), Cells(thefirstcolorrow + nvalue, LastColtocolor)).Select 

Next nvalue 

End sub 
0

私はあなたが何を意味するかは確かではないんだけど、あなたは次のように使用できるワークシートの長方形の範囲を参照したい場合:

With Sheet1 
    .Range(.Range("B5"), .Range("G7")).Select 
End With 

これは、Sheet1という名前のオブジェクトでB5:G7を選択します。別の方法としては、シート名を使用できます。

With Sheets("Sheet 1") 
    .Range(.Range("B5"), .Range("G7")).Select 
End With 

を、すなわち、シート1がオブジェクトであるSheet1.Name可能性が最も高い「シート1」と同じであることに注意してください、「シート1」はオブジェクトの名前です。あなたがこの区別を理解する(または理解することを学ぶ)なら、おそらくあなた自身が素晴らしいサービスをしているでしょう。

編集:

ActiveSheet.Range(ActiveSheet.Cells(colorrow & "2"), ActiveSheet.Cells(colorrow & "2").End(xlDown).End(xlToRight)).Select 
+0

こんにちは、私は変更するように頼んだものを試しました、それは私が期待するものを選択しなかったし、また、最初の行の最初の列の値を変更します – user1204868

+0

こんにちは、私は変更された値のため、しかし、あなたが与えたコードは、私が選択すると期待していない範囲を選択します – user1204868

関連する問題