![](/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.