Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Import data into seperate sheets

Hi guys,

The piece of code that i am using here, imports data from 3 externa
files into one worksheet. Could someone tell me how the code would loo
if i were to put the data from each file into seperate worksheets?

Sub Open3_v2()
Dim i As Integer

'Clear out the existing data
Worksheets("Data").UsedRange.EntireRow.Delete

'Get the 3 data files
For i = 1 To 3
OpenFile
Next
End Sub

Sub OpenFile()
Dim strFile As String
Dim wbkData As Workbook

'Locate the file - quit if user presses cancel
strFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls"
, "Select file", "OK")
If Dir(strFile) = "" Then Exit Sub

'Open the file
Set wbkData = Workbooks.Open(strFile)

'Copy all the data from the one sheet
wbkData.Activate
ActiveSheet.UsedRange.Select
Selection.Copy

'Come back to this workbook and paste the data at the bottom
ThisWorkbook.Activate
Worksheets("Data").Activate
Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1).Select
ActiveSheet.Paste

'Close the data file
wbkData.Close False

'Free up used space in memory
Set wbkData = Nothing
End Sub

Thanks in advance,
Cathal

--
Message posted from http://www.ExcelForum.com

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Import data into seperate sheets

off the information you provided:

if you have 3 sheets Data1, Data2, Data3 then set the OpenFile Sub t
accept a parameter:

e.g. Sub OpenFile(iCount as integer)

then send the Sub the value of 'i' when it is called

e.g OpenFile i
or Call OpenFile (i)

then within the Sub use the value to access the specific sheet.

Worksheets("Data" & iCount).Activate

Hope this helps

Tanner Dhes

--
Message posted from http://www.ExcelForum.com

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Import data into seperate sheets

Tried doing this but im still a begginer at this and it would not work
can u please show me where that code goes in??

Thanks.
Cathal

--
Message posted from http://www.ExcelForum.com

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Import data into seperate sheets

Hi

The 3 worksheets that you are going to import to must be named Data1
Data2 and Data3 for this to work. I've altered the code you poste
below and marked the changed lines with - '###:
'-------------------------------------------------------------------
Sub Open3_v2()
Dim i As Integer

'Clear out the existing data
Worksheets("Data").UsedRange.EntireRow.Delete

'Get the 3 data files
For i = 1 To 3
'### the i is added on the end of this statement and sent to th
OpenFile sub
OpenFile i
Next
End Sub

'### the variable iCount was added here to take the value sent to thi
sub
Sub OpenFile(iCount as Integer)
Dim strFile As String
Dim wbkData As Workbook

'Locate the file - quit if user presses cancel
strFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls",
"Select file", "OK")
If Dir(strFile) = "" Then Exit Sub

'Open the file
Set wbkData = Workbooks.Open(strFile)

'Copy all the data from the one sheet
wbkData.Activate
ActiveSheet.UsedRange.Select
Selection.Copy

'Come back to this workbook and paste the data at the bottom
ThisWorkbook.Activate
'### the iCount value is appended onto the word 'Data' to create th
name of the worksheet and it will be incremented each time the OpenFil
sub is called
Worksheets("Data" & iCount).Activate
Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1).Select
ActiveSheet.Paste

'Close the data file
wbkData.Close False

'Free up used space in memory
Set wbkData = Nothing
End Sub

'-------------------------------------------------------------------
Hope that's clearer

Regard

--
Message posted from http://www.ExcelForum.com

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Import data into seperate sheets

Thanks a million for this, but it is givin me an error '9' saying th
script is out of range on the first line Sub open3_v2() line
"Worksheets("Data").UsedRange.EntireRow.Delete "
So i changed the name of sheet 1 from data 1 to just data and i
imported the data from the first file, but then it gave an error on th
line: "Worksheets("Data" & iCount).Activate" before it gave me th
option to open the sceond file, how do i go about solving this?

--
Message posted from http://www.ExcelForum.com



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Import data into seperate sheets

I solved this by renaming sheet 1 as 'Data', sheet 2 as 'Data1', sheet
as 'Data2' and sheet 4 as 'Data3'. but this is too much trouble and i
puts nothing into the sheet called 'Data'. Is there any way of makin
this work without havin to change the names of the worksheets at all
i.e. leave them as sheet1, sheet2, sheet3?

--
Message posted from http://www.ExcelForum.com

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Import data into seperate sheets

Sorry - my fault, i didn't read all the code properly. i've now move
the line that clears out the existing data into the loop as you shoul
have 3 worksheets that need clearing. you will need the worksheet
named Data1, Data2 & Data3 (with no space between the word an
number):
'-------------------------------------------------------------------
Sub Open3_v2()
Dim i As Integer

'######## this for loop has changed to include the clearing of data
'Get the 3 data files
For i = 1 To 3
'#####
'Clear out the existing data
Worksheets("Data" & i).UsedRange.EntireRow.Delete
'### the i is added on the end of this statement and sent to th
OpenFile sub
OpenFile i
Next
End Sub

'### the variable iCount was added here to take the value sent to thi
sub
Sub OpenFile(iCount as Integer)
Dim strFile As String
Dim wbkData As Workbook

'Locate the file - quit if user presses cancel
strFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls",
"Select file", "OK")
If Dir(strFile) = "" Then Exit Sub

'Open the file
Set wbkData = Workbooks.Open(strFile)

'Copy all the data from the one sheet
wbkData.Activate
ActiveSheet.UsedRange.Select
Selection.Copy

'Come back to this workbook and paste the data at the bottom
ThisWorkbook.Activate
'### the iCount value is appended onto the word 'Data' to create th
name of the worksheet
'### and it will be incremented each time the OpenFile sub is called
Worksheets("Data" & iCount).Activate
Cells(ActiveSheet.UsedRange.Rows.Count + 1, 1).Select
ActiveSheet.Paste

'Close the data file
wbkData.Close False

'Free up used space in memory
Set wbkData = Nothing
End Sub

'-------------------------------------------------------------------
Regard

--
Message posted from http://www.ExcelForum.com

  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Import data into seperate sheets

Sorry should know that myself (just change 'data' to 'sheet' in th
code!!) For some reason it is copying the code over and leaving row on
blank how can i change this from happening?

--
Message posted from http://www.ExcelForum.com

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Import data into seperate sheets

Again i worked this out, (really should think before i ask!!!).

This has been great help, thanks ever so much,

Regards,
Cathal

--
Message posted from http://www.ExcelForum.com

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
add two cells from seperate work sheets into a cell on seperate wo lar Excel Worksheet Functions 6 April 27th 10 06:54 PM
Splitting data from sheets into seperate workbooks based on formul bUncE Excel Worksheet Functions 1 September 7th 07 05:55 PM
Auto insert data to seperate sheets? greig2000_uk New Users to Excel 2 May 27th 06 01:10 PM
Import data into seperate columns naulerich Excel Discussion (Misc queries) 32 April 4th 06 09:22 PM
Vlook up for matching data in two seperate sheets funky via OfficeKB.com Excel Discussion (Misc queries) 1 June 3rd 05 10:16 PM


All times are GMT +1. The time now is 10:13 PM.

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"