简体   繁体   中英

Concatenate using vba - visible cells only?

I'm looking to concatenate a number of cells from one sheet into another and then copy that formula down to the last row.

However the sheet that I'm looking to concatenate from has hidden cells. So currently this is messing up my results on the target sheet.

Is there a way to fix this so it only picks up visible cells?

Dim FitRng As Range, Lastrowteam As Long

Lastrowteam = Cells(Rows.Count, "H").End(xlUp).Row

Sheets("Pipeline simplified").Select
Range("W7").FormulaR1C1 = _
"=CONCATENATE(Pipeline!R[2]C[7],"" "",Pipeline!R[2]C[8],"" "",Pipeline!R[2]C[9],"" "",Pipeline!R[2]C[10])"

Set FitRng = Range("W7:W" & Lastrowteam).SpecialCells(xlCellTypeVisible)
FitRng.FillDown

Something like this should work for you based on code provided in your question:

Sub tgr()

    Dim wb As Workbook
    Dim wsData As Worksheet
    Dim wsDest As Worksheet
    Dim rData As Range
    Dim rDest As Range
    Dim DataCell As Range
    Dim aResults() As String
    Dim i As Long

    Set wb = ActiveWorkbook
    Set wsData = wb.Worksheets("Pipeline Simplified")   'Sheet where you are getting the concatenated data
    Set wsDest = wb.Worksheets("Sheet1")                'Sheet where the results will be output
    Set rDest = wsDest.Range("A2")                      'Cell on destination sheet where output results will start

    On Error Resume Next
    Set rData = wsData.Range("G2", wsData.Cells(wsData.Rows.Count, "G").End(xlUp)).SpecialCells(xlCellTypeVisible)
    On Error GoTo 0
    If rData Is Nothing Then Exit Sub   'No visible cells
    If rData.Row < 2 Then Exit Sub      'No data

    ReDim aResults(1 To rData.Cells.Count, 1 To 1)
    For Each DataCell In rData.Cells
        i = i + 1
        aResults(i, 1) = DataCell.Value & " " & DataCell.Offset(, 1).Value & " " & DataCell.Offset(, 2).Value & " " & DataCell.Offset(, 3).Value
    Next DataCell

    rDest.Resize(UBound(aResults, 1)).Value = aResults

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM