限制search和replacemacros在一个页面上的一列

我有以下的Excelmacros:

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 

它工作正常,但我如何使它只能在单个工作表中的单个列上运行? (限于select也可以,我猜。)

谢谢!

我怎样才能使它只在一列上运行

如果您只想操作工作表中的单个Column而不是工作表中的所有Cells ,请使用(例如) Columns(4).Replace而不是Cells.Replace

在一个工作表中

如果您不想处理ActiveWorkbook中的所有Worksheets ,而只想处理(例如) ActiveWorkbook.Worksheets("specific_worksheet")删除您添加到代码中的循环以处理ActiveWorkbook.Worksheets中的Each工作表,然后使用ActiveWorkbook.Worksheets("specific_worksheet")而不是您的循环对象( sht )。

限制它的select也可以

如果您只想在当前Selection上执行Replace ,请使用Selection.Replace

只要将上述评论合并为一个答案(按照这个元主题):

 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 

当然,只要将Columns(1)调整到想要replace的列就可以了。您还可以通过在Range对象上使用.EntireColumn来select列,例如:

 ActiveSheet.Range("A1").EntireColumn.Replace ... 

如果你只是希望它通过select工作,然后使用Selection对象(这也是一个types范围的对象):

 Selection.Replace ...