VBA的宏显示“子过程或函数未定义”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VBA的宏显示“子过程或函数未定义”相关的知识,希望对你有一定的参考价值。
在网上找到一个用来检查文章中重复词的宏,但是在测试时提示子过程或函数未定义,不知道怎么修改,求个高手帮帮忙。
代码如下:
Sub FinReP()
Dim iRng As Range, jRng As Range
Dim i As Integer, j As Integer, o As Integer
Set iRng = ActiveDocument.Content
With iRng.Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(findtext:="<[!^13]@^13<[!^13]@^13", MatchWildcards:=True)
Set jRng = iRng.Duplicate
i = 1
For j = 0 To Len(iRng) - 1
Set jRng = Range(iRng.Start + j, iRng.Start + j + 1)
If jRng Like "[一-﨩]" And jRng.Font.Color = wdColorAutomatic Then
Set fRng = Range(jRng.End, iRng.End)
o = 0
Do While fRng.Find.Execute(findtext:=jRng, MatchWildcards:=False)
i = i + 1
jRng.Font.ColorIndex = i - o
fRng.Font.ColorIndex = i - o
fRng.SetRange fRng.End, iRng.End
o = o + 1
Loop
End If
Next
iRng.SetRange iRng.End, ActiveDocument.Content.End
Loop
End With
End Sub
2、对子过程(或函数)的定义不一致。
例如:子过程test1做在某个工作表中,设定的是private属性。然后在模块中有一个全局程序test要调用其这个子过程,那么就会出现这种错误。更改方法:将这个子过程设置成全局的(public),并放在模块中,这样就可以解决了。 参考技术A 代码放在 ThisDocument中不会报错,代码放在模块中就会报错,因为Range需要限定
所有的Range前要加上ActiveDocument.本回答被提问者采纳 参考技术B Sub Auto_Open()
ThisWorkbook.Application.Caption = APP_NAME_STR
CreateTableMenu
End Sub
Sub CreateTableMenu()
DeleteTableMenu
MenuBars(xlWorksheet).Menus.Add (MENU_REPORT_STR)
MenuBars(xlWorksheet).Menus(MENU_REPORT_STR).MenuItems.Add Caption:=MENU_REPORT_HISTDATA_SEARCH_STR, OnAction:="FormShow"
End Sub
Sub DeleteTableMenu()
On Error Resume Next
MenuBars(xlWorksheet).Menus(MENU_REPORT_STR).MenuItems(MENU_REPORT_HISTDATA_SEARCH_STR).Delete
On Error Resume Next
MenuBars(xlWorksheet).Menus(MENU_REPORT_STR).Delete
On Error Resume Next
End Sub
Sub FormShow()
UserForm2.Show
End Sub
Function ChangeDate(ByVal OldDate As String) As String
Dim DateLen, i As Long
Dim ss, EndDate As String
Dim mas As Integer
EndDate = ""
DateLen = Len(OldDate)
For i = 0 To DateLen - 1
ss = Mid$(OldDate, i + 1, 1)
mas = Asc(ss)
If mas <> 0 Then
EndDate = EndDate + ss
End If
Next i
ChangeDate = EndDate
End Function
Function GetVBString(ByVal OldStr As String) As String
Dim nLen, i As Long
Dim ss, strRet As String
Dim mas As Integer
Dim nSpacePos As Integer
nLen = Len(OldStr)
nSpacePos = nLen
For i = 0 To nLen - 1
ss = Mid$(OldStr, i + 1, 1)
mas = Asc(ss)
If mas = 0 Then
nSpacePos = i
Exit For
End If
Next i
GetVBString = Left(OldStr, nSpacePos)
End Function
以上是关于VBA的宏显示“子过程或函数未定义”的主要内容,如果未能解决你的问题,请参考以下文章