创建新工作簿时,将选择单元格粘贴到列中

时间:2015-08-12 15:21:52

标签: excel vba excel-vba

此脚本从C2:C3复制工作簿中所有工作表的所有单元格。我想它忽略所有隐藏的床单。当我尝试粘贴到下一个可用列时,当前脚本也会粘贴到下一个可用行中。

Option Explicit
Sub Sample()
Dim wsInput As Worksheet, wsOutput As Worksheet
Dim rng As Range
Dim LRowO As Long, LRowI As Long

'Set the output sheet
Set wsOutput = ThisWorkbook.Sheets("Master")

For Each wsInput In ThisWorkbook.Worksheets
    If wsInput.Name <> wsOutput.Name Then
        With wsInput      
            Set rng = .Range("C2:C3")
            rng.Copy
            With wsOutput             
                LRowO = .Range("A" & .Rows.Count).End(xlUp).Row + 1
                .Range("A" & LRowO).PasteSpecial xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            End With
        End With
    End If
Next wsInput

Exit Sub

End Sub

1 个答案:

答案 0 :(得分:1)

测试:

Option Explicit

Sub CommandButton()

    Dim ws1 As Worksheet, ws2 As Worksheet, wsM As Worksheet, lr As Long

    Set wsM = Worksheets("Master")
    Set ws1 = Worksheets("New_Part")
    ws1.Copy After:=Worksheets(ws1.Index)
    Set ws2 = Worksheets(ws1.Index + 1)

    lr = wsM.Cells(wsM.Rows.Count, 1).End(xlUp).Row + 1

    wsM.Range(wsM.Cells(lr, 1), wsM.Cells(lr + 1, 1)).Value2 = ws2.Range("C2:C3").Value2

End Sub