简体   繁体   中英

Copy and paste not empty cells from a specific range

I have never don't VBA scripting, or macros.

However I need to copy pasting a lot of excel documents into one. So I was wondering how I could implement the following (or what direction to head):

I need to copy a table of x rows and y columns but there are many empty rows. And a lot of rows are merged. I need to copy this to another file and unmerge the rows and copy the content to all of the merged columns.

There are multiple files like this and need to go into one file. Each file has varying amount of sheets.

If anything is there anyways I can just created a macro to copy and paste only non empty columns and unmerge the merged columns and have the same data between all the merged rows?

This is a partial answer, which does not address the processing of the individual sheets. It does give you a framework to start with.

Sub Process_Workbooks()
'Process a Collection of workbooks
Dim arrPathandFile, FilePointer As Long
Dim strPathAndFile As String
Dim bkSource As Workbook, shInput As Worksheet
Dim bkDestination As Workbook, shResult As Worksheet
Dim myPath, PathandFile As String

arrPathandFile = Application.GetOpenFilename("Audit Files (*.xls*), *.xlsx, All Files (*.*), *.*", , "Select Workbooks to process", "", True)
' user cancels file selection
If Not IsArray(arrPathandFile) Then Exit Sub

'Create a place to put the results
Set bkDestination = Workbooks.Add

'For each file in the collectin
For FilePointer = 1 To UBound(arrPathandFile)
    strPathAndFile = arrPathandFile(FilePointer)

    'Open the workbook
    Set bkSource = Workbooks.Open(strPathAndFile)

    'process each worksheet
    For Each shInput In bkSource.Sheets
        Set shResult = bkDestination.Sheets.Add
        shResult.Name = shInput.Name & "(" & FilePointer & ")"

        'figure out the source range to copy
        shInput.Range("A1:Z900").Copy Destination:=shResult.Range("A1")

        'now do stuff to the sheet in the destination.
        Call Do_Stuff_To_sheets(shInput)

    'repeat for each sheet in the workbook
    Next shInput
    bkSource.Close
'repeat for each workbook selected
Next FilePointer

'save the results
bkDestination.SaveAs myPath & "NewFilename.xlsx"
End Sub

Private Sub Do_Stuff_To_sheets(mySheet As Worksheet)
'process each sheet to unmerge and defrag columns

End Sub

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