在附加到单元格时,在excel vba中input不匹配
我试图追加一些文本到我的Excel文档中的每个标题列与VBA脚本。
Dim c As Range For Each c In Sheet13.Rows(1) c.Value = c.Value & "bleh" Next
然而,这是给我一个types不匹配的错误13,我不知道为什么。 单元格只包含文本。
For Each c In Sheet13.UsedRange.Rows(1).Cells
.Cells
获取单个单元格,而不是整行。
.UsedRange
避免到行的末尾( XFD1
)。 你可以调整它只获得非空的,恒定的单元格,即:
For Each c In Sheet13.UsedRange.Rows(1).SpecialCells(xlCellTypeConstants)
试试下面的代码:
Option Explicit Sub AddBleh() Dim c As Range Dim LastCol As Long Dim HeaderRng As Range With Sheet13 ' find last column with data in header row LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column ' set Range to only header row where there is data Set HeaderRng = .Range(.Cells(1, 1), .Cells(1, LastCol)) For Each c In HeaderRng.Cells c.Value = c.Value & "bleh" Next End With End Sub