首页 > 解决方案 > VBA,范围列 + 1

问题描述

我试图在我的范围内将我的列增加一。人们说要使用.Cells这个,但我得到一个错误。Type mistmatch. 如何将nameColumn变量列增加 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))

名称列变量:

nameColumn = Split(.Find(Sheets("Input").Range("A2"), .Cells(.Cells.Count), xlValues, _
        xlWhole).Address, "$")(1)

标签: excelvba

解决方案


使用列字母很笨重。您不能添加1到列字母。

改用列索引:

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

然后,您可以轻松地使用列号进行数学运算,即colNumber + 1.


推荐阅读