简体   繁体   English

从特定范围复制和粘贴非空单元格

[英]Copy and paste not empty cells from a specific range

I have never don't VBA scripting, or macros.我从来没有不使用 VBA 脚本或宏。

However I need to copy pasting a lot of excel documents into one.但是我需要将很多excel文件复制粘贴到一个文件中。 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.我需要复制一个包含 x 行和 y 列的表格,但有很多空行。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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