Solved

Excel VBA

Posted on 2014-11-06
15
221 Views
Last Modified: 2014-11-10
I have a Template Workbook that is my main file.

the workbook name is   MyTemp.xlsm   it has only two sheets Sheet("Main") and Sheet("Data")
now every week, i download another excel workbook that its extenstion is .xls and it has two sheet("Other") and Sheet("Data") .  

i need a macro that when both of my files are open and there are no other files open, the macro copies everything from downloaded file Sheet("Data") and pastes it into the workbook MyTemp.xlsm Sheet("Data")  

that's it.

thanks.
0
Comment
Question by:Flora
  • 6
  • 5
  • 3
15 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40426382
Do you want formulas to copy across, or just the values?
0
 
LVL 5

Author Comment

by:Flora
ID: 40426392
just values
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 40426404
Will the columns in both data sheets be the same?

You could setup a Data Connection between the two files. When source Data sheet is updated, refresh link in Destination workbook and new data will be populated.

Thanks
Rob H
0
 
LVL 5

Author Comment

by:Flora
ID: 40426416
the column of both are not the same always.

what i want is that i do not need the existing data in workbook MyTemp.xlsm   Sheet("Data").  the macro should delete all data first from MyTemp.xlsm   Sheet("Data") before pasting new data from other downloaded workbook.
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40426432
Make sure you have saved MyTemp.xlsm before running, as it cannot be undone. This code should be launched from your spreadsheet.

Sub CopyFromTempToTemp()
    Sheets("Data").Select
    Selection.Copy
    Windows("MyTemp.xlsm").Activate
    Sheets("Data").Select
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
End Sub

Open in new window

0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 40426436
I assume the formulas in Main are referencing particular columns. If the columns are not the same, how are you managing the columns moving?

Or, have you misunderstood the question??? I would expect the number of rows in each column to change but the position of the columns to stay the same. If this is the case then doing a Data Connection would do as you require, it would remove existing data and replace with new.

Thanks
Rob H
0
 
LVL 5

Author Comment

by:Flora
ID: 40426451
@Phillip Burton
thank you Philip. just a question, i need to run the code from MyTemp.xlsm  . do i understand correctly that i would put your written code into MyTemp.xlsm and then run it?  becuase MyTemp.xlsm is my main file that i want to launch the macro from.   wouldn't there by any possiblity that if i run the code form MyTemp.xlsm it activetes other workbook copies the data then activates MyTemp.xlsm and pastes it in MyTemp.xlsm Data sheet.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 5

Author Comment

by:Flora
ID: 40426455
@Rob Henson
thank you.  the number of columns and rows are increased and sometimes decreased each time i download the new data.
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 40426487
Looks like Philips routine will pretty much do what you want but I note there are a couple of steps missing:

Sub CopyFromTempToTemp()
    'Select Temp Data File
    Windows("TempFile.xls").Activate
    Sheets("Data").Select
    'Copy cells from temp data
    Cells.Copy
    Windows("MyTemp.xlsm").Activate
    Sheets("Data").Select
    Cells.Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
        SkipBlanks:=False, Transpose:=False
End Sub

Open in new window

0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40426494
I would put the code in MyTemp.xlsm.

Activate the other workbook (which name I don't know).

Run the code from the other workbook (it will be called MyTemp.xlsm!CopyFromTemptoTemp

>> wouldn't there by any possiblity that if i run the code form MyTemp.xlsm it activetes other workbook copies the data then activates MyTemp.xlsm and pastes it in MyTemp.xlsm Data sheet

I thought that is what you wanted. But my code is designed to be stored in MyTemp.xlsm, but run from the other spreadsheet. So you have just opened the other spreadsheet, and then run the code.
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 40426498
For the changing columns, how are you handling the ongoing calculations, presumably on the Main sheet?

Thanks
Rob H
0
 
LVL 5

Author Comment

by:Flora
ID: 40426503
thanks Philip

you are the star.

that is exactly what i needed.
0
 
LVL 31

Expert Comment

by:Rob Henson
ID: 40426634
I think maybe you should have accepted Phillip's solution.

You can request moderator attention to change it.

Thanks
Rob
0
 
LVL 5

Author Comment

by:Flora
ID: 40426851
Yes. i made a mistake. i meant to click on the Philips comment. i will ask mod to change it.

thanks Rob for pointing this.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

What is a Form List Box? (skip if you know this) The forms List Box is the alternative to the ActiveX list box. If you are using excel 2007, you first make sure you have a developer tab (click the Orb)->"Excel Options"->Popular->"Show Developer tab…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now