我在Windows 7上的Excel 2010中製作並測試了此宏,也使用Excel 2007與另一臺Windows 7計算機一起測試過。在我的工作計算機上使用它(Windows 7,Excel 2007)在第一個「Next」語句中出現「類型不匹配錯誤」。看了看,發現我可以使用「退出」而不是「下一步」,但它只是抱怨包含「結束如果」的下一行。現在它宣稱「End If without Block If」。我想我只是不明白這是如何工作在一臺Win7 \ Excel 2007計算機上,而不是其他。Excel vba宏在2007年獲取類型不匹配錯誤,但在2010年工作
宏只是搜索電子郵件主題中所選單元格的值(如果單元格尚未着色),如果匹配,它會更改單元格的顏色。
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
如果有人有任何想法,他們將不勝感激。
難道你是在兩臺機器上使用不同版本的Outlook?你是否包含正確的參考?這更像是一個「環境」問題,而不是Excel中的差異,我懷疑...... – Floris 2013-02-15 04:49:18
順便說一句...... Exit For'是一個很好的方法,可以儘早打破循環 - 但您仍然需要一個「常規」結束你的循環。所以它不是「用'Exit For'替換'Next'的情況,而是更多:'爲...做東西......如果有東西退出......做更多的事情......下一個'。 ? – Floris 2013-02-15 04:52:32
這兩臺Excel 2007計算機都運行在Windows 7 Pro上的outlook 2007,兩者都包含完全相同的參考文獻,我想詳細說明一下,我需要找到一種方法在我的「公司」計算機上運行。我可以想象這是一些簡單的變化,我可以讓我忽略它 – user2074189 2013-02-15 05:42:48