【问题标题】:Running through a column in excel and removing certain text based on a vlookup [closed]在excel中运行一列并根据vlookup删除某些文本[关闭]
【发布时间】:2017-11-16 19:23:07
【问题描述】:
如果这是微不足道的,我提前道歉,但我很难记住 Visual Basic 的语法,因为它已经很长时间了。
我在 D 列的单元格中有需要删除的文本(国家名称)。我有一张单独的表格,其中包含所有国家/地区名称,因此我认为我可以逐个单元格地遍历列,并检查是否有任何单元格的文本与我在单独的表格中的国家表的文本相匹配。 (即,如果文本与“deletME”相匹配,则会更改文本的 vlookup)
谢谢!
【问题讨论】:
标签:
vba
excel
loops
vlookup
【解决方案1】:
这是一个使用数组而不是范围的简单解决方案。只需根据需要将国家/地区名称插入数组即可:
Sub deletingcountries()
Dim sht As Worksheet
Dim i As Long
Dim lastrow As Integer
Set sht = ActiveWorkbook.Worksheets("Sheet1")
lastrow = sht.Cells(sht.Rows.Count, "D").End(xlUp).Row
Dim countries As Variant, country As Variant
countries = Array("Country1", "Country2", "Country3", "Country4", "Country5") 'add however many countries necessary
For i = 1 To lastrow
For Each country In countries
If InStr(ActiveWorkbook.Worksheets("Sheet1").Range("D" & i).Value, country) Then
ActiveWorkbook.Worksheets("Sheet1").Range("D" & i).Value = "deletME"
End If
Next country
Next i
End Sub