473,698 Members | 2,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Copy a particular workbook with data from another workbook

3 New Member
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").Val ue
HomeBook = ActiveWorkbook. Name
Workbooks.Open Filename:="Sour ce.xls"
OtherBook = ActiveWorkbook. Name
Cells.Select
Selection.Copy
Windows(HomeBoo k).Activate
Sheets("BackUp" ).Select
Range("A1").Sel ect
ActiveSheet.Pas te
Application.Dis playAlerts = False
Workbooks(Other Book).Close SaveChanges:=Fa lse
Application.Dis playAlerts = 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 3566
MikeTheBike
639 Recognized Expert Contributor
@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
hiitzsdg
3 New Member
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
hiitzsdg
3 New Member
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 Recognized Expert Contributor
@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
25257
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 figured out that it is not possible to open the workbook for data access with ADO (http://support.microsoft.com/?KBID=211378). Is there another way to use a password protected Excel workbook in my
0
1759
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 , I dont know the size of the data ,so I can't use the get_range option of the c# . I need a command like select.currentregion of the vba. antoher problem that I have is that I can't open 2 workbook with the
0
6794
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 chance that 'Sales' already exists in "Workbook2". I need to copy the worksheet 'sales' from 'Workbook1' to 'Workbook2'. The following cases have to be taken care off..
0
1616
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, but just last a flash, and , anyone know what's going wrong? Dim oApp As New Excel.Application Dim oWBa As Excel.Workbook = oApp.Workbooks.Open("c:\Test.XLS") Dim oWBb As Excel.Workbook = oApp.Workbooks.Open("c:\Error.xls")
1
1210
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 Excel '02. - I was initially asked to do this as an Access DB, but... - Data comes from a web-based report generator into an Excel workbook, so why go with Access, especially when no one in the office knows how to use it... - Generated workbook has a...
0
3211
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 been searching for code for each part of the task separately and trying to piece together multiple macros, that do something similar, to what I’m trying to accomplish in my over all task, but I’m not having a lot of luck. So, here’s the entire task,...
3
3814
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 workbook and save the results in a new workbook. I keep getting the message "subscript out of range." Any help is greatly appreciated. Private Sub Copier_Click() ' Copy all sheets in FileA before sheets in FileMaster and save as NewFile Dim...
5
2682
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. Here is the selection code... Function SelectARange(sPrompt As String, sCaption As String, oReturnedRange As Range) As Boolean Dim frmSelectCells As ufSelectCells Set frmSelectCells = New ufSelectCells With frmSelectCells ...
4
4032
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 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...
0
8683
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8611
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.