-1

I would like to copy data from a csv file into an excel worksheet. There are 11 csv files. So far I have this (it is a modified version from a previous post):

Sub importData()   
  Dim filenum(0 To 50) As Long
  filenum(0) = 052
  filenum(1) = 060
  filenum(2) = 064
  filenum(3) = 068
  filenum(4) = 070
  filenum(5) = 072
  filenum(6) = 074
  filenum(7) = 076
  filenum(8) = 178
  filenum(9) = 180
  filenum(10) = 182

  Dim sh1 As Worksheet
  On Error GoTo my_handler

  For lngPosition = LBound(filenum) To UBound(filenum)
    Windows(filenum & ".csv").Activate
    Range("A1").Select
    Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
    Selection.Copy
    Windows("30_graphs_w_Macro.xlsm").Activate
    sh1 = Worksheets(filenum)
    sh1.Activate
    Range("A69").Select
    ActiveSheet.Paste
    Range("A69").Select

  Next lngPositionlngPositionlngPosition

my_handler:
  MsgBox "All done."
  Exit Sub
End Sub

However, I am getting a type mismatch over here:

Windows(filenum & ".csv").Activate

I am not too sure how to fix it. Other than this, does the rest of the code look ok? This is my first VBA script...

user2883071
  • 960
  • 1
  • 20
  • 50

1 Answers1

1

Filenum is an array, so you need to refer to it using an index

Windows(filenum & ".csv").Activate is wrong

Change to:

filenum(lngPosition) 

or

filenum(1)

Also your Next is wrong..

Next lngPositionlngPositionlngPosition

You must have hit paste too many times

Sam
  • 7,245
  • 3
  • 25
  • 37
  • that part works now, however it still does not get the data and paste it.. any idea why? – user2883071 Nov 06 '13 at 14:52
  • You should avoid the use of `Select` and `Copy` and `Activate` that will be causing you issues I think. Lots of info on Stackoverflow and google about it. – Sam Nov 06 '13 at 15:08