2016-07-15 66 views
0

我在以下線程中使用Siddharth Rout的代碼來大寫所選列的大寫,但當我在具有單元格的列上使用它時遇到錯誤'13'MISMATCH公式在一些範圍內。Excel VBA - 利用公式在列中大寫所有選定的單元格

Excel VBA - Capitalizing all selected cells in column on double click

下面是基於非公式列數據的工作從上面的鏈接代碼:

Sub ChangeToUpper() 
    Dim rng As Range 

    '~~> Check if what the user selected is a valid range 
    If TypeName(Selection) <> "Range" Then 
     MsgBox "Select a range first." 
     Exit Sub 
    End If 

    Set rng = Selection 

    rng = WorksheetFunction.Transpose(Split(UCase(Join(_ 
      WorksheetFunction.Transpose(rng), vbBack)), vbBack)) 
End Sub 

我搜索了論壇,並沒有發現與此相關的細節。所以我GOOGLE了它,並且Mr.Excel有這樣的代碼,但仍然給出錯誤'13',當我清除錯誤信息時,所有內容都被大寫。有沒有辦法消除這個錯誤?

下面是從Mr.Excel的代碼:

Sub MyUpperCase() 

    Application.ScreenUpdating = False 

    Dim cell As Range 
    For Each cell In Range("$A$1:" & Range("$A$1").SpecialCells(xlLastCell).Address) 
     If Len(cell) > 0 Then cell = UCase(cell) 
    Next cell 

    Application.ScreenUpdating = True 

End Sub 
+0

你需要配方嗎?這聽起來像添加一行代碼將公式轉換爲值是最簡單的解決方案? – StevenWalker

+0

我會將此視爲一種可能的修復方法,謝謝。 –

+0

訪問使用的範圍,將其全部複製,然後粘貼特殊值。我記不清頭頂上的確切語法,並且在移動設備上,但您可以很容易地找到示例Google搜索。已發佈的其他解決方案更可取,但可以保留公式 – StevenWalker

回答

1

檢查如果細胞具有式和或錯誤,如果是,則忽略。

Sub MyUpperCase() 

    Application.ScreenUpdating = False 

    Dim cell As Range 
    For Each cell In Range("$A$1:" & Range("$A$1").SpecialCells(xlLastCell).Address) 

     '/ Exclude errors 
     If Not IsError(cell) Then 
     If Len(cell) > 0 And Not cell.HasFormula Then 
      cell = UCase(cell) 
     End If 
    End If 
    Next cell 

    Application.ScreenUpdating = True 
End Sub 
+0

此更改仍導致錯誤「13」。再次清除錯誤,整張紙被大寫。它似乎只在具有這種情況的公式表上發生。感謝您的幫助,我將不得不尋找解決辦法。 –

+0

你的牢房有錯誤嗎?如果是,則檢查它們並使用「IsError」進行排除。我已經更新了答案。 – cyboashu

+0

不,沒有錯誤。謝謝你的提示。 –

相關問題