Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 尝试选择值的矩形范围时出现运行时错误1004_Vba_Excel - Fatal编程技术网

Vba 尝试选择值的矩形范围时出现运行时错误1004

Vba 尝试选择值的矩形范围时出现运行时错误1004,vba,excel,Vba,Excel,我想知道是否有人知道如何选择矩形范围的值?此范围将不固定。对于这个特定的示例,它将选择矩形形式的B5-G7,然后设置一个条件格式以添加一些颜色 我已经尝试了代码,但这部分给了我一个错误 ActiveSheet.Cells(colorrow & "2", _ ActiveSheet.Cells(colorrow & "2").End(xlDown).End(xlToRight)).Select 不知是否有人知道原因?我会感激的 我试图写出一些代码 我的代码如下: Sub Macr

我想知道是否有人知道如何选择矩形范围的值?此范围将不固定。对于这个特定的示例,它将选择矩形形式的B5-G7,然后设置一个条件格式以添加一些颜色

我已经尝试了代码,但这部分给了我一个错误

ActiveSheet.Cells(colorrow & "2", _
ActiveSheet.Cells(colorrow & "2").End(xlDown).End(xlToRight)).Select
不知是否有人知道原因?我会感激的

我试图写出一些代码

我的代码如下:

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

我不确定你的意思,但如果你想在工作表中引用一个矩形区域,你可以使用以下方法:

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很可能等于Sheet1,即Sheet1是对象,Sheet1是对象的名称。如果你理解或学会理解这一区别,你可能会为自己做出巨大的贡献

编辑: 改变


我找到了一个办法。我想和其他人分享

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

您好,我尝试了您要求我更改的代码,它没有选择我期望的内容,它还更改了我的第一行第一列的值。您好,由于前面提到的更改值,我对代码做了一些更改。。但是你给出的代码仍然选择了我不希望它选择的范围。我认为有更好的方法。你能确认数据总是按那个顺序排列吗。i、 e测试1在掉测、硬测或软测之后永远不会出现?是的。。在那三次测试之后就不会发生了。。。但我想我的应该没问题。。我已经尝试了许多例子,它实际上工作得很好…好吧,那么我们就到此为止:
ActiveSheet.Range(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