我正在尝试填充表格中的每两行,但我希望彩色线条停在最后一列。这是我的代码,你能告诉我出了什么问题吗?
Sub FormatChargeCompleted()
Dim LR As Long, LC As Long, x As Long, y As Long
Workbooks("sample.xls").Worksheets("sample").Activate
LR = Worksheets("sample").Cells(Rows.Count, 1).End(xlUp).Row
LC = Worksheets("sample").Cells(Columns.Count, 1).End(xlToLeft).Column
For x = 1 To LR Step 2
For Columns.Value = 1 To LC
Rows(x).Interior.Color = RGB(200, 200, 200)
Next Columns.Value
Next x
End Sub
感谢!
答案 0 :(得分:1)
最后,这是我正在寻找的代码,我在查看L42的答案时发现:
Sub FormatChargeCompleted()
Dim LR As Long, LC As Long, x As Long, y As Long, fillrng As Range
Workbooks("sample.xlsx").Worksheets("sample").Activate
LR = Cells(Rows.Count, 1).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column
For x = 1 To LR
If WorksheetFunction.IsOdd(x) Then
For y = 1 To LC
Cells(x, y).Interior.Color = RGB(200, 200, 200)
Next y
End If
Next x
End Sub
答案 1 :(得分:0)
不是编码,而是将范围格式化为表格。有几种内置颜色方案,其中许多包含交替的行颜色。当您添加/插入/删除行时,交替模式将自动应用。
作为奖励,标题行包含用于过滤和排序的下拉列表。
没有一行代码的所有内容。
答案 2 :(得分:0)
根据我的评论,我尝试重新编写代码。
Sub FormatChargeCompleted()
Dim LR As Long, LC As Long, x As Long, fillrng As Range
With Worksheets("sample")
LR = .Cells(.Rows.Count, 1).End(xlUp).Row
LC = .Cells(1, .Columns.Count).End(xlToLeft).Column
For x = 1 To LR Step 4
If fillrng Is Nothing Then
Set fillrng = .Range(.Cells(x, 1), .Cells(x + 1, LC - 1))
Else
Set fillrng = Union(fillrng, .Range(.Cells(x, 1), _
.Cells(x + 1, LC - 1)))
End If
Next
If Not fillrng Is Nothing Then fillrng.Interior.Color = RGB(200, 200, 200)
End With
End Sub
<强>结果:强>
以下是我发现的一些错误。
LC
无法正确反映最后一栏。 Columns.Count
应位于ColumnIndex
而不是RowIndex
。如果这不是您需要的结果,那么也可以发布示例插图,或者您可以使用代码并调整它以适应。 HTH。