0

所以这是我的情况。请参阅图片以更好地理解。示例表

所以我想要做的是,如果C 列中的单元格不为空,则 excel 会将 B 列的前 3 个字母填充到 D 列。我使用以下宏来执行此操作:

Sub FillCountryCode()

Sheets("Sheet1").Range("D2:D20").Formula = "=IF(C2 <> """", LEFT(B2,3), """")"

End Sub

所以我对这个解决方案有两个问题。

1st)如果我移动国家代码列,假设列 F,宏将不再工作,因为它没有意识到该列已被移动。那么改变宏工作的最简单方法是什么,以便它根据标题名称(例如Country Code)搜索正确的列,然后遍历所有行(在我的实际 excel 文件中,有数百行,当示例表仅有 8). 因此,宏的相关标题和单元格位于哪一列实际上并不重要。

2nd)目前我正在手动确定宏的范围,使宏检查文件中所有行的正确命令是什么。(所有行在Car BrandCountry中都有值)

这是我在尝试解决此问题时提出的解决方案。

Sub FillCountryCode()


Worksheets("Sheet1").Activate
Rows(3).Select

Set CountRY = Selection.Find(What:="COUNTRY", After:=ActiveCell, LookIn:=xlFormulas, _
                           LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _
                           MatchCase:=False, SearchFormat:=False)

Set ENGINE = Selection.Find(What:="ENGINE", After:=ActiveCell, LookIn:=xlFormulas, _
                           LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _
                           MatchCase:=False, SearchFormat:=False)

Set COUNTRYCODE = Selection.Find(What:="COUNTRYCODE", After:=ActiveCell, LookIn:=xlFormulas, _
                           LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _
                           MatchCase:=False, SearchFormat:=False)

Dim LastItemRow As Long

LastItemRow = Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row

For pointer = 4 To LastItemRow

    If Cells(pointer, ENGINE.Column) <> "" Then

        Cells(pointer, COUNTRYCODE.Column).Value = Sheets("Sheet1").Left(Cells(pointer, COUNTRY.Column), 3)

    End If

Next pointer

End Sub

尝试运行此解决方案时,IF 条件存在一些问题,但我不明白它是什么。有人能帮我吗?我得到的错误是:错误 438:对象不支持此属性或方法。

4

1 回答 1

0

我认为你应该得到分数,因为你已经完成了大部分工作。我只是发布您的代码并进行一些修改,以使其效率更高,并避免弹出错误消息。我添加了一些评论来解释这些变化

Sub FillCountryCode()

Dim COUNTRY As Range, ENGINE As Range, COUNTRYCODE As Range 'declared all your variables
Dim LastItemRow As Long, pointer As Long

With Worksheets("Sheet1")   'removes Selects and need to repeat sheet reference elsewhere
    Set COUNTRY = .Rows(3).Find(What:="COUNTRY", LookIn:=xlFormulas, _
                           LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _
                           MatchCase:=False, SearchFormat:=False)   'remove ActiveCell reference as will error if not in search range
    Set ENGINE = .Rows(3).Find(What:="ENGINE", LookIn:=xlFormulas, _
                           LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _
                           MatchCase:=False, SearchFormat:=False)
    Set COUNTRYCODE = .Rows(3).Find(What:="COUNTRYCODE", LookIn:=xlFormulas, _
                           LookAt:=xlWhole, searchorder:=xlByRows, searchdirection:=xlNext, _
                           MatchCase:=False, SearchFormat:=False)

    If Not COUNTRY Is Nothing And Not COUNTRYCODE Is Nothing And Not ENGINE Is Nothing Then  'avoids error if text not found
        LastItemRow = .Cells(Rows.Count, "A").End(xlUp).Row
        For pointer = 4 To LastItemRow
            If .Cells(pointer, ENGINE.Column) <> "" Then
                .Cells(pointer, COUNTRYCODE.Column).Value = Left(.Cells(pointer, COUNTRY.Column), 3)
            End If
        Next pointer
    End If
End With

End Sub
于 2017-10-19T09:42:21.050 回答