2013-09-27 32 views
0

我有一個非常大的工作表,大約有150列,其中大部分都包含公式。當我想將我輸入的數據排序到不使用公式的單元格時,會混淆整個表單。 - 輸入單元不在一起VBA僅對文本進行排序,忽略具有公式的單元格

目前,我在VBA中的解決方案是將單元複製到另一個(隱藏)表單中,並將其全部歸併。我只是覺得這對於一個看起來很簡單的任務來說太麻煩了。有沒有更聰明的方法來解決這個問題?

編輯

QUA @varocarbas我曾嘗試下面的代碼:

Private Sub sortInputButton_Click() 
    ' This sub sorts the inpur, without messing up the references [hopefully] 
    Dim rowCount As Integer 
    Dim colCount As Integer 

    rowCount = countItems 
    colCount = 180 

    Dim inputRange As Range 
    Set inputRange = Sheets("Input").Range("A3") 
    Set inputRange = inputRange.Resize(rowCount, colCount) 

    Dim targetRange As Range 

    On Error Resume Next ' If range is nothing, throws an error - we don't want that 
    Set targetRange = inputRange.SpecialCells(xlCellTypeConstants) 
    On Error GoTo 0 

    If targetRange.Cells.count > 0 Then 
     Sheets("Input").Select 
     targetRange.Select 
     Call targetRange.Sort(Sheets("Input").Range("A3"), xlAscending) 
    End If 

End Sub 

但是,這給我的錯誤the command you chose cannot be performed with multiple selections. Select a single range and click the command again.

回答

0

您可以使用SpecialCells

Dim targetRange As Range 
On Error Resume Next 'In case of not finding any cell under the requested conditions. 
Set targetRange = inputRange.SpecialCells(xlCellTypeConstants) 

inputRange包括所有單元格和targetRange只是不包含公式的單元格。

UPDATE:

Dim targetRange As Range 
On Error Resume Next ' If range is nothing, throws an error - we don't want that 
Set targetRange = inputRange.SpecialCells(xlCellTypeConstants) 
Call targetRange.Sort(targetRange.Cells(1, 1), xlAscending) 
'targetRange.Sort targetRange.Cells(1, 1), xlAscending -> alternative not requiring "Call" 

你不需要On Error GoTo 0,既不targetRange.Cells.count(如果targetRange沒有細胞,On Error Resume Next會抓住它 - >這就是爲什麼我包括這條線; SpecialCells有這種小的限制)。您不需要任何您包括的.Select電話。大概是在.Sort中引發錯誤的是Sheets("Input").Range("A3"),我用targetRange的第一個單元替換(不管它是什麼)。

+0

這似乎起初工作 - 但我發現它只在我輸入詳細信息之前有效。然後我得到這個錯誤:'你選擇的命令不能執行多個選擇。選擇一個範圍,然後再次點擊該命令。' 我有6個用戶輸入數據單元格,然後是20x'[formula] [input]',後面跟着更多。我的觀點是輸入和公式是混合的。 – Kentora

+0

@Kentora我不理解問題。你能否用你試過的代碼更新你的問題,以便我可以幫忙?這個解決方案應該沒有任何限制。 – varocarbas

+0

@Kentora另外,我包含了一個錯誤捕獲,因此無論如何它都不能輸出錯誤(在最壞的情況下它不會工作)。 – varocarbas

相關問題