Link to home
Start Free TrialLog in
Avatar of Flora Edwards
Flora EdwardsFlag for Sweden

asked on

Excel VBA

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.
Avatar of Phillip Burton
Phillip Burton

Do you want formulas to copy across, or just the values?
Avatar of Flora Edwards

ASKER

just values
Avatar of Rob Henson
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
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.
ASKER CERTIFIED SOLUTION
Avatar of Phillip Burton
Phillip Burton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
@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.
@Rob Henson
thank you.  the number of columns and rows are increased and sometimes decreased each time i download the new data.
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

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.
For the changing columns, how are you handling the ongoing calculations, presumably on the Main sheet?

Thanks
Rob H
thanks Philip

you are the star.

that is exactly what i needed.
I think maybe you should have accepted Phillip's solution.

You can request moderator attention to change it.

Thanks
Rob
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.