Copying large amounts of information from multiple other sheets

∥☆過路亽.° 提交于 2019-12-11 23:04:52

问题


What I'm trying to do is combine information from multiple sheets all into one sheet. I've seen other posts on SO but they don't seem to work for me. I tried making something like this:

Sub Copy_Data()
    Dim empt As Long
    Dim emptmas As Long

    For s = 2 To 8
        Set ws = Worksheets(2)
        For col = 1 To 25
            For row = 2 To 51

            empt = Worksheets(1).Cells(1, Columns.Count).End(xlToLeft).Select
            emptmas = empt + 1

            Worksheets(1).Cells(row, col).Value = Worksheets(s).Cells(emptmas, col).Value

            Next row
        Next col
    Next s

End Sub

But nothing happens when I run the code, not even an error. I tried running just:

Worksheets(1).Cells(1, 1).Value = Worksheets(2).Cells(1, 1).Value

But even that didn't do anything. Is it not possible to use the Cells() function to copy from another sheet?


回答1:


I don't have time right now to complete this for you but hopefully someone else will or it will get you in the right direction. I'll check back later.

Private Sub Copy_Data()
    Dim iIndex As Integer
    Dim ws As Excel.Worksheet
    Dim wsCopyFrom As Excel.Worksheet
    Dim r As Range

    'Set the worksheet where you are going to copy the data to.
    Set ws = ActiveWorkbook.Sheets("Sheet5")

    'Loop through the worksheets 
    For iIndex = = 2 To 8

        Set wsCopyFrom = Worksheets(iIndex)
        wsCopyFrom.Activate

        'Copy range "A2:Z51" from wsCopyFrom to
        'the bottom of ws

    Next iIndex
End Sub



回答2:


Not tested, but I think this is what you're after:

Sub MacroMan()

Dim lastCell As Excel.Range

For i = 2 To 8
    With Sheets(i)    
        Set lastCell = .Cells.Find(What:="*", After:=.Cells(2, 1), Lookat:=xlPart, _
            LookIn:=xlFormulas, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
            MatchCase:=False)

        If Not lastCell Is Nothing Then 
            .Range(Cells(2, 1), lastCell).Copy Destination:=Sheets(1).Range("A" & _
                Sheets(1).Rows.Count).End(xlUp).Offset(1, 0)
        End If
    End With
Next i

End Sub


来源:https://stackoverflow.com/questions/32384223/copying-large-amounts-of-information-from-multiple-other-sheets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!