我有一张非常大的工作表,约有 150 列,其中大部分包含公式。当我想对输入到不使用公式的单元格的数据进行排序时,它会弄乱整个工作表。 - 输入单元格不在一起
目前,我在 VBA 中的解决方案是将单元格复制到另一个(隐藏)工作表,排序并将其全部放回去。我只是觉得对于一个看似简单的任务来说这工作量太大了。有更聪明的方法来解决这个问题吗?
EDIT
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.
您可以使用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
(if targetRange
没有细胞,On Error Resume Next
会抓住它 -> 这就是我添加这一行的原因;SpecialCells
有这个小限制)。你不需要任何.Select
您所包含的电话。大概是什么引起了错误.Sort
is Sheets("Input").Range("A3")
我已将其替换为第一个单元格targetRange
(不管是什么)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)