2016-12-29 4 views
0

vbaスクリプトを使用してExcelの各ヘッダー列にテキストを追加しようとしています。セルに追加する際にexcel vbaの型が一致しない

Dim c As Range 

For Each c In Sheet13.Rows(1) 
    c.Value = c.Value & "bleh" 
Next 

しかし、これは私に型の不一致のエラー13を与えていると私はなぜ知りません。 セルにはテキストが含まれています。

答えて

2
For Each c In Sheet13.UsedRange.Rows(1).Cells 

.Cells全行ではなく個々のセルを取得する。

.UsedRange行の最後に移動しないようにしてください(XFD1)。あなたはすなわち、非空で、一定の細胞のみを得るためにそれを微調整することができます。:

For Each c In Sheet13.UsedRange.Rows(1).SpecialCells(xlCellTypeConstants) 
+1

を。タイマーがなくなると正しく表示されます。 – CathalMF

2

以下のコード試してみてください。優れたおかげ

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 
関連する問題