简体   繁体   中英

VBA copy number of Rows to new Workbook

as a beginner to coding and VBA in specific im currently trying to automate as much worksteps as im able to, mainly to get into it. So for now i got a set of >50 Excel Workbooks and the "simple" task to collect the number of Datapoints (one row for each datapoint) in each and pass this value to a new workbook . What i built together until now is this (the credit for basic construct goes fully to Henrik Schiffner , i used it for several other operations):

Sub count_rows()
    'Define variables:
    Dim numberOfFilesChosen, i As Integer
    Dim tempFileDialog As FileDialog
    Dim mainWorkbook, sourceWorkbook As Workbook
    Dim tempWorkSheet As Worksheet
    Dim LastRow As Integer

    Set mainWorkbook = Application.ActiveWorkbook

    'This Step is not mandatory of course but quite comfortable to choose 
    'the workbooks to work with
    Set tempFileDialog = Application.FileDialog(msoFileDialogFilePicker)        

    tempFileDialog.AllowMultiSelect = True        
    numberOfFilesChosen = tempFileDialog.Show

    'Loop through all selected workbooks
    For i = 1 To tempFileDialog.SelectedItems.count
        Workbooks.Open tempFileDialog.SelectedItems(i)
        Set sourceWorkbook = ActiveWorkbook

        For Each tempWorkSheet In sourceWorkbook.Worksheets
            LastRow = tempWorkSheet.UsedRange.Rows.count - 1
            MsgBox LastRow
        Next tempWorkSheet            

        Application.DisplayAlerts = False
        sourceWorkbook.Close
        Application.DisplayAlerts = True
    Next i

    mainWorkbook.Save
End Sub

This gives me the correct Value from each file in the prompted message box. However, im failing to grab the LasRow value and simply copy it to the mainWorkbook. Aim is to have one value after another in one column (lets say "A"). Failure is on different levels like: Searching for the last empty row in mainWorkbook with:

destinationRow = mainWorkbook.Worksheets("Sheet1").Cells(Rows.count, 1).End(xlUp) + 1

Or even to give the lastRow Value to any spot in mainWorkbooks with eg:

mainWorkbook.Worksheets("Sheet1").Rows(destinationRow) = LastRow

Or

LastRow.Copy After:=XXXX

Im pretty sure im misunderstanding a basic concept of VBA, so it would be awesome to get a short explanation why my operations did not work out instead of just getting a working code. However, adding the name of each Workbook as a header for its value would be magnificent!

Searching for the last empty row in mainWorkbook would use the UsedRange property of Worksheet object; this returns a Range object which you then access the Address property of. You then want to use the Split() function which will take your string and place each substring seperated by a delimiter ($ in this case) into an index of an array. For your purposes you want the 4th index which will be the last row in the UsedRange:

'The MsgBox's should help you understand what is happening with this code
MsgBox (mainWorkbook.Sheets("Sheet1").UsedRange.Address)
lastRow = Split(mainWorkbook.Sheets("Sheet1").UsedRange.Address, "$")(4)
MsgBox (lastRow)

I am a little confused by what you meant with, "give the lastRow Value to any spot in mainWorkbooks." If you meant you would like the set the value of a cell in your mainWorkbook to the value of the lastRow, you would use:

mainWorkbook.Sheets("Sheet1").Cells(1, 1).Value = lastRow

And I am not sure what you are trying to do with: LastRow.Copy After:=XXXX . Your LastRow variable is an Integer Dim LastRow As Integer . An Integer Data Type cannot use the Copy Method. I mostly use the .Copy After:= Method to copy one worksheet and paste it after another. If that is your goal:

'Copy Sheet1 and place it after the last sheet in the workbook
Sheets("Sheet1").Copy After:=Sheets(ActiveWorkbook.Sheets.count)

Otherwise, if you could explain your goal with that I would be happy to help.

As a beginner to code and VBA I would suggest a few things. The first is to do research on OOP (Object Oriented Programming). A very basic breakdown is that you have Objects which then have Methods and Properties (for example the CoffeMug object has a .Temperature property and a .Spill Method, and timsMug and tinasMug are both CoffeeMug objects). I love OOP, but it takes a while to get a truly deep understanding of how it works. After that though, it's a lot of fun and really powerful.

Other than that, use https://docs.microsoft.com/en-us/office/vba/api/overview/ to learn about VBA. I use the Excel section and Language Reference section all the time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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