使用IF条件和标题填充值

时间:2017-10-19 07:10:59

标签: excel vba excel-vba if-statement auto-populating

所以这是我的情况。请参阅图片以便更好地理解。 Example table

所以我试图做的是, IF 列C中的单元格不为空,excel将填充B列的前3个字母到列D.使用以下宏来执行此操作:

Sub FillCountryCode()

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

End Sub

所以我解决了这个问题的2个问题。

1st)如果我移动国家/地区代码列,让我们对F栏说,宏不再工作,因为它没有意识到该列已经被感动了那么,将宏更改为最简单的工作方式是什么,以便根据标题名称搜索正确的列(例如国家/地区代码)然后通过所有列进行搜索行(在我的实际excel文件中有数百行,当示例表只有8行时)。因此,实际上并不重要,宏的相关标题和单元格位于哪一列。

第二)目前我手动确定宏的范围,正确的命令是什么使宏检查文件中的所有行。 (所有行的值均为汽车品牌国家/地区

这是解决这个问题时我提出的解决方案。

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:对象不支持此属性或方法。

1 个答案:

答案 0 :(得分: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