2016-04-05 93 views
0

在Excel中我有一个VBA函数返回由用户选择的单元格中文本的连接字符串。检查所选范围内的单元格是否可见

这可以按我的要求工作,但是如果在选择中存在隐藏的单元格,隐藏单元格的值将包含在内,这是不可取的。发生此问题的一个示例是过滤表。

有没有办法修改我的函数来检查正在读取的单元格是否可见?

Sub ConcatEmialAddresses() 

    Dim EmailAddresses As String 

    ActiveSheet.Range("C3").Value = combineSelected() 
    ActiveSheet.Range("C3").Select 

    Call MsgBox("The email address string from cell ""C3"" has been copied to your clipboard.", vbOKOnly, "Sit back, relax, it's all been taken care of...") 

End Sub 

Function combineSelected(Optional ByVal separator As String = "; ", _ 
         Optional ByVal copyText As Boolean = True) As String 

    Dim cellValue As Range 
    Dim outputText As String 

    For Each cellValue In Selection 
     outputText = outputText & cellValue & separator 
    Next cellValue 

    If Right(outputText, 2) = separator Then outputText = Left(outputText, Len(outputText) - 2) 

    combineSelected = outputText 

End Function 

回答

1

要确定范围有一个隐藏的电池,我会检查每一行/列的高度/宽度不为零:

Function HasHiddenCell(source As Range) As Boolean 
    Dim rg As Range 

    'check the columns 
    If VBA.IsNull(source.ColumnWidth) Then 
    For Each rg In source.Columns 
     If rg.ColumnWidth = 0 Then 
     HasHiddenCell = True 
     Exit Function 
     End If 
    Next 
    End If 

    ' check the rows 
    If VBA.IsNull(source.RowHeight) Then 
    For Each rg In source.rows 
     If rg.RowHeight = 0 Then 
     HasHiddenCell = True 
     Exit Function 
     End If 
    Next 
    End If 
End Function 

Sub UsageExample() 
    If HasHiddenCell(selection) Then 
    Debug.Print "A cell is hidden" 
    Else 
    Debug.Print "all cells are visible" 
    End If 
End Sub 
+0

伟大的建议,而且很容易。它完全按照您所描述的那样工作感谢你的回答。 –

相关问题