我正在尝试编写一个循环,根据几个条件将单元格添加到某个范围。
1)细胞位于已经在范围内的细胞的右侧或下方,
2)单元格与先前指定的颜色相同。
Sub DefineContiguousRegion()
Dim c As Range
Dim areNewCells As Boolean
Do
areNewCells = False
For Each c In CurrentRange
If (Intersect(CurrentRange, c.Offset(1)) Is Nothing) And c.Offset(1).Interior.ColorIndex = CurrentColor Then
areNewCells = True
CurrentRange = Union(CurrentRange, c.Offset(1))
End If
If (Intersect(CurrentRange, c.Offset(, 1)) Is Nothing) And c.Offset(, 1).Interior.ColorIndex = CurrentColor Then
areNewCells = True
CurrentRange = Union(CurrentRange, c.Offset(, 1))
End If
Next
Loop Until areNewCells = False
问题是变量areNewCells在For Each循环结束时被重置为脱离上下文。因此,即使该值永远不会设置为true,也永远不会满足until条件。
为什么会这样?我该如何解决?
如果有人能想出一个更简单的方法来创建这个范围,那就太好了,但我真正想要的是更好地理解变量范围/生命周期,因为这是一个学习项目,而不是时间敏感的工作相关的。
答案 0 :(得分:0)
因为您要求更简单的方式,您可以考虑以下代码:
$result = $db_con->query('SELECT * FROM `some_table`');
$fp = fopen('php://output', 'w');
if ($fp && $result) {
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row));
}
die;
}