Importing File

  • Thread starter Thread starter infojmac
  • Start date Start date
I

infojmac

Hi,

I have the following which allows the user to browse and gives me th
filename

Public Sub Importflu()
Dim myfile As Variant


myfile = Application.GetOpenFilename _
(filefilter:="CSV Files(*.csv),*.csv,All Files (*.*),*.*")


End Sub

What i want is it to then take the data from this file and copy it int
the active workbook - i used the recorder to import specific data bu
couldn't marry up my 'myfile' path with the connection:= bit.

So the user should just select the appropiate file and it copies ou
the data and sticks it in the new workbook - then i can do the othe
sorting macros etc in that workbook.

Any help much appreciated it seems like it should be simple but i'v
been trying all mornniing!

Cheer
 
Public Sub Importflu()
Dim myfile As Variant
Dim bk as workbook, sh as worksheet
worksheets.Add
set sh = ActiveSheet
myfile = Application.GetOpenFilename _
(filefilter:="CSV Files(*.csv),*.csv,All Files (*.*),*.*")
set bk = Workbooks.Open( Filename:=myFile)
bk.worksheets(1).UsedRange.copy Destination:= sh.Range("A1")
bk.Close Savechanges:=False
End Sub
 
Try this:

Public Sub Importflu()
Dim myfile As String
Dim wsSource as Worksheet, wsDestination as Worksheet

Set wsDestination = ActiveSheet
myfile = Application.GetOpenFilename _
(filefilter:="CSV Files(*.csv),*.csv,All Files (*.*),*.*")

'open workbook, and get handle on first worksheet
Set wsSource = Wokbooks.Open(myfile).Sheets(1)

'''copy data
'copy single cell value only
wsDestination.Range("A1").Formula =_
wsSource.Range("A1").Formula
'...or copy a chunk, including formats
wsSource.Range("A1:G10").Copy_
Destination:wsDestination.Range("A1")

'close workbook
wsSource.Parent.Close(False)
End Sub

give this a try and get back with any problems,
Cheers.
Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top