【问题标题】:Searching for Text in column and incrementing the value in the next cell by 1在列中搜索文本并将下一个单元格中的值加 1
【发布时间】:2014-12-04 02:20:53
【问题描述】:

在我的工作表的“G”列中,我的单元格中包含文本“已计费月数” - 我有搜索此文本的代码,并且在文本旁边的单元格中将值增加 1。

我在此站点上找到了代码,但由于某种原因,我收到运行时错误 13 - 更改所有值后的类型不匹配。编辑 - 我做了一步,一旦代码运行通过列 G 并更改了所有值,它就会循环回 For Each 语句,然后我在 found.offset 语句中得到运行时错误。

这是我的代码:

Sub UpdateMonthsBilled()

Dim findRng As Range, _
    targetRng As Range, _
    findCell As Range, _
    found As Range
Dim firstFound As String, _
    columnName As String
Dim Month As Integer

Month = 1
columnName = "G"
Set findRng = Range("G5:H650")
For Each findCell In findRng
    Set targetRng = Range(columnName & "2", Range(columnName & Rows.Count).End(xlUp))
    With targetRng
        Set found = .Find(findCell.Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not found Is Nothing Then
            firstFound = found.Address
            Do
                found.Offset(0, 1).Value = found.Offset(0, 1).Value + 1

                Set found = .FindNext(found)
            Loop While Not found Is Nothing And found.Address <> firstFound
        End If
    End With
Next findCell


End Sub

任何帮助将不胜感激,因为我不知道我哪里出错了?

【问题讨论】:

    标签: excel vba search runtime-error increment


    【解决方案1】:

    我将您的代码复制到 VBA 并运行它。

    G1 到 G4 行似乎也在递增。

    但是没有错误。这是 Excel 2003。

    【讨论】:

    • 我正在运行 excel 2013。当我执行 Step Into 时,代码正在运行 G 列,并为所有 15 次出现的“月计费”增加 H 列中的单元格。一旦它到达最后一次出现,代码就会返回到 For Each 语句,然后一旦它到达 offset 语句,我就会得到错误。有没有办法更改代码,以便在更改最后一次出现的 Months Billed 后结束?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 2022-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多