473,394 Members | 1,722 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,394 software developers and data experts.

Copy a particular workbook with data from another workbook

Hi All,

I am completely new to VBA and I am trying to develop a macro in a workbook (Backup.xls). The main functionality of the macro is:

1. It would open another workbook (Source.xls).
2. This workbook is regularly updated. The previously updated rows are replaced with new rows everyday.
3. Copy the newly updated row.
4. Paste the copied row after the previously inserted row.

The number of rows that can be present in the source.xls can vary from 0 to n.

Let us assume that in my Backup.xls, the number of rows that are present currently is say 16.

Thus my requirement is:
The macro would open source.xls, copy the 3 newly inserted rows (say) and then paste the same from 17th row. The first row is the column name which is same in both the excels.

I have made up the following piece of code:

Sub FetchData()
Dim SourceFile As String
Dim HomeBook As String
Dim OtherBook As String

Sheets("BackUp").Select
SourceFile = Range("A1").Value
HomeBook = ActiveWorkbook.Name
Workbooks.Open Filename:="Source.xls"
OtherBook = ActiveWorkbook.Name
Cells.Select
Selection.Copy
Windows(HomeBook).Activate
Sheets("BackUp").Select
Range("A1").Select
ActiveSheet.Paste
Application.DisplayAlerts = False
Workbooks(OtherBook).Close SaveChanges:=False
Application.DisplayAlerts = True

End Sub
The problem that I am facing is after I am executing the above piece of code, the backup.xls is having the newly inserted rows only. The previously inserted rows are overwritten.
Feb 17 '09 #1
4 3554
MikeTheBike
639 Expert 512MB
@hiitzsdg
Hi

Without knowing too much about what you are copying, perhaps this will give you a start ?
Expand|Select|Wrap|Line Numbers
  1. Sub FetchData()
  2.     Dim SourceFile As String
  3.     Dim HomeBook As String
  4.     Dim OtherBook As String
  5.     Dim NextRow As Long
  6.     Dim LastRow As Long
  7.  
  8.     Sheets("BackUp").Select
  9.     SourceFile = Range("A1").Value
  10.     HomeBook = ActiveWorkbook.Name
  11.     Workbooks.Open Filename:=SourceFile
  12.     OtherBook = ActiveWorkbook.Name
  13.  
  14.     Range("A65536").Select
  15.     Selection.End(xlUp).Select
  16.     LastRow = ActiveCell.Row
  17.  
  18.     Rows("1:" & LastRow).Select
  19.     Selection.Copy
  20.  
  21.     Windows(HomeBook).Activate
  22.  
  23.     Sheets("BackUp").Select
  24.  
  25.     Range("A65536").Select
  26.     Selection.End(xlUp).Select
  27.     NextRow = ActiveCell.Row + 1
  28.     Range(Cells(NextRow, 1).Address).Select
  29.  
  30.     ActiveSheet.Paste
  31.  
  32.     Application.DisplayAlerts = False
  33.     Workbooks(OtherBook).Close SaveChanges:=False
  34.     Application.DisplayAlerts = True
  35.  
  36. End Sub
  37.  
HTH


MTB
Feb 17 '09 #2
Hey Mike!!! Thanks a ton for your help... I got a headstart and I would now be able to complete the rest....
Feb 18 '09 #3
I have 1 more question in regards to the above query. Is it possible for us to change the serial number.

In the Source.xls, the serial number always starts from 1 and it starts from A3 cell.
However, in backup.xls, the serial number should increase by 1 after the last serial number thats updated there. In other words, suppose the last serial # in backup.xls be 16, then my intent is to copy the 3 rows present in source.xls and paste the same in backup.xls with serial # as 17, 18 and 19 instead of 1, 2 and 3.

The serial number is available in the first column in both the sheets.
Feb 18 '09 #4
MikeTheBike
639 Expert 512MB
@hiitzsdg
Hi again

Glad you were able to take it and change to suit your needs.

Regarding your last question, perhaps a mod in line withn this would do it?
Expand|Select|Wrap|Line Numbers
  1. Sub FetchData()
  2.     Dim SourceFile As String
  3.     Dim HomeBook As String
  4.     Dim OtherBook As String
  5.     Dim NextRow As Long
  6.     Dim LastRow As Long
  7.  
  8.     Sheets("BackUp").Select
  9.     SourceFile = Range("A1").Value
  10.     HomeBook = ActiveWorkbook.Name
  11.     Workbooks.Open Filename:=SourceFile
  12.     OtherBook = ActiveWorkbook.Name
  13.  
  14.     Range("A65536").Select
  15.     Selection.End(xlUp).Select
  16.     LastRow = ActiveCell.Row
  17.  
  18.     Rows("1:" & LastRow).Select
  19.     Selection.Copy
  20.  
  21.     Windows(HomeBook).Activate
  22.  
  23.     Sheets("BackUp").Select
  24.  
  25.     Range("A65536").Select
  26.     Selection.End(xlUp).Select
  27.     NextRow = ActiveCell.Row + 1
  28.     Range(Cells(NextRow, 1).Address).Select
  29.  
  30.     ActiveSheet.Paste
  31.  
  32.     Application.DisplayAlerts = False
  33.     Workbooks(OtherBook).Close SaveChanges:=False
  34.     Application.DisplayAlerts = True
  35.  
  36.     Dim i As Long
  37.     i = NextRow
  38.  
  39.     Do Until Cells(i, 1) = ""
  40.         Cells(i, 1) = Cells(i - 1, 1) + 1
  41.         i = i + 1
  42.     Loop
  43.  
  44. End Sub
MTB
Feb 18 '09 #5

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

Similar topics

6
by: Geert-Pieter Hof | last post by:
Hello, My VB 6.0 application read and writes data from and to a MS Excel workbook, using the Microsoft.Jet.OLEDB.4.0 provider. Now I want to protect the Excel workbook with a password, but I...
0
by: eyal | last post by:
Hello, I want to open 2 workbook, , lets say workbook1 and workbook2 now I want to copy data from a sheet name "TEST" in workbook2 and paste the data to a sheet named "TEST" in workbook. now...
0
by: shan_chennai | last post by:
I have the followign requirement.. I have a worksheet Named 'Sales' in a excel Workbook named "WorkBook1". There is another workbook by Name "Workbook2" which has many worksheets. There is a...
0
by: Kay | last post by:
Hi all, I want to copy an entire row from a worksheet to another worksheet, when I set the excel app = visible and step thru the code I can actally see a row is appended to another worksheet,...
1
by: shutterlug | last post by:
Hello Everyone, I'm in the middle of what has turned into a very strange project. I am desperate and could use some help. Here are the circumstances: - The company I'm working for uses...
0
by: Taxman | last post by:
Windows XP, MS Office Excel 2003 If the tasks, I’m trying accomplish have been addressed previously (separately or in combination). Please, provide the links or keyword search to find them. I’ve...
3
by: samj | last post by:
Looking at code examples, it would seem this is simple, but I can't figure it out. From an Access 2000 form command button, I want to copy all worksheets in an existing workbook to an existing...
5
by: dehboy | last post by:
I've got this program that I want to have the user select a range in one workbook, close that workbook, and then paste that selection in the second workbook. This is VBA code for an excel document. ...
4
by: omono84 | last post by:
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...
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: 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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.