[英]Populating values with IF condition and headers
So here's my situation. 这就是我的情况。 See the picture for better understanding.
请参阅图片以获得更好的理解。 Example table
示例表
So what I'm trying to do is that, IF cell in columnn C isn't empty, the excel will populate first 3 letters of Column B to columnn D. I used following macro to do so: 所以我想做的是, 如果 C列中的单元格不为空,则excel会将B列的前3个字母填充到D列。我使用了以下宏:
Sub FillCountryCode()
Sheets("Sheet1").Range("D2:D20").Formula = "=IF(C2 <> """", LEFT(B2,3), """")"
End Sub
So I've 2 problems with this solution. 因此,此解决方案有2个问题。
1st) If I move the Country Code column, let's say to column F, the macro won't work anymore as it doesn't realize the column has been moved. 1)如果我将“ 国家/地区代码”列移动到F列,则该宏将不再起作用,因为它没有意识到该列已被移动。 So what's the easiest way to change macro to work so that it searches the right columns according to header name (for example Country Code ) and then goes trough all the rows (in my actual excel file there are hundreds of rows, when example table only has 8).
那么更改宏以使其工作的最简单方法是,它根据标题名称 (例如Country Code )搜索正确的列,然后遍历所有行(在我的实际excel文件中有数百行,仅当使用示例表时)有8)。 So it doesn't actually matter, in which column the relevant headers and cells for macro are located.
因此,与宏相关的标头和单元格位于哪一列中实际上并不重要。
2nd) At the moment I'm manually determining the range for macro, what's the correct command to make macro check all the rows in file. 2)目前,我正在手动确定宏的范围,使宏检查文件中所有行的正确命令是什么? (All the rows have values in Car Brand and Country )
(所有行在“ 汽车品牌”和“ 国家/地区”中都有值)
Here's the solution I came up, when trying to solve this. 这是我尝试解决此问题时提出的解决方案。
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
When trying to run this solution, there's some problem in the IF-condition, but I don't understand what is it. 尝试运行此解决方案时,IF条件存在一些问题,但我不知道这是什么。 Can some one help me?
有人能帮我吗? THe error I get is: Error 438: Object doesn't support this property or method.
我得到的错误是:错误438:对象不支持此属性或方法。
I think you deserve the points as you have done most of the work. 我认为您已经完成了大部分工作,因此应该得到这些要点。 I'm just posting your code with a few amendments to make it slightly more efficient, and avoid error messages popping up.
我只是在发布您的代码时对其进行一些修改,以使其效率更高一些,并避免出现错误消息。 I've added a few comments to explain the changes
我添加了一些评论来解释更改
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.