我有以下Excel宏:
Rem Attribute VBA_ModuleType=VBAModule
Sub FixPlatforms()
Dim fndList As Object
Set fndList = CreateObject("Scripting.Dictionary")
fndList.Add "3DO Interactive Multiplayer", "3DO"
fndList.Add "Nintendo 3DS", "3DS"
fndList.Add "Ajax", "AJAX"
fndList.Add "Xerox Alto", "ALTO"
fndList.Add "Amiga CD32", "AMI32"
fndList.Add "Amiga", "AMI"
fndList.Add "Apple I", "APPI"
fndList.Add "Apple IIe", "APPIIE"
fndList.Add "Apple IIGS", "APPGS"
fndList.Add "Apple II Plus", "APPII+"
fndList.Add "Apple II series", "APPII"
fndList.Add "Apple II", "APPII"
For Each sht In ActiveWorkbook.Worksheets
For Each strKey In fndList.Keys()
sht.Cells.Replace What:=strKey, Replacement:=fndList(strKey), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next strKey
Next sht
End Sub
它运行正常,但我如何才能使它只在单个工作表中的单个列上运行? (将其限制为选择也可以,我猜。)
谢谢!
答案 0 :(得分:1)
如何制作它以便它只在单个列上运行
如果您只想在工作表中使用单个Column
而不是工作表中的所有Cells
,请使用(例如)Columns(4).Replace
代替Cells.Replace
在单个工作表中
如果您不想处理Worksheets
中的所有ActiveWorkbook
,而只想处理(例如)ActiveWorkbook.Worksheets("specific_worksheet")
,请删除您添加到自己的循环在Each
中处理ActiveWorkbook.Worksheets
工作表的代码,然后使用ActiveWorkbook.Worksheets("specific_worksheet")
代替您的循环对象(sht
)。
将其限制为选择也没关系
如果您只想在当前Replace
上执行Selection
,请使用Selection.Replace
答案 1 :(得分:1)
只需将上述评论合并到一个答案中(每个this元话题):
Sub FixPlatforms()
Dim fndList As Object
Set fndList = CreateObject("Scripting.Dictionary")
fndList.Add "3DO Interactive Multiplayer", "3DO"
fndList.Add "Nintendo 3DS", "3DS"
fndList.Add "Ajax", "AJAX"
fndList.Add "Xerox Alto", "ALTO"
fndList.Add "Amiga CD32", "AMI32"
fndList.Add "Amiga", "AMI"
fndList.Add "Apple I", "APPI"
fndList.Add "Apple IIe", "APPIIE"
fndList.Add "Apple IIGS", "APPGS"
fndList.Add "Apple II Plus", "APPII+"
fndList.Add "Apple II series", "APPII"
fndList.Add "Apple II", "APPII"
For Each strKey In fndList.Keys()
ActiveSheet.Columns(1).Replace What:=strKey, Replacement:=fndList(strKey), _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
SearchFormat:=False, ReplaceFormat:=False
Next strKey
End Sub
当然,只需将列(1)调整为您希望替换的列。您还可以在Range对象上使用.EntireColumn
选择列,如:
ActiveSheet.Range("A1").EntireColumn.Replace ...
如果您只是希望它通过Selection工作,那么使用Selection
对象(也是Range类型的对象):
Selection.Replace ...