Excel 单元格的内部颜色,直到列中包含数据的最后一个单元格

Excel 单元格的内部颜色,直到列中包含数据的最后一个单元格,excel,vba,Excel,Vba,我的问题是: 我有一个excel电子表格,其中有表2、总体数据和表3 使用从表2中复制的基于年份的数据复制到表8 在每张图纸(第3-8页)中,添加每行(不包括第1行)中的值 和D列中每行的总和 从第2行到第4行,D列中有内饰颜色和粗体字体 最后一行数据我使用了以下代码(示例 第3页) 使用表2中的命令按钮更新电子表格 当我在VB开发人员中单独运行代码时,有时它会工作 有时它会导致运行时错误1004 当我尝试使用按钮更新电子表格时,它总是 导致错误的原因 有人能帮我避免这个错误并更新电子表格,

我的问题是:

  • 我有一个excel电子表格,其中有表2、总体数据和表3 使用从表2中复制的基于年份的数据复制到表8
  • 在每张图纸(第3-8页)中,添加每行(不包括第1行)中的值 和D列中每行的总和
  • 从第2行到第4行,D列中有内饰颜色和粗体字体 最后一行数据我使用了以下代码(示例 第3页)
  • 使用表2中的命令按钮更新电子表格
  • 当我在VB开发人员中单独运行代码时,有时它会工作 有时它会导致运行时错误1004
  • 当我尝试使用按钮更新电子表格时,它总是 导致错误的原因
有人能帮我避免这个错误并更新电子表格,在表格的每个D列中保留内部颜色和粗体字体吗?

试试这个:
台前

Sheet3.Range("D2", Cells(Rows.Count, "D").End(xlUp).Offset(-2, 0)).Interior.Color = RGB(255, 192, 0)
Sheet3.Range("D2", Cells(Rows.Count, "D").End(xlUp).Offset(-2, 0)).Font.Bold=True  

lastrow2=Sheet3.单元格(Rows.Count,“D”).结束(xlUp).行

并将您的行更改为:

Sheet3.Range("D2", Cells(lastrow2, "D")).Interior.Color = RGB(255, 192, 0)
Sheet3.Range("D2", Cells(lastrow2, "D")).Font.Bold = True
您还可以更改此设置:

For j = 2 To lastrow2

Sheet3.Cells(j, 4).Value = Sheet3.Cells(j, 5).Value + Sheet3.Cells(j, 6).Value + Sheet3.Cells(j, 7).Value + Sheet3.Cells(j, 8) + Sheet3.Cells(j, 9).Value + Sheet3.Cells(j, 10).Value + Sheet3.Cells(j, 11).Value +      Sheet3.Cells(j, 12).Value + Sheet3.Cells(j, 13).Value + Sheet3.Cells(j, 14).Value + Sheet3.Cells(j, 15).Value + Sheet3.Cells(j, 16).Value

Next j  
为此:

Sheet3.Range("D2:D" & lastrow2).Formula = "=SUM(E2:T2)"  
要浏览第3-8页,整个代码如下所示(请记住
j
是该页的索引!如有必要,请进行调整):


尝试使用
Sheet3.Range(“D2”,Sheet3.Cells(Rows.Count,“D”).End(xlUp).Offset(-2,0)).Interior.Color=RGB(255,192,0)
(与下一行相同)。看,我确实试过用电池旁边的Sheet3,但一直出错谢谢你的帮助。这个{Sheets(j).Range(“D2”,Cells(lastrow2,“D”))).Interior.Color=RGB(255,192,0)}仍然会导致运行时错误1004,我真的不明白为什么会发生这种情况。我将lastrow2更改为lastrow3,以便在列D中有不同的最后一行,并在Cells(lastrow3,“D”)之前添加了表(j),效果非常好。谢谢你的帮助!
For j = 2 To lastrow2

Sheet3.Cells(j, 4).Value = Sheet3.Cells(j, 5).Value + Sheet3.Cells(j, 6).Value + Sheet3.Cells(j, 7).Value + Sheet3.Cells(j, 8) + Sheet3.Cells(j, 9).Value + Sheet3.Cells(j, 10).Value + Sheet3.Cells(j, 11).Value +      Sheet3.Cells(j, 12).Value + Sheet3.Cells(j, 13).Value + Sheet3.Cells(j, 14).Value + Sheet3.Cells(j, 15).Value + Sheet3.Cells(j, 16).Value

Next j  
Sheet3.Range("D2:D" & lastrow2).Formula = "=SUM(E2:T2)"  
Sub YearlyForcast2011_2012()

Dim j As Integer
Dim lastrow2 As Long
Dim sumrange As Long

For j = 3 To 8

    Sheets(j).Columns("D").HorizontalAlignment = xlRight

    lastrow2 = Sheets(j).Cells(Rows.Count, 1).End(xlUp).Row

     Sheets(j).Range("D2:D" & lastrow2).Formula = "=SUM(E2:T2)"

    sumrange = Sheets(j).Cells(Rows.Count, "D").End(xlUp).Row

     Sheets(j).Range("D" & sumrange + 2).Formula = "=SUM(D2:D" & sumrange & ")"
     Sheets(j).Range("D" & sumrange + 2).Font.Bold = True
     Sheets(j).Range("D" & sumrange + 2).Font.Size = 12
     Sheets(j).Range("D" & sumrange + 2).Font.Color = RGB(255, 0, 0)

    lastrow2 = Sheets(j).Cells(Rows.Count, "D").End(xlUp).Row

     Sheets(j).Range("D2", Cells(lastrow2, "D")).Interior.Color = RGB(255, 192, 0)
     Sheets(j).Range("D2", Cells(lastrow2, "D")).Font.Bold = True

     Sheets(j).Range("c" & sumrange + 2).Value = "TOTAL 2011-2011 YEARLY FORCAST"
     Sheets(j).Range("c" & sumrange + 2).Font.Bold = True
     Sheets(j).Range("c" & sumrange + 2).Font.Size = 12
     Sheets(j).Range("c" & sumrange + 2).Font.Color = RGB(255, 0, 0)
     Sheets(j).Range("c" & sumrange + 2).HorizontalAlignment = xlRight

Next j

Application.ScreenUpdating = False
Application.CutCopyMode = False

End Sub