简体   繁体   English

如何将多个工作簿中的数据复制、转置和合并到原始活动工作簿中?

[英]How to copy, transposing and consolidate data from several workbooks to the original active workbook?

I´m new and starting to make baby steps in VBA.我是新手,开始在 VBA 中迈出第一步。 I want to make a macro that opens CSV files and that asks for a range to be selected from the first file (I need to select a column vector and that range will be the same along the macro), extracts the data from that column vector and paste it as a row vector (transposes the data) in the original active workbook.我想创建一个宏来打开 CSV 文件并要求从第一个文件中选择一个范围(我需要 select 一个列向量,并且该范围将与宏相同),从该列向量中提取数据并将其粘贴为原始活动工作簿中的行向量(转置数据)。 I have tried lots of things but I think I´m missing some knowledge.我已经尝试了很多东西,但我认为我缺少一些知识。 I think maybe I need to make an array inside an array cause A(i) has more than one element, it is itself an array.我想也许我需要在数组中创建一个数组,因为A(i)有多个元素,它本身就是一个数组。 This is what I wrote:这是我写的:

Option Explicit
Option Base 1

Sub x()

    Dim FileNames() As Variant, nw As Integer
    Dim i As Integer, A() As Variant
    Dim tWB As Workbook, aWB As Workbook
    
    Set tWB = ThisWorkbook
    
    Dim UserRange As Range

    FileNames = Application.GetOpenFilename("CSV Files (*.csv*),*.csv*", , , , True)
    nw = UBound(FileNames)
    
    Application.ScreenUpdating = False

    ReDim A(nw) As Variant

    Set UserRange = Application.InputBox("Select range", "Range Selection", , , , , , 8)
    
    For i = 1 To nw
         
        Workbooks.Open FileNames(i)
        Set aWB = ActiveWorkbook
        A(i) = aWB.Sheets(1).Range("UserRange")
        tWB.Activate
        tWB.Sheets(1).Range.Cells(i, 1) = WorksheetFunction.Transpose(A)
        aWB.Close SaveChanges:=False

    Next i

End Sub

Thank you, I really appreciate your help谢谢你,我真的很感谢你的帮助

Your project falls into 4 parts which, technically speaking, require 4 questions which I shall not answer here because in this forum one thread is supposed to deal with one question only.您的项目分为 4 个部分,从技术上讲,需要 4 个问题,我不会在这里回答,因为在这个论坛中,一个线程应该只处理一个问题。

  1. First subject: Get file names.第一个主题:获取文件名。 Your idea to load the names into an array is correct.您将名称加载到数组中的想法是正确的。 If your code allows you selection of multiple files it's by chance, not by design.如果您的代码允许您选择多个文件,那是偶然的,而不是设计使然。 I recommend you create a function that returns the array.我建议您创建一个返回数组的 function。 If the user presses Cancel there will be no array and the execution should stop.如果用户按下Cancel将没有数组并且执行应该停止。 Youi can ask questions about that function.你可以问关于那个function的问题。
  2. Specify the column.指定列。 Your idea isn't workable.你的想法行不通。 The Range object is tied to one worksheet. Range object 绑定到一个工作表。 You can't specify the "same" range in each sheet.您不能在每张工作表中指定“相同”范围。 You also have no worksheet open at the time of calling the InputBox.在调用 InputBox 时,您也没有打开工作表。 It's possible to specify the same range address for each sheet but you can't get that from the InputBox.可以为每张工作表指定相同的范围地址,但不能从 InputBox 中获取。 I suspect you should enter a column ID (like "C") or a column number (like 3) or, most likely, a column caption (like "APPL") and let the next section look for that column in each open file.我怀疑您应该输入一个列 ID(如“C”)或一个列号(如 3),或者最有可能的是一个列标题(如“APPL”),然后让下一部分在每个打开的文件中查找该列。
  3. Open files.打开文件。 Use Application.ScreenUpdating = False to never see the open file.使用Application.ScreenUpdating = False永远不会看到打开的文件。 You don't need to Activate anything.你不需要Activate任何东西。 In fact, you don't need the variable aWB since you work with the ActiveWorkbook .事实上,您不需要变量aWB ,因为您使用的是ActiveWorkbook But consider to specify a worksheet.但考虑指定一个工作表。 Your loop is good but it isn't finished.你的循环很好,但还没有完成。 Make it run, Debug.Print-ing the name of each workbook / worksheet before clolsing it so that you have something that you can ask questions about if needed.让它运行,调试。在关闭之前打印每个工作簿/工作表的名称,以便您可以在需要时提出问题。
  4. Extract data from each sheet.从每张纸中提取数据。 A column has some 1 million rows.一列有大约 100 万行。 How many of them do you need?你需要几个? Your code is mute on that.您的代码对此静音。 Specify the range in this section based upon the column specs defined earlier.根据前面定义的列规范指定本节中的范围。 Then use Arr = UserRange.Value (where Arr is a Variant - not A() ), then tWB.Workheets(1).Cells(1, 1).Value = Application.Transpose(Arr) .然后使用Arr = UserRange.Value (其中Arr是 Variant - 不是A() ),然后tWB.Workheets(1).Cells(1, 1).Value = Application.Transpose(Arr) Here the row isn't specified.这里没有指定行。 You probably need to specify the first blank cell under column A, which is a subject in this fourth question.您可能需要指定 A 列下的第一个空白单元格,这是第四个问题的主题。

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

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