Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Specify data to import from other excel file


Hi! I was wondering if anyone could help me. I need to import a column
from another excel file. The thing is, the macro needs to ask you the
name of the file and the column (if it could possible with also a
range. i.e. you input the column "D" and you only want from D3:D1200)
in where the data is and copy them to the active cell.

Until now, I have only made the code ask for the file from which the
data is imported, but the range has to be programmed previously, is
there any function that ask the user to give the range of cells to be
copy? And the range copied in a specific column?

What i am trying to do is that in column A I import the description of
a variable number of products, in column B I import the code of the
product and in column D the number of packages (all of them from the
same file). After that I program another macro the calculates some
values with VLOOKUP, INDEX, MATCH with other files. I am stucked up
with this and can't continue, if someone can help please, it will be
very thankful.

My code is this one:

Sub Example5()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim SourceRcount As Long
Dim N As Long
Dim rnum As Long
Dim MPath As String
Dim SaveDriveDir As String
Dim FName As Variant

SaveDriveDir = CurDir
MPath = "C:\Documents and Settings\ba7268\My Documents"
ChDrive MPath
ChDir MPath

FName = Application.GetOpenFilename(filefilter:="Excel Files
(*.xls), *.xls", _
MultiSelect:=True)
If IsArray(FName) Then
Application.ScreenUpdating = False
Set basebook = ThisWorkbook
rnum = 1
basebook.Worksheets(1).Cells.Clear
'clear all cells on the first sheet

For N = LBound(FName) To UBound(FName)
Set mybook = Workbooks.Open(FName(N))
Set sourceRange = mybook.Worksheets(1).Range("H1:H1500")
SourceRcount = sourceRange.Rows.Count


With sourceRange
Set destrange =
basebook.Worksheets(1).Cells(rnum, "A"). _
Resize(.Rows.Count,
..Columns.Count)
End With
destrange.Value = sourceRange.Value

mybook.Close False
rnum = rnum + SourceRcount
Next
End If
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub

Thanks!

Daniel


--
reditz11
------------------------------------------------------------------------
reditz11's Profile: http://www.excelforum.com/member.php...o&userid=31682
View this thread: http://www.excelforum.com/showthread...hreadid=513811

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,339
Default Specify data to import from other excel file

Hi,

Try:

Dim srcrng as range

........
Set mybook = Workbooks.Open(FName(N))
Set srcrng = Application.InputBox("Enter source range", Type:=8)
Set sourceRange = mybook.Worksheets(1).Range(srcrng.Address)


HTH

"reditz11" wrote:


Hi! I was wondering if anyone could help me. I need to import a column
from another excel file. The thing is, the macro needs to ask you the
name of the file and the column (if it could possible with also a
range. i.e. you input the column "D" and you only want from D3:D1200)
in where the data is and copy them to the active cell.

Until now, I have only made the code ask for the file from which the
data is imported, but the range has to be programmed previously, is
there any function that ask the user to give the range of cells to be
copy? And the range copied in a specific column?

What i am trying to do is that in column A I import the description of
a variable number of products, in column B I import the code of the
product and in column D the number of packages (all of them from the
same file). After that I program another macro the calculates some
values with VLOOKUP, INDEX, MATCH with other files. I am stucked up
with this and can't continue, if someone can help please, it will be
very thankful.

My code is this one:

Sub Example5()
Dim basebook As Workbook
Dim mybook As Workbook
Dim sourceRange As Range
Dim destrange As Range
Dim SourceRcount As Long
Dim N As Long
Dim rnum As Long
Dim MPath As String
Dim SaveDriveDir As String
Dim FName As Variant

SaveDriveDir = CurDir
MPath = "C:\Documents and Settings\ba7268\My Documents"
ChDrive MPath
ChDir MPath

FName = Application.GetOpenFilename(filefilter:="Excel Files
(*.xls), *.xls", _
MultiSelect:=True)
If IsArray(FName) Then
Application.ScreenUpdating = False
Set basebook = ThisWorkbook
rnum = 1
basebook.Worksheets(1).Cells.Clear
'clear all cells on the first sheet

For N = LBound(FName) To UBound(FName)
Set mybook = Workbooks.Open(FName(N))
Set sourceRange = mybook.Worksheets(1).Range("H1:H1500")
SourceRcount = sourceRange.Rows.Count


With sourceRange
Set destrange =
basebook.Worksheets(1).Cells(rnum, "A"). _
Resize(.Rows.Count,
.Columns.Count)
End With
destrange.Value = sourceRange.Value

mybook.Close False
rnum = rnum + SourceRcount
Next
End If
ChDrive SaveDriveDir
ChDir SaveDriveDir
Application.ScreenUpdating = True
End Sub

Thanks!

Daniel


--
reditz11
------------------------------------------------------------------------
reditz11's Profile: http://www.excelforum.com/member.php...o&userid=31682
View this thread: http://www.excelforum.com/showthread...hreadid=513811


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
import data from txt file to an existing excel file shaji Excel Discussion (Misc queries) 1 September 12th 09 04:15 PM
Import data from several excel files into one file Aspa Excel Discussion (Misc queries) 2 April 16th 08 06:31 PM
USING DATA FROM AN EXCEL FILE TO IMPORT INTO ANOTHER Andrew Pickles Excel Worksheet Functions 1 January 17th 07 07:30 PM
Import data into Excel sheet from CSV File Connie Excel Discussion (Misc queries) 3 November 8th 06 06:02 AM
Import Excel Data from another workbook or file tamato43 Excel Discussion (Misc queries) 1 February 28th 05 05:26 PM


All times are GMT +1. The time now is 12:10 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"