简体   繁体   English

如何在VBA中跨范围的行和列循环?

[英]How do I loop across both rows and columns of a range in VBA?

I'm trying to create a Gantt chart type display using only cell values. 我正在尝试仅使用单元格值创建甘特图类型显示。 Formulas will not work due to formatting so I need to have VBA run instead. 由于格式,公式无法正常工作,因此我需要运行VBA。

I want to loop across the both the columns and rows of a single named range. 我想遍历单个命名范围的列和行。 However, I don't think the VBA format is correct as this errors out "438: Object doesn't support this property or method" 但是,我认为VBA格式不正确,因为这样会出现错误“ 438:对象不支持此属性或方法”

Dim d As Integer
Dim e As Integer
For d = 1 To **Range("PRcal").Cols.Count**
    For e = 1 To **Range("PRcal").Rows.Count**
        If [Range("PRcal").Cells.(e, d).Value = Range("PRcal").Cells(1, d).Value] Then
            Cells(e, d).Value = Cells(e, 1)
        End If
    Next e
Next d

Any suggestions? 有什么建议么? Thanks! 谢谢!

  • Ranges don't have a Cols property - they do have a Columns property 范围没有Cols属性-它们具有Columns属性
  • you have an extra dot in your if in Cells.(e,d) ifCells.(e,d)中,您的if中有一个额外的点Cells.(e,d)
  • you can't use square brackets in your if 您不能在您的if使用方括号

This should work fine: 这应该工作正常:

Dim d As Integer
Dim e As Integer
For d = 1 To Range("PRcal").Columns.Count
    For e = 1 To Range("PRcal").Rows.Count
        If Range("PRcal").Cells(e, d).Value = Range("PRcal").Cells(1, d).Value Then
            Cells(e, d).Value = Cells(e, 1)
        End If
    Next e
Next d

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

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