vba - 选择具有 i 个整数的单元格范围
问题描述
我正在尝试选择其中包含 i 个整数的单元格范围。我想要一个特定范围的单元格,而不是选择整行。下面我提供了用于选择整行的代码(它运行良好)以及我为选择特定范围而编写的代码(它不运行)
行代码
With ws2
lastrow1 = .Range("A" & Rows.Count).End(xlUp).Row
For i = 4 To lastrow1
If .Cells(i, "D").Interior.ColorIndex = -4142 Or .Cells(i, "D").Interior.ColorIndex = 2 Then
If CopyRange Is Nothing Then
Set CopyRange = .Rows(i)
Else
Set CopyRange = Union(CopyRange, .Rows(i))
End If
End If
Next i
End With
范围代码
With ws2
lastrow1 = .Range("A" & Rows.Count).End(xlUp).Row
For i = 4 To lastrow1
If .Cells(i, "D").Interior.ColorIndex = -4142 Or .Cells(i, "D").Interior.ColorIndex = 2 Then
If CopyRange Is Nothing Then
Set CopyRange = .Cells("A & i & ":F" & i)
Else
Set CopyRange = Union(CopyRange, .Rows(i))
End If
End If
Next i
End With
解决方案
这里的问题是Cells
它只接受像这样的行和列,Cells(row, column)
所以你需要使用Range()
像.Range("A" & i & ":F" & i)
.
您还需要else
相应地调整您的部分,因为您仍然Rows(i)
在Union()
With ws2
lastrow1 = .Range("A" & Rows.Count).End(xlUp).Row
For i = 4 To lastrow1
If .Cells(i, "D").Interior.ColorIndex = -4142 Or .Cells(i, "D").Interior.ColorIndex = 2 Then
If CopyRange Is Nothing Then
Set CopyRange = .Range("A" & i & ":F" & i)
Else
Set CopyRange = Union(CopyRange, .Range("A" & i & ":F" & i))
End If
End If
Next i
End With
推荐阅读
- mysql - 通过 mySQL 连接到 Active Directory 数据库
- php - 如何在没有命名空间的情况下将 PHP 库添加到 TYPO3 扩展?
- angular - angular 7 http post在edge和firefox上工作,但在chrome上返回错误
- prolog - 通过列表约束变量域
- c# - 使用 WPF PRISM MVVM 打开 CommonOpenFileDialog
- python - 使用漂亮的汤从网络抓取的维基百科页面 html 文件中提取相关数据时遇到问题
- c# - 如何使用 XUnit 复制 MSTest 附件功能?
- typo3 - 在本地安装和设置 TYPO3 的最快方法是什么?
- android - 如何从 Kotlin 的 SearchView 中正确检索数据?
- java - 将图像绘制为多边形