0

I´m new and starting to make baby steps in 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. 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. 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

QHarr
  • 83,427
  • 12
  • 54
  • 101
  • Please give a little bit more detail about what isn't working. Also, are you expecting only a single column input range to be selected and will there be a max number of rows? Ranges read in from a sheet will be 2D arrays _A(i) = aWB.Sheets(1).Range("UserRange")_ so you may need to Index slice out appropriate column. Also, you will need to appropriately resize _tWB.Sheets(1).Range.Cells(i, 1)_ this to be the same dimensions as intended output array (so store transposed array into variable first....) – QHarr Nov 29 '20 at 00:59
  • You are pasting your transposed array and closing the aWB every iteration of i. Shouldn't that be outside of the loop? – Dude_Scott Nov 29 '20 at 02:34
  • Thank you for answering. Yes, it is a column vector, number of rows to be determined when the range is selected. I´m thinking to make an array that has dimension "number of files=i"*"number of columns=j" *. Then I need to paste in row (i), the j element in the original file. I´m trying to make a nested For Loop – Hols Muller Nov 29 '20 at 19:56

1 Answers1

0

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.

  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. If the user presses Cancel there will be no array and the execution should stop. Youi can ask questions about that function.
  2. Specify the column. Your idea isn't workable. The Range object is tied to one worksheet. You can't specify the "same" range in each sheet. You also have no worksheet open at the time of calling the InputBox. It's possible to specify the same range address for each sheet but you can't get that from the 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.
  3. Open files. Use Application.ScreenUpdating = False to never see the open file. You don't need to Activate anything. In fact, you don't need the variable aWB since you work with the 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. 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). 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.
Variatus
  • 14,293
  • 2
  • 14
  • 30
  • Thank you Variatus, I´m trying to refine the code as to make a more accurate question. Your comments are very helpful. I´d prefer to make it all on a sub. Working on specifying the number of rows. – Hols Muller Nov 29 '20 at 19:58