2016-04-05 11 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

グレート提案を、非常に簡単。それはあなたが記述した通りに正確に働いた。答えをありがとう。 –

関連する問題