gpt4 book ai didi

Excel vba 宏在 2007 年出现类型不匹配错误,但在 2010 年有效

转载 作者:行者123 更新时间:2023-12-02 09:43:48 25 4
gpt4 key购买 nike

我在 Windows 7 上的 Excel 2010 中制作并测试了此宏,还使用另一台 Windows 7 计算机(但使用 Excel 2007)进行了测试。在两台计算机上都可以工作,但是当我尝试在我的工作计算机(Windows 7、Excel 2007)上使用它时我在第一个“Next”语句中收到“类型不匹配错误”。查找后发现我可以使用“Exit For”而不是“next”,但它只是提示包含“End If”的下一行。现在它声称“End If without block If”。我想我只是无法理解这在一台 Win7\Excel 2007 计算机上如何工作,但在另一台计算机上却不行。

该宏只是搜索电子邮件主题中所选单元格的值(如果单元格尚未着色),如果匹配,则会更改单元格的颜色。

Sub MultipleCellSubjectSearch()

'This macro searches for the selected cell values (if there is no cell color), when it finds a match it turns the cell color yellow

Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olItem As MailItem
Dim olInbox As Outlook.MAPIFolder
Dim olFolder As Outlook.MAPIFolder
Dim oCell As Range

'The following sets the Outlook folder to search
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
Set olInbox = olNamespace.GetDefaultFolder(olFolderInbox)

'The following searches for cell value string in subject
For Each oCell In Selection
If oCell.Interior.Pattern = xlNone Then
For Each olItem In olInbox.Items
If InStr(olItem.Subject, (oCell.Value)) <> 0 Then
oCell.Interior.ColorIndex = 6
End If
Next
End If
Next

Set olInbox = Nothing
Set olNamespace = Nothing
Set olApp = Nothing

End Sub

如果有人有任何想法,我们将不胜感激。

最佳答案

这里有一个想法 - 您的 olItem 被定义为 mailItem。当邮箱中的下一个项目不是邮件时,代码可能会失败吗?日历请求或其他原因会导致这种情况吗?您可能想在内部循环中放置一个 Debug.Print 语句来查看正在查看哪些对象 - 并查看循环是否确实执行,直到它在收件箱中遇到奇怪的项目...

作为一个快速修复,如果您允许它成为变体,则不会出现类型错误。所以你只需将其声明为

Dim olItem

没有作为mailItem

这是一个远景。

关于Excel vba 宏在 2007 年出现类型不匹配错误,但在 2010 年有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14887765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com