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.
4 4017
Hi
Taking you code (and moding it - for instance only one file is opened- and closed!) - Sub cmdopen_Click()
-
Dim strFile As String
-
Dim i As Long
-
Dim TargetSht As Worksheet
-
Dim LastRow As Integer
-
-
'SET (REMEMBER) CURRENT SHEET AS TARGET SHEET
-
Set TargetSht = ActiveSheet
-
strFile = Application.GetOpenFilename _
-
(FileFilter:="Excel Filer (*.xls),*.xls", _
-
Title:="Open File(s)", MultiSelect:=False)
-
-
If strFile = "False" Then Exit Sub
-
-
Application.ScreenUpdating = False
-
-
Workbooks.Open strFile
-
-
'THE LAST OPENED WORKBOOK IS AUTOMATICALLY THE ACTIVE WORKBOOK
-
Range("A2").Select
-
ActiveCell.SpecialCells(xlLastCell).Select
-
-
LastRow = ActiveCell.Row
-
-
'COPY FROM CURRENT WORKSHEET AND PASTE INTO TARGET SHEET
-
Range("A2:E" & LastRow).Copy TargetSht.Range("a17")
-
-
ActiveWorkbook.Close False
-
-
Application.ScreenUpdating = True
-
End Sub
HTH
MTB
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????
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
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 -
Option Explicit
-
-
Private Sub cmdopen_Click()
-
CopyData
-
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 - Option Explicit
-
-
Sub CopyData()
-
Dim strFile As String
-
Dim i As Long
-
Dim TargetSht As Worksheet
-
Dim LastRow As Integer
-
-
'SET (REMEMBER) CURRENT SHEET AS TARGET SHEET
-
Set TargetSht = ActiveSheet
-
strFile = Application.GetOpenFilename _
-
(FileFilter:="Excel Filer (*.xls),*.xls", _
-
Title:="Open File(s)", MultiSelect:=False)
-
-
If strFile = "False" Then Exit Sub
-
-
Application.ScreenUpdating = False
-
-
Workbooks.Open strFile
-
-
'THE LAST OPENED WORKBOOK IS AUTOMATICALLY THE ACTIVE WORKBOOK
-
Range("A2").Select
-
ActiveCell.SpecialCells(xlLastCell).Select
-
-
LastRow = ActiveCell.Row
-
-
'COPY FROM CURRENT WORKSHEET AND PASTE INTO TARGET SHEET
-
Range("A2:E" & LastRow).Copy TargetSht.Range("a17")
-
-
ActiveWorkbook.Close False
-
-
Application.ScreenUpdating = True
-
End Sub
and see what happens.
Of course I could be wrong!!
MTB
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
.......
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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...
| | |