Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel macro problem with copy and fill down

Posted on 2011-02-24
12
Medium Priority
?
536 Views
Last Modified: 2012-05-11
I have included 2 workbooks and two macros. One is the database (wb1) and the other (wb2) is where the data is copied to. The macros copy the latest data in WB1 into WB2 by comparing the last rows and filling down the new data.
If you run the macros you will see that the EURUSD macro copies the data correctly whilst the AUDUSD macro does not. In the latter case it copies the data by adding 1 to each row.
I actually have about twenty macros that all work; only the AUDUSD does not but I cannot found out where the mistake is. As far as I can see the macros are identical.

Can you help please?
Thanks
CC
ub AUDUSD()

Dim Lastrow1 As Long, Lastrow2 As Long, Lastrow3 As Long, i As Long, rowtouseforAutofill As Long
Lastrow1 = ThisWorkbook.Sheets("AUDUSD").Range("A65536").End(xlUp).Row
Lastrow2 = Workbooks("wb2").Sheets("audusd").Range("A65536").End(xlUp).Row
rowtouseforAutofill = IIf(Lastrow1 < Lastrow2, Lastrow1, Lastrow2)
Workbooks("wb2").Sheets("audusd").Activate
Workbooks("wb2").Sheets("audusd").Rows(rowtouseforAutofill).AutoFill Destination:=Rows(Lastrow1 & ":" & Lastrow2)



End Sub

Sub EURUSD()

Dim Lastrow1 As Long, Lastrow2 As Long, Lastrow3 As Long, i As Long, rowtouseforAutofill As Long
Lastrow1 = ThisWorkbook.Sheets("EURUSD").Range("A65536").End(xlUp).Row
Lastrow2 = Workbooks("wb2").Sheets("eurusd").Range("A65536").End(xlUp).Row
rowtouseforAutofill = IIf(Lastrow1 < Lastrow2, Lastrow1, Lastrow2)
Workbooks("wb2").Sheets("eurusd").Activate
Workbooks("wb2").Sheets("eurusd").Rows(rowtouseforAutofill).AutoFill Destination:=Rows(Lastrow1 & ":" & Lastrow2)



End Sub

Open in new window

wb2.xlsm
WB1.xls
0
Comment
Question by:CC10
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 34976863
You will have to attach a working copy of the files. The files already attached are not working copies - the sheet names are not as per the Macro requirement. Without the working copy which is giving you the problem it would be difficult to detect what is going wrong.

Saqib
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 34976873
You will have to attach a working copy of the files. The files already attached are not working copies - the sheet names are not as per the Macro requirement. Without the working copy which is giving you the problem it would be difficult to detect what is going wrong.

Saqib
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 34977309
As Saqib says, it's very difficult to determine your problem without the working copies. However, two odd things about the macros:

1. Lastrow3 is declared in both macros but never used
2. The empty module in Wb1 seems to indicate that the macros resides in this workbook. This means that Lastrow1 and Lastrow2 will always be identical.

Curt
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 34977497
The second line should have read:
The empty module in Wb2 seems to indicate that the macros resides in this workbook. This means that Lastrow1 and Lastrow2 will always be identical.

Curt
0
 

Author Comment

by:CC10
ID: 34977809
My apologies. I have included the two workbooks which I have saved in the C drive. I have deleted the "last row 3" in the macros. The real macros are more complex but if the data download does not work, then all the rest is useless.
Sub AUDUSD()

Dim Lastrow1 As Long, Lastrow2 As Long, i As Long, rowtouseforAutofill As Long
Lastrow1 = ThisWorkbook.Sheets("AUDUSD").Range("A65536").End(xlUp).Row
Lastrow2 = Workbooks("wb2").Sheets("audusd").Range("A65536").End(xlUp).Row
rowtouseforAutofill = IIf(Lastrow1 < Lastrow2, Lastrow1, Lastrow2)
Workbooks("wb2").Sheets("audusd").Activate
Workbooks("wb2").Sheets("audusd").Rows(rowtouseforAutofill).AutoFill Destination:=Rows(Lastrow1 & ":" & Lastrow2)



End Sub

Sub EURUSD()

Dim Lastrow1 As Long, Lastrow2 As Long, i As Long, rowtouseforAutofill As Long
Lastrow1 = ThisWorkbook.Sheets("EURUSD").Range("A65536").End(xlUp).Row
Lastrow2 = Workbooks("wb2").Sheets("eurusd").Range("A65536").End(xlUp).Row
rowtouseforAutofill = IIf(Lastrow1 < Lastrow2, Lastrow1, Lastrow2)
Workbooks("wb2").Sheets("eurusd").Activate
Workbooks("wb2").Sheets("eurusd").Rows(rowtouseforAutofill).AutoFill Destination:=Rows(Lastrow1 & ":" & Lastrow2)



End Sub

Open in new window

wb2.xlsm
WB1.xls
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 1000 total points
ID: 34977950
The AUDUSD sheet does not work as expected because it contains values which are incremented by 1 through the "Autofill" method.

On the other hand the EURUSD sheet has formulas which refer to the other workbook and the Autofill increments the formula by 1 thus giving the desired results.

Saqib
0
 
LVL 43

Assisted Solution

by:Saqib Husain, Syed
Saqib Husain, Syed earned 1000 total points
ID: 34977983
You need to change the AUDUSD sheet to contain formulas similar to the EURUSD worksheet and then you can run the macro as desired.
0
 

Author Comment

by:CC10
ID: 34978117
Staring at me in the face and I didn't notice it.  I will split the points as the solutions arrived at the same time. I hope thats is OK.

Thank you for your help.
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 34978137
>>>I will split the points...

It really does not matter as both the comments from me.... which further strengthens your comment Staring at me in the face and I didn't notice it....

;-)

Cheers

Thanks for the points

Saqib
0
 

Author Comment

by:CC10
ID: 34978250
Saqib,

it is what happens to you when you get over 35...............

Best,
Chris
0
 
LVL 18

Expert Comment

by:Curt Lindstrom
ID: 34978280
Both halves of Saquib deserved to split the points! I missed out making any useful contribution enjoying my dinner and a glass of wine here down under in Oz while it all happened  :-)

Curt
0
 

Author Comment

by:CC10
ID: 34978666
Sorry Curt. I will be back soon with another moronic question.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

704 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