简体   繁体   English

转置可见细胞直到最后一列

[英]Transpose visible cells till last column

I've an excel sheet which basically will have a variable numbers in Columns A till C, it gets filtered to unique values based on for next loop I've achieved this and next I'm trying to copy the visible range starting from column F till last column (since variable columns each time when filters) and transpose it vertically in new sheet.The approach that I've used is counting each visible row and copy till end. 我有一个excel工作表,基本上在A列到C列中都有一个可变数字,它会根据for next循环的实现被过滤为唯一值,我已经做到了这一点,接下来我尝试从F列开始复制可见范围直到last column (每次过滤时都是可变列),然后将其垂直转置到新工作表中。我使用的方法是对每个可见行进行计数并复制到最后。 Here's the code. 这是代码。

Set ws = ActiveSheet
Set WS2 = ThisWorkbook.Sheets("3")
L2 = ws.Cells(Rows.Count, 1).End(xlUp).Row
For Each CELL In ws.Range("F2:F" & L2).SpecialCells(xlCellTypeVisible)
    i = CELL.Row
    L3 = ws.Cells(i, Columns.Count).End(xlToLeft).Column
    ws.Range(Cells(i, 6), Cells(i, L3)).Copy
    L4 = WS2.Cells(Rows.Count, 4).End(xlUp).Row
    WS2.Cells(L4 + 1, 4).PasteSpecial Paste:=xlPasteAll, Transpose:=True
Next CELL

过滤范围

But is there any alternate way to copy and transpose cells that have values from Column F till last column?In the above example starting from F108:H110 select and copy only cells that have values in it. 但是,是否还有其他方法可以复制和转置从F列到最后一列具有值的单元格?在上面的示例中,从F108:H110选择并仅复制其中具有值的单元格。

SpecialCells is a member of Range returns a range Object. SpecialCells是Range的成员,返回范围Object。 Knowing that we can chain them together .SpecialCells(xlCellTypeVisible).SpecialCells(xlCellTypeConstants) to narrow our range. 知道我们可以将它们链接在一起.SpecialCells(xlCellTypeVisible).SpecialCells(xlCellTypeConstants)可以缩小范围。 This will give you a non-continuous range. 这将为您提供不连续的范围。 You cannot use the copy command with a Non-continuous. 您不能将复制命令与不连续一起使用。 If you assign it to an array, the array will only be partially populated. 如果将其分配给数组,则只会部分填充该数组。 You must iterate over it with a For Each loop. 您必须使用For Each循环对其进行迭代。

Sub SelectVisibleNonBlankCells()
    Dim c As Range, r As Range
    Dim L2 As Long

    With ThisWorkbook.Sheets("3")

       L2 = .Cells(Rows.Count, 1).End(xlUp).Row
       Set r = .Range("F2:F" & L2).SpecialCells(xlCellTypeVisible).SpecialCells(xlCellTypeConstants)

    End With

    For Each c In r

    Next

End Sub

I would just iterate over the all the rows checking for visibility. 我只是遍历所有行以检查可见性。 Next just add the data to an an array and use range resize to fill the destination range. 接下来,只需将数据添加到数组中,然后使用范围调整大小来填充目标范围。

Sub TransposeVisibleCells() With ThisWorkbook.Sheets("3") Dim ColumnCount As Integer, lastRow As Long, RowCount As Long, x As Long, y As Long Dim arData lastRow = .Cells(Rows.Count, 1).End(xlUp).Row ColumnCount = .Cells(1, Columns.Count).End(xlToLeft).Column ReDim arData(ColumnCount, RowCount) For x = 2 To lastRow If Not .Rows(x).Hidden Then ReDim Preserve arData(ColumnCount, RowCount) For y = 1 To ColumnCount arData(y -1, RowCount) = .Cells(x, y).Value Next RowCount = RowCount + 1 End If Next End With Worksheets("Transposed Data").Range("A1").Resize(ColumnCount, RowCount) = arData End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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