473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

open unknown excel workbook, copy data and paste back in original workbook

I know that this should be rather simple but i seem to be missing a step to get it to work. and have been unable to find a solution on the net.

The aim is that I click on the open button to find and open an unknown workbook that contains the data that I need to imput into my current workbook, once the unknown workbook is opened it should automatically select my range (number of columns known, but number of rows unknown- the number of rows needed to be copied will change each time the unknown workbook is opened). this range will then be copied and pasted into the original workbook (book1.xls) starting in cellA17.

the below code opens the unknown workbook fine but it will not select the range properly. I have tried the range select in the unknown workbook by itself and it works like a charm but in the below code it seems to get very very confused as to which workbook it should be looking in and i don’t know how to make it point correctly so that it will work.



my code:

Sub cmdopen_Click()

'find and open file

Dim vaFiles As Variant
Dim i As Long

vaFiles = Application.GetOpenFilename _
(FileFilter:="Excel Filer (*.xls),*.xls", _
Title:="Open File(s)", MultiSelect:=True)

If Not IsArray(vaFiles) Then Exit Sub

With Application
.ScreenUpdating = False
For i = 1 To UBound(vaFiles)
Workbooks.Open vaFiles(i)
Next i
.ScreenUpdating = True
End With

'so far code works great but from here i start having problems.....

' select woksheet2 and select range of cells from A2 (to E2 and down unknow number of rows)

Worksheets("Sheet2").Range("a2").Select

Range(ActiveCell, Range(ActiveCell.Address).End _
(xlToRight).End(xlDown)).Select.Copy

' this seems to work perfect if i run these three lines in the new workbook, but when running it in conjunction with the code above it it seems to select the data in cell A2 from the original workbook and paste it in the active cell in the newly opened workbook, the active cell is not A2 in new workbook

I would then like the copied data pasted back into original workbook ("book1.xls) in cell A17

any help would be greatly appreciated.
Dec 18 '09 #1
4 4017
MikeTheBike
639 Expert 512MB
Hi

Taking you code (and moding it - for instance only one file is opened- and closed!)
Expand|Select|Wrap|Line Numbers
  1. Sub cmdopen_Click()
  2.     Dim strFile As String
  3.     Dim i As Long
  4.     Dim TargetSht As Worksheet
  5.     Dim LastRow As Integer
  6.  
  7.     'SET (REMEMBER) CURRENT SHEET AS TARGET SHEET
  8.     Set TargetSht = ActiveSheet
  9.     strFile = Application.GetOpenFilename _
  10.     (FileFilter:="Excel Filer (*.xls),*.xls", _
  11.     Title:="Open File(s)", MultiSelect:=False)
  12.  
  13.     If strFile = "False" Then Exit Sub
  14.  
  15.     Application.ScreenUpdating = False
  16.  
  17.     Workbooks.Open strFile
  18.  
  19.     'THE LAST OPENED WORKBOOK IS AUTOMATICALLY THE ACTIVE WORKBOOK
  20.     Range("A2").Select
  21.     ActiveCell.SpecialCells(xlLastCell).Select
  22.  
  23.     LastRow = ActiveCell.Row
  24.  
  25.     'COPY FROM CURRENT WORKSHEET AND PASTE INTO TARGET SHEET
  26.     Range("A2:E" & LastRow).Copy TargetSht.Range("a17")
  27.  
  28.     ActiveWorkbook.Close False
  29.  
  30.     Application.ScreenUpdating = True
  31. End Sub
HTH


MTB
Dec 18 '09 #2
Many thanks for your help MTB, I'm still quite a novie when it comes to using macros.

I have used the code you supplied and unfortunatly i'm experencing a run time error "Application-defined or object-defined error" on the line:

Range("A2").Select

To try to get around this I changed it to:

Worksheets("Sheet2").Range("A2") = Activecell

The macro ran to the end without error, but it copied the data from the original workbook, not the newly opened workbook????
Dec 20 '09 #3
MikeTheBike
639 Expert 512MB
Hi

Before we go any further, can you confirm that the 'cmdopen_Click'
subroutine code is located in a Code Module (or even ThisWorkbook Module - not recommended) and NOT the Sheet1 Module?


MTB
Dec 21 '09 #4
MikeTheBike
639 Expert 512MB
Hi again

After reflecting on my last post (and looking at the code more closely!) I think I have can answer my own question.

My conclusion is this

You have a button on the sheet and the code previously posted is run in the button click event (being as it is called 'cmdopen_Click()' !!).

In this case change the code in the Button Click event in the Sheet Module to
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Private Sub cmdopen_Click()
  4.     CopyData
  5. End Sub
and then insert a CODE MODULE (in the left pain Right Click the Microsoft Excel Objects -> Insert -> Module) and paste this code into it
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Sub CopyData()
  4.     Dim strFile As String
  5.     Dim i As Long
  6.     Dim TargetSht As Worksheet
  7.     Dim LastRow As Integer
  8.  
  9.     'SET (REMEMBER) CURRENT SHEET AS TARGET SHEET
  10.     Set TargetSht = ActiveSheet
  11.     strFile = Application.GetOpenFilename _
  12.     (FileFilter:="Excel Filer (*.xls),*.xls", _
  13.     Title:="Open File(s)", MultiSelect:=False)
  14.  
  15.     If strFile = "False" Then Exit Sub
  16.  
  17.     Application.ScreenUpdating = False
  18.  
  19.     Workbooks.Open strFile
  20.  
  21.     'THE LAST OPENED WORKBOOK IS AUTOMATICALLY THE ACTIVE WORKBOOK
  22.     Range("A2").Select
  23.     ActiveCell.SpecialCells(xlLastCell).Select
  24.  
  25.     LastRow = ActiveCell.Row
  26.  
  27.     'COPY FROM CURRENT WORKSHEET AND PASTE INTO TARGET SHEET
  28.     Range("A2:E" & LastRow).Copy TargetSht.Range("a17")
  29.  
  30.     ActiveWorkbook.Close False
  31.  
  32.     Application.ScreenUpdating = True
  33. End Sub
and see what happens.

Of course I could be wrong!!


MTB
Dec 21 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
1
by: Alex | last post by:
Hi all, I've seen this noted in many posts, but nothing I've checked out gives me any clue on how to do this. Basically as my topic says, I have a DTS and I simply need to export some data...
3
by: Otie | last post by:
I am trying to copy the cell contents in an MSFLXGRD control (using VB5) into Excel, retaining the foreground colors of the text and numbers. I have tried using the Clipboard.SetText...
3
by: | last post by:
Hello, I have a routine in my Windows application that exports the contents of a datagrid to Excel. It is modeled closely after the HowTo example on MSDN: http://tinyurl.com/5g2jm. Depending...
3
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel...
2
by: John Henry | last post by:
I posted the following message to the Pywin32 list but if anybody here can help, it would be appreciated very much. ============================ Hi list, I have a need to copy 3 rows of data...
3
by: toffee | last post by:
Hi all, I got a pre-formatted spreadsheet. would it be possible using js to copy the data from a table on the current webpage, open the spreadsheet and paste the content ? if so, anyone got any...
4
by: urprettyfriend | last post by:
Hi, I have an Excel file with 400 rows of old values and the corresponding new values. My table currently has 10 columns out of which 3 columns use the old value specified in the excel file. I...
2
by: GS | last post by:
I have installed the ms PIA for ofc XP, and followed the article http://support.microsoft.com/kb/247412/ trying to paste into a worksheet However I got late binding not allowed errors .......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.