2016-04-06 128 views
0

我使用宏來檢查我在Excel中創建的發票之前保存它。目前,它拼寫檢查整個工作簿。我想拼寫檢查特定工作表的certian單元格(因爲我不想拼寫檢查發票上的人名和地址)。VBA拼寫檢查Excel中的特定單元格

具體來說,我想拼寫檢查只有細胞D15-D19上表「發票」和細胞D38上表「安全檢查」

有人能指出我在正確的方向?

這是我目前使用的是我希望優化VBA代碼:

Option Explicit 
Sub SaveAsSafety() 
'Saves filename as value of A1 plus the current date 


Application.CommandBars("Tools").Controls("Spelling...").Execute 
'Invokes spell check 

Dim newFile As String, fName As String 
' Don't use "/" in date, invalid syntax 
fName = Range("M9").Value 
'Change the date format to whatever you'd like, but make sure it's in quotes 
newFile = fName & " - " & Range("D10").Value & " - " & Range("D9") 
' Change directory to suit your PC, including USER NAME 

Sheets(Array("Safety Inspection", "Invoice")).Select 
Sheets("Safety Inspection").Activate 

ChDir _ 
"C:\Users\Brian\Google Drive\Buller Heating and Air\Invoices" 
ActiveWorkbook.SaveAs Filename:=newFile 
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("A1").Value _ 
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _ 
:=False, OpenAfterPublish:=False 

End Sub 
+0

你幾乎沒有。你已經有了正確的表單數組,你可以添加一個範圍地址數組,然後同時迭代它們中的兩個,收集表單和相應的範圍,在這個範圍內你的法術例程(你沒有包括在上面代碼) – user3598756

回答

1

要在特定細胞檢查拼寫嘗試:

Sub bbuller() 
    Dim boo As Boolean 

    s = Array("Invoice!D15", "Invoice!D16", "Invoice!D17", "Invoice!D18", "Invoice!D19", "'Safety Inspection'!D38") 
    For Each ss In s 
     boo = CheckItNew(Range(ss)) 
     If boo Then 
      MsgBox ss & " has no errors" 
     Else 
      MsgBox ss & " has errors" 
     End If 
    Next ss 
End Sub 

Public Function CheckItNew(r As Range) As Boolean 
    Dim MyText As String 
    MyText = r(1).Text 
    Dim oxlAp As Object 
    Set oxlAp = CreateObject("Excel.Application") 
    CheckItNew = oxlAp.CheckSpelling(MyText) 
    oxlAp.Quit 
    Set oxlAp = Nothing 
End Function 
+0

我學到了一些新東西(再一次!)。我會補充說,因爲OP的環境是Excel,所以可以避免使用'CheckItNew'函數並直接使用If Application.CheckSpelling(Range(ss)(1).Text)然後' – user3598756