【问题标题】:Stack different columns into one column on a different worksheet将不同的列堆叠成不同工作表上的一列
【发布时间】:2019-12-18 03:08:03
【问题描述】:

我想将从 C5 开始的所有填充单元格复制到不同工作表的 F 列。

我提到了另一个帖子:Excel - Combine multiple columns into one column 根据我的需要修改了代码。

    Sub CombineColumns()
Dim Range1 As Range, iCol As Long, Range2 As Range, Check As Range, wks As Worksheets

Set Range1 = wks("T(M)").Range(Cells(5, 3), Cells(Cells(5, 3).End(xlDown).Row, Cells(5, 3).End(xlToRight).Column))
Set Check = wks("csv").Range("F1")
If IsEmpty(Check.Value) = True Then
Set Range2 = Check
Else
LastRow = wks("csv").Range("F" & Rows.Count).End(xlUp).Row
Set Range2 = wks("csv").Cells(LastRow, 6).Offset(1, 0)
End If
For iCol = 3 To Range1.Columns.Count
    wks("T(M)").Range(Cells(5, iCol), Cells(Range1.Columns(iCol).Rows.Count, iCol)).Copy
    wks("csv").Range2.PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Next iCol
End Sub

但我一直收到错误消息

“对象不支持此方法或属性”

在粘贴步骤。在我尝试限定所有范围后,它说我没有设置对象变量。

非常感谢您的帮助!

【问题讨论】:

  • 不应该.Range2 改成.Range 吗?或者只使用 Range2. 而不使用工作表声明,因为 [ActiveSheet] Cells 参考中已经暗示了它。
  • 您好,我需要将复制的单元格粘贴到 Range2 中,这是另一个工作表(而不是当前活动工作表)的 F 列中的第一个空单元格
  • 你真的需要用一张表来限定每个范围和单元格对象,否则你会遇到这样的各种混乱。
  • 由于 Range2 是可变的,已设置为工作表 csv 上的单元格,因此删除工作表前缀。即Range2.PasteSpecial ...

标签: excel vba


【解决方案1】:

这个怎么样?

Sub Transposes()

    ' Example just for hardcoded data
    Dim inputRange As Range
    Set inputRange = Sheets("Sheet1").Range("C5:F10").SpecialCells(xlCellTypeConstants)

    Dim outputCell As Range
    Set outputCell = Sheets("Sheet2").Range("A1")

    Dim cell As Range
    For Each cell In inputRange
        Dim offset As Long
        outputCell.offset(offset).Value = cell.Value
        offset = offset + 1
    Next cell

End Sub

将 ColumnF 中的最后一行设置为您想要的任何内容,如果动态更改,只需使用多种技术中的任何一种即可找到您需要复制/粘贴的最后一个单元格。

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多