VBAreplace关键字macros

我只需要把这个macros下载到我的Excel表格中。

基本上我有一个macros访问,我需要重复每个领域,我必须应用它。 我希望做一个macros的macros,使我的访问项目的macros,并保存自己很多打字。

我需要这个命令来replace关键字我有“0”和“9”单元格B2会去所有的“0”和B3会去所有的“9”,那么我会重复它的按照列C2和C3,D2和D3 ….等,但现在我已经在它只是在列B

但是我不断收到一个“运行时间450”:错误的参数或无效的属性分配“的错误,无论我做什么。

Sub replace() selection.find.clearformating selection.find.Replacement.clearformating With selection.find .Text = "0" .Replacement.Text = Worksheets("Sheet1").Range("B2") .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With selection.find.Execute replace:=wdReplaceAll End Sub 

这就是我现在所拥有的。

我的vba代码在访问im计划更换关键字是这个模板:

  Private Sub Form_Current() DoEvents If Me!0 = "-1" Then Me!9.Visible = True Else Me!9.Visible = False End If End Sub Private Sub 0_Click() DoEvents If Me!0 = "-1" Then Me!9.Visible = True Else Me!9.Visible = False End If End Sub 

所以让我知道如果你有任何想法来修复Excelmacros,并保存我很多打字。 我有大约300多个字段来编写代码。

OK JC这应该让你去。

这个例子的假设是:

..你正在改变的数据被列在名为“数据”

replace关键字存储在名为“Sheet1”的工作表中

..数据关键字(“0”和“9”)作为文本值存储在图表数据中 – 您将需要处理多位数字,即“0”将被replace为“0”,“10” ,“100”等

..根据您的Q值,列号代表要被replace的数据列和列以及该列replace值所在的列

没有保证,这将在Access VBA运行,但你应该得到一个办法的想法。

 'This is the calling sub to iterate through your data Sub callsub() Dim startcol As Long, endcol As Long, c As Long startcol = 2 'col B endcol = 4 'col D For c = startcol To endcol Call replace(c) 'replace keywords in Col no c Next c End Sub 'This Sub will replace two keyword values per call in the column (number) passed to it Sub replace(col) With Sheets("Data") repstr0 = Worksheets("Sheet1").Cells(2, col) .Columns(col).replace What:="0", Replacement:=repstr0, SearchOrder:=xlByColumns, MatchCase:=True repstr9 = Worksheets("Sheet1").Cells(3, col) .Columns(col).replace What:="9", Replacement:=repstr9, SearchOrder:=xlByColumns, MatchCase:=True End With End Sub 

有关使用Range.Replace方法的数据可以在这里find

你并没有明确指出哪一行代码导致你得到的错误(如果你很难识别它,请查看“VBAerror handling”),但我相信这一点:

 .clearformating 

是一个问题。 如果在代码中键入的内容与您的代码完全相同,那么Microsoft在命名“clear formatting”方法时发生了错字,或者您调用了不存在的方法。

另外,整个search取决于实际Selection内容 – 这可能不会做你期望的事情,在你期待的地方。

首先要做的,就是准确地找出哪些指令正在炸毁。 当VBAdebugging对话框出现时,单击[debugging]button,非法指令应以黄色突出显示。