Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.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
Excel VBA循环根据列标题填充列_Vba_Excel - Fatal编程技术网

Excel VBA循环根据列标题填充列

Excel VBA循环根据列标题填充列,vba,excel,Vba,Excel,所以我有一个数据透视表,它每天都从SQL数据库更新。我想突出显示days>5的整个部分,但由于数据每天更新,条件格式不起作用。我创建了一个动态范围(见下文),现在我需要它来运行一个循环,以找到第29列(日期位于名称旁边)大于5的位置。我需要以下所有内容以红色突出显示,如附件所示。有什么帮助或建议吗?我知道这很复杂 代码: 豪迪,这应该能帮你完成工作。只需将ws变量指定给要在其上运行的工作表。如果你有任何问题,请告诉我 Sub ColorFill() Dim ws As Worksheet Dim

所以我有一个数据透视表,它每天都从SQL数据库更新。我想突出显示days>5的整个部分,但由于数据每天更新,条件格式不起作用。我创建了一个动态范围(见下文),现在我需要它来运行一个循环,以找到第29列(日期位于名称旁边)大于5的位置。我需要以下所有内容以红色突出显示,如附件所示。有什么帮助或建议吗?我知道这很复杂

代码:


豪迪,这应该能帮你完成工作。只需将ws变量指定给要在其上运行的工作表。如果你有任何问题,请告诉我

Sub ColorFill()
Dim ws As Worksheet
Dim rngColor As Range, rngHeader
Dim lastRow As Long, lastCol As Long, firstRow, firstCol

'Set Sheet to desired sheet
Set ws = Sheet1

'find top left of range
firstRow = ws.UsedRange.Row
firstCol = ws.UsedRange.Column

'find bottom right of range
lastRow = firstRow + ws.UsedRange.Rows.Count - 1
lastCol = firstCol + ws.UsedRange.Columns.Count - 1

'set range of headers
Set rngHeader = Range(Cells(firstRow, firstCol + 1), Cells(firstRow, lastCol))

'loop through range of headers and color column
For Each cell In rngHeader
If cell.Value > 5 Then
    Set rngColor = Range(cell.Offset(1, 0), Cells(lastRow, cell.Column))
    rngColor.Interior.ColorIndex = 3
End If
Next

End Sub

非常感谢你。有没有一个地方我可以设置我的头球位置?此外,我可以从哪里开始我的范围左上角?由于某种原因,代码没有填充我想要的红色矩形,仍然有斑点。有什么建议吗?我需要看更多你的数据来说明为什么它不起作用。我猜你的账单上不止这张桌子。您可以只将此表移动到一个单独的选项卡上,它将起作用,或者按照您所说的定义您的起点。谷歌如何在vba中设置范围,当我这样说时,不难理解:“将图纸设置为所需的图纸集ws=Sheet4 set startCell=Range(“N29”)”查找数据的最后一行和最后一列lastRow=ws.Cells(ws.Rows.Count,startCell.column)。End(xlUp)。row lastCol=ws.Cells(startCell.row,ws.Columns.Count)。End(xlToLeft).Column“选择数据的动态ramge ws.Range(startCell,ws.Cells(lastRow-1,lastCol-1))。选择“设置标题范围set rngHeader=Range(Cells(firstRow,firstCol+1),Cells(firstRow,lastCol))”“设置标题范围”部分会弹出vba“运行时错误”的任何提示,我想你应该把xlUp改成xlDown,把xlToLeft改成xlToRight。同样,如果没有数据的精确结构,很难说。
Sub ColorFill()
Dim ws As Worksheet
Dim rngColor As Range, rngHeader
Dim lastRow As Long, lastCol As Long, firstRow, firstCol

'Set Sheet to desired sheet
Set ws = Sheet1

'find top left of range
firstRow = ws.UsedRange.Row
firstCol = ws.UsedRange.Column

'find bottom right of range
lastRow = firstRow + ws.UsedRange.Rows.Count - 1
lastCol = firstCol + ws.UsedRange.Columns.Count - 1

'set range of headers
Set rngHeader = Range(Cells(firstRow, firstCol + 1), Cells(firstRow, lastCol))

'loop through range of headers and color column
For Each cell In rngHeader
If cell.Value > 5 Then
    Set rngColor = Range(cell.Offset(1, 0), Cells(lastRow, cell.Column))
    rngColor.Interior.ColorIndex = 3
End If
Next

End Sub