0

150 列までの非常に大きなシートがあり、そのほとんどに数式が含まれています。数式を使用していないセルに入力したデータを並べ替えたい場合、シート全体がめちゃくちゃになります。- 入力セルが一緒ではありません

現在、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.

4

1 に答える 1

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、数式を含まないセルのみが含まれます。

アップデート:

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 0targetRange.Cells.counttargetRangeセルがない場合はOn Error Resume Nextそれをキャッチします->そのため、この行を含めました。SpecialCellsこの小さな制限があります)。.Select含めている呼び出しも必要ありません。そして、おそらくエラーを引き起こしているのは.Sort、(それが何であれ)Sheets("Input").Range("A3")の最初のセルに置き換えたものです。targetRange

于 2013-09-27T14:23:18.367 に答える