来自不同工作表的复制粘贴上需要的对象

时间:2018-11-07 14:35:56

标签: excel vba object copy-paste

我正在尝试从工作表中复制一个范围,然后将其转置+粘贴到同一工作簿中的另一工作表中,但是由于某种原因,我得到了

  

对象必需错误

在首行.Copy

。有人可以向我解释吗?

Sub Clean()
    Dim tRow As Double
    Dim bRow As Double
    Dim PasteRange As Double

    tRow = 5

    With ThisWorkbook.Worksheets("Canadian").Activate
        bRow = Cells(Rows.Count, "A").End(xlUp).row
        Worksheets("Canadian").Range(.Cells(tRow, "A"), .Cells(bRow, "A")).Copy
        PasteRange = (bRow - tRow)
    End With

    With ThisWorkbook.Worksheets("SectorSort").Activate
        .Range(.Cells(7, "D"), .Cells(7, PasteRange + 4)).PasteSpecial (xlPasteValues), Transpose:=True    
    End With
End Sub

2 个答案:

答案 0 :(得分:1)

需要进行一些更改:

Sub Clean()

    Dim tRow As Long: tRow = 5
    Dim bRow As Long

    With ThisWorkbook.Worksheets("Canadian")
        bRow = .Cells(.Rows.Count, "A").End(xlUp).row
        .Range(.Cells(tRow, "A"), .Cells(bRow, "A")).Copy
    End With

    With ThisWorkbook.Worksheets("SectorSort")
        .Range("D7").PasteSpecial Paste:=xlPasteValues, Transpose:=True
    End With


End Sub

需要删除.Activate

因为您在.语句中,所以在RangeCells之前添加With

而且粘贴时您也可以只定位单个单元格

答案 1 :(得分:0)

更改

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "CMD.EXE";
psi.Arguments = "/K yourmainprocess.exe";
Process p = Process.Start(psi);
p.WaitForExit();

进入

With ThisWorkbook.Worksheets("Canadian").Activate

与下一个相同(关于“ SectorSort”)