![](/img/trans.png)
[英]Error in my VBA Selection.SpecialCells(xlCellTypeConstants, 1) cell.Value = cell.Text
[英]SpecialCells(xlCellTypeConstants) results unexpected result on just one cell as the base range
總結一下:如果SpecialCells的基本范圍只是一個單元格,則range .SpecialCells(xlCellTypeConstants,xlNumbers)的結果不僅生成該單元格,也不生成任何內容。 我猜想,SpecialCells應該在要應用的對象內產生一個范圍...還是我錯了?
我已經“開發”了一些新的excel書籍,只是為了顯示我不明白的內容,如下所示:
創建了一本新的Excel書
填充A1:B3,如下所示:
AB ... 1 1 12 2 2 22 3 3 32 .
表格中的所有其他單元格都保持不變。
在一個新模塊中,我添加了以下代碼:
Private Sub test() Dim oSet As Range, oSpec As Range, oUsed As Range Worksheets("Sheet1").Activate Set oSet = ActiveSheet.Range("A1:A1") Set oSpec = oSet.SpecialCells(xlCellTypeConstants, xlNumbers) Set oUsed = ActiveSheet.UsedRange Set oSet = Nothing Set oSpec = Nothing Set oUsed = Nothing End Sub
運行該子例程,並在第一次范圍重置時將其停止,它將產生:
oSet.Address = "$A$1" oSpec.Address = "$A$1:$B$3" ' this seems to be wrong; should it be just "$A$1" ? oUsed.Address = "$A$1:$B$3"
將A1的值更改為A,重新運行子項,然后在同一位置停止,它給出了(與前一個一致,這表明它可以正常工作):
oSet.Address = "$A$1" oSpec.Address = "$B$1","$A$2:$B$3" ' this seems to be wrong; should oSpec be nothing ? oUsed.Address = "$A$1:$B$3"
但是,將A1的值重置為原始1,但是將子例程中第一個Set操作的范圍從僅單元格“ A1”更改為單元格“ A1:A2”的真實范圍,然后重新運行該子級並停止在這個地方,它給出了截然不同的結果(並且還有更多預期):
oSet.Address = "$A$1:$A$2" oSpec.Address = "$A$1:$A$2" ' this is good oUsed.Address = "$A$1:$B$3"
如果有人可以解釋結果,我將不勝感激。 謝謝你
發生這種情況是因為在一個單元格的情況下,它考慮使用UsedRange。
參考文獻:
1) 超級秘密SpecialCells
2) 在Excel VBA中使用SpecialCells
作為有用的注釋,我將為您提供SpecialCells的包裝函數及其輔助方法(在C#中,在VBA中):
/// <summary>
/// <para>Wrapper for SpecialCells function.</para>
/// </summary>
/// <param name="inputRange"></param>
/// <param name="cellType"></param>
/// <returns></returns>
/// <remarks>Throws null when there are no cells in <paramref name="inputRange"/> corresponding to <paramref name="cellType"/>, unlike SpecialCells which throws exception</remarks>
public static Range GetRangeSpecialCells(this Microsoft.Office.Interop.Excel.Range inputRange, XlCellType cellType)
{
try
{
if (inputRange.Cells.Count == 1)
{
if (cellType == XlCellType.xlCellTypeComments)
{
if (inputRange.Comment != null)
{
return inputRange;
}
else
{
return null;
}
}
else if (cellType == XlCellType.xlCellTypeFormulas)
{
if (inputRange.HasFormula == true)
{
return inputRange;
}
else
{
return null;
}
}
else if (cellType == XlCellType.xlCellTypeBlanks)
{
if (string.IsNullOrEmpty(inputRange.Value2) == true)
{
return inputRange;
}
else
{
return null;
}
}
else if (cellType == XlCellType.xlCellTypeLastCell)
{
return inputRange;
}
else
{
// since inputRange has a single cell, SpecialCells will apply to entire worksheet
// this range has all cells from worksheet (UsedRange) of type provided: cellType
Range temp = inputRange.SpecialCells(cellType);
// intersect range with single cell (inputRange) with above range
Range rangeOverlap = Intersect(inputRange, temp);
// if range with single cell is contained in that intersection, then this cell is of type xlCellTypeConstants
if (rangeOverlap.Count == inputRange.Count && rangeOverlap.Rows.Count == inputRange.Rows.Count && rangeOverlap.Columns.Count == inputRange.Columns.Count)
{
return inputRange;
}
else
{
return null;
}
}
}
else
{
return inputRange.SpecialCells(cellType);
}
}
catch (System.Runtime.InteropServices.COMException ex)
{
return null;
}
}
/// <summary>
/// <para>Customized function for intersection of two ranges (<paramref name="rangeA"/> ∩ <paramref name="rangeB"/>)</para>
/// </summary>
/// <param name="rangeA"></param>
/// <param name="rangeB"></param>
/// <returns>Range corresponding to intersection of the two provided ranges</returns>
/// <remarks>This function returns null if any of provided ranges is null or malformed, unlike Application.Intersect which throws exception</remarks>
public static Range Intersect(Range rangeA, Range rangeB)
{
Range rngIntersect;
if (rangeA == null)
{
rngIntersect = null;
}
else if (rangeB == null)
{
rngIntersect = null;
}
else if (rangeA.Worksheet != rangeB.Worksheet)
{
rngIntersect = null;
}
else
{
try
{
rngIntersect = Globals.ThisAddIn.Application.Intersect(rangeA, rangeB);
}
catch (Exception ex)
{
rngIntersect = null;
}
}
return rngIntersect;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.