2014-11-06 39 views
3

我正在嘗試創建一個指向包含公式或常量的所有單元格的命名區域。但我得到的是與集合R =聯盟(...創建包含常量或公式的所有單元格的命名範圍?

我怎樣才能得到這個工作?

Dim r As Range 

Set r = Union(Sheet1.Cells.SpecialCells(xlCellTypeConstants), Sheet1.Cells.SpecialCells(xlCellTypeFormulas), _ 
     Sheet22.Cells.SpecialCells(xlCellTypeConstants), Sheet22.Cells.SpecialCells(xlCellTypeFormulas)) 
+1

不'Union'與RA工作更換多張牀單? – 2014-11-06 13:58:50

+0

這似乎是問題所在。謝謝! – user1283776 2014-11-06 14:05:28

回答

4

Union只與在同一表範圍的工作開始於該行的錯誤信息你可以建立地址的集合這樣雖然

Sub Main() 

    Dim arr As Variant 

    arr = Array(_ 
       GetAddresses(Sheet1, xlCellTypeConstants), _ 
       GetAddresses(Sheet1, xlCellTypeFormulas), _ 
       GetAddresses(Sheet2, xlCellTypeConstants), _ 
       GetAddresses(Sheet2, xlCellTypeFormulas) _ 
       ) 

    Dim r As Variant 
    For Each r In arr 
     If Len(r) > 0 Then Debug.Print r 
    Next 

End Sub 

Function GetAddresses(sh As Worksheet, cellType As XlCellType) As String 
    On Error Resume Next 
    GetAddresses = sh.Name & "!" & sh.Cells.SpecialCells(cellType).Address 
    On Error GoTo 0 
End Function 

如果你需要不同的方式處理錯誤,看看this answer

+3

++很好完成! Imp建議:當使用'SpecialCells'時,它處理錯誤:)) – 2014-11-06 14:26:14

+0

@SiddharthRout有效點:) – 2014-11-06 14:27:39

相關問題