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
4

1 回答 1

1

要确定 Range 是否有隐藏单元格,我会检查每行/列的高度/宽度是否不为零:

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
于 2016-04-05T14:59:40.800 回答