2012-03-16 46 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

我認爲有一個更好的方法來做到這一點。你能確認數據總是按照這個順序嗎?即「test1」在「droptest」或「最硬」或「最軟」之後不會出現? – 2012-03-16 11:51:07

+0

yup ..它不會發生在那3個測試之後......但我認爲我的應該沒問題..我已經嘗試了一些例子,它實際上工作正常...... – user1204868 2012-03-16 13:26:57

+0

好吧,我們將離開它在那: ) – 2012-03-16 14:03:43

回答

0

我已經找到了一條出路。並喜歡與其他人分享..

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 

注意Sheet1.Name是最有可能等於「表1」,即工作表Sheet是對象,「表1」是對象的名稱。如果你理解(或學會理解)這種區別,你可能會爲自己做一個很好的服務。

編輯: 變化

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

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

你好,我已經試過一個你問我改變,它沒有選擇我的期望,它也改變了我的第一行第一列的價值 – user1204868 2012-03-16 01:56:29

+0

嗨,我已經改變了我的代碼有點因爲改變的價值,我前面提到..但仍然你給的代碼選擇了我不期望它選擇的範圍 – user1204868 2012-03-16 02:29:19

相關問題