简体   繁体   English

VBA单元格格式错误

[英]VBA Cell Format Error

I'm trying to format a group of cells using a for loop (later a conditional while loop) 我正在尝试使用for循环格式化一组单元格(后来有条件的while循环)

I'm getting the error '1004' 我收到错误“ 1004”

Method 'Range' of object "Global' failed 对象“全局”的方法“范围”失败

I can't figure out how to convert the cells value to a range value that doesn't throw this error. 我不知道如何将单元格值转换为不会引发此错误的范围值。 The Cells(x,y) by itself throws an error as well Cells(x,y)本身也会引发错误

Thanks in advance 提前致谢

    For row = 2 To 5

    With Range(Cells(row, 5)).Interior
        .Pattern = xlPatternRectangularGradient
        .Gradient.RectangleLeft = 0.5
        .Gradient.RectangleRight = 0.5
        .Gradient.RectangleTop = 0.5
        .Gradient.RectangleBottom = 0.5
        .Gradient.ColorStops.Clear
    End With
    With Range(Cells(row, 5)).Interior.Gradient.ColorStops.Add(0)
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Range(Cells(row, 5)).Interior.Gradient.ColorStops.Add(1)
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = -0.250984221930601
    End With

Next row

Why are you even looping? 你为什么还要循环播放?

Sub SO()

    With Range(Cells(2, 5), Cells(5, 5)).Interior
        .Pattern = xlPatternRectangularGradient
        .Gradient.RectangleLeft = 0.5
        .Gradient.RectangleRight = 0.5
        .Gradient.RectangleTop = 0.5
        .Gradient.RectangleBottom = 0.5
        .Gradient.ColorStops.Clear
        With .Gradient.ColorStops.Add(0)
            .ThemeColor = xlThemeColorDark1
            .TintAndShade = 0
        End With
        With .Gradient.ColorStops.Add(1)
            .ThemeColor = xlThemeColorAccent6
            .TintAndShade = -0.250984221930601
        End With
    End With

End Sub

No need for Range() 不需要Range()

Just do Cells(row, 5).Interior 只做Cells(row, 5).Interior

  For row = 2 To 5

    With Cells(row, 5).Interior
        .Pattern = xlPatternRectangularGradient
        .Gradient.RectangleLeft = 0.5
        .Gradient.RectangleRight = 0.5
        .Gradient.RectangleTop = 0.5
        .Gradient.RectangleBottom = 0.5
        .Gradient.ColorStops.Clear
    End With
    With Cells(row, 5).Interior.Gradient.ColorStops.Add(0)
        .ThemeColor = xlThemeColorDark1
        .TintAndShade = 0
    End With
    With Cells(row, 5).Interior.Gradient.ColorStops.Add(1)
        .ThemeColor = xlThemeColorAccent6
        .TintAndShade = -0.250984221930601
    End With

Next row

Get rid of of Range() 摆脱Range()

With Cells(row, 5).Interior

You want to either use 您想使用

Cells(row, col) 
Cells(2, 5)

or 要么

Range(addressString)
Range("E2")

If you don't put a worksheet object in front of either of them they'll refer to the Activesheet or 'Me' if you're working within a worksheet module. 如果您没有在任何一个工作表对象的前面放置对象,则在工作表模块中工作时,它们将引用Activesheet或“ Me”。

For reference something like: 供参考,例如:

Dim ws as worksheet
set ws = Thisworkbook.sheets("Sheet1")

ws.cells(2, 5)
ws.Range("E2")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM