0

I am trying to increment my column by one in my range. People are saying to use .Cells for this, but I am getting an error. Type mistmatch. How can I increment my column of nameColumn Variable by 1?

sh.Range(sh.Cells(lastR1, nameColumn + 1), sh.Cells(lastR1, lastCol)).AutoFill _
   Destination:=sh.Range(sh.Cells(lastR1, nameColumn + 1), sh.Cells(lastR, lastCol))

name column variable:

nameColumn = Split(.Find(Sheets("Input").Range("A2"), .Cells(.Cells.Count), xlValues, _
        xlWhole).Address, "$")(1)
7
  • Is nameColumn a String? It shouldn't be.
    – BigBen
    Commented Dec 10, 2020 at 19:26
  • yes it is a string.
    – excelguy
    Commented Dec 10, 2020 at 19:28
  • 1
    sh.Cells(lastR1, nameColumn).Offset(,1). Or show us how you are determining nameColumn, probably better to work with the column index.
    – BigBen
    Commented Dec 10, 2020 at 19:28
  • you cannot add 1 to as string. Commented Dec 10, 2020 at 19:28
  • 3
    colNumber = .Find(Sheets("Input").Range("A2"), .Cells(.Cells.Count), xlValues, _ xlWhole).Column. Working with the column letter is clunky.
    – BigBen
    Commented Dec 10, 2020 at 19:40

1 Answer 1

1

Working with the column letter is clunky. You can't add 1 to a column letter.

Use the column index instead:

Dim foundCell As Range
Set foundCell = .Find(Sheets("Input").Range("A2"), .Cells(.Cells.Count), xlValues, xlWhole)

If Not foundCell Is Nothing Then
    Dim colNumber As Long
    colNumber = foundCell.Column
Else
    ' not found, throw error or alert user...
End If

Then you can easily do math with the column number, i.e. colNumber + 1.

Not the answer you're looking for? Browse other questions tagged or ask your own question.