[英]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 个问题,我不会在这里回答,因为在这个论坛中,一个线程应该只处理一个问题。
Range
object is tied to one worksheet. Range
object 绑定到一个工作表。 You can't specify the "same" range in each sheet.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.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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.