Solved

Excel Vba

Posted on 2014-09-22
13
256 Views
Last Modified: 2014-09-22
Hello,

i have the two files that i put array formulas regularly by updating the Main.xlsm from Data.xlsx.
please see the attached files.   four columns from Data headers are highlihgted in yellow to be matched with the data in Main file and return all those highlighted columns to the Main file highlighted columns.  

i wanted to have this automated by vba, i was wondering if any expert in VBA could help.

thanks.
MAIN.xlsm
DATA.xlsx
0
Comment
Question by:ProfessorJimJam
[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
  • 7
  • 6
13 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40336189
Why VBA? If you have something that is working in Excel that is not too difficult to maintain, why would you want it in VBA, where it is less easy?

Here's the code, which just replicates your formulas:

Sheets("Main").Select
For introw = 2 To 50
    Cells(introw, 10).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!R" & introw & "C3:R51C3,MATCH(RC2&RC1,[DATA.xlsx]File!R" & introw & "C3:R51C3&[DATA.xlsx]File!R" & introw & "C5:R51C5,0)),""Not Found"")"
    Cells(introw, 11).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!R" & introw & "C5:R51C5,MATCH(RC2&RC1,[DATA.xlsx]File!R" & introw & "C3:R51C3&[DATA.xlsx]File!R" & introw & "C5:R51C5,0)),""Not Found"")"
    Cells(introw, 12).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!R" & introw & "C7:R51C7,MATCH(RC2&RC1,[DATA.xlsx]File!R" & introw & "C3:R51C3&[DATA.xlsx]File!R" & introw & "C5:R51C5,0)),""Not Found"")"
    Cells(introw, 13).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!R" & introw & "C12:R51C12,MATCH(RC2&RC1,[DATA.xlsx]File!R" & introw & "C3:R51C3&[DATA.xlsx]File!R" & introw & "C5:R51C5,0)),""Not Found"")"
    Range(Cells(introw, 10), Cells(introw, 13)).Formula = Range(Cells(introw, 10), Cells(introw, 13)).Value
Next

Open in new window

0
 
LVL 26

Author Comment

by:ProfessorJimJam
ID: 40336221
VBA is required becuase the range of data is not always 50, it can be hundreds of rows. the attachments were just a sample.
i also do not want the formula to be placed in the cells but instead just the values. becuase having these array formulas will make the worksheet very slow.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40336230
The above code copies the formula, then copies the value from the formula, so it will only calculate one row at a time.
0
Technology Partners: 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!

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40336231
The above code copies the formula for one row, then copies the value from the formula, so it will only calculate one row at a time.

You can adjust the number of rows but adjusting the number in line 2.
0
 
LVL 26

Author Comment

by:ProfessorJimJam
ID: 40336232
also your code did not produce the correct result.

perhaps somthing wrong with the ,[DATA.xlsx]File!R" & introw & part. becuase with each cell moving down, it also excludes the previous rows.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40336240
Try this code then. It continues until column A is blank.

Sheets("Main").Select
introw = 2
Do Until Cells(introw, 1) = ""
    Cells(introw, 10).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!$C$2:$C$51,MATCH($B" & introw & "&$A" & introw & ",[DATA.xlsx]File!$C$2:$C$51&[DATA.xlsx]File!$E$2:$E$51,0)),""Not Found"")"
    Cells(introw, 11).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!$E$2:$E$51,MATCH($B" & introw & "&$A" & introw & ",[DATA.xlsx]File!$C$2:$C$51&[DATA.xlsx]File!$E$2:$E$51,0)),""Not Found"")"
    Cells(introw, 12).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!$G$2:$G$51,MATCH($B" & introw & "&$A" & introw & ",[DATA.xlsx]File!$C$2:$C$51&[DATA.xlsx]File!$E$2:$E$51,0)),""Not Found"")"
    Cells(introw, 13).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!$L$2:$L$51,MATCH($B" & introw & "&$A" & introw & ",[DATA.xlsx]File!$C$2:$C$51&[DATA.xlsx]File!$E$2:$E$51,0)),""Not Found"")"
    Range(Cells(introw, 10), Cells(introw, 13)).Formula = Range(Cells(introw, 10), Cells(introw, 13)).Value
    introw = introw + 1
    DoEvents
Loop

Open in new window

0
 
LVL 26

Author Comment

by:ProfessorJimJam
ID: 40336248
thanks Phillip

there is one last thing to have it fix. somthings in Main file the columns A and B names might have leading and trailing zeros. how this could be modifies to remove the leading and trailing zeros from Column A and B of the main file, before the formula is placed.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40336250
Can you give me an example - would column A and B be 00Hawkins or Benjamin00?

Should there be any zeros in column A or B?
0
 
LVL 26

Author Comment

by:ProfessorJimJam
ID: 40336252
Sorry not zeros, spaces. I mistyped space with zeros
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40336255
Sheets("Main").Select
introw = 2
Do Until Cells(introw, 1) = ""
    Cells(introw, 1) = Trim(Cells(introw, 1))
    Cells(introw, 2) = Trim(Cells(introw, 2))
    Cells(introw, 10).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!$C$2:$C$51,MATCH($B" & introw & "&$A" & introw & ",[DATA.xlsx]File!$C$2:$C$51&[DATA.xlsx]File!$E$2:$E$51,0)),""Not Found"")"
    Cells(introw, 11).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!$E$2:$E$51,MATCH($B" & introw & "&$A" & introw & ",[DATA.xlsx]File!$C$2:$C$51&[DATA.xlsx]File!$E$2:$E$51,0)),""Not Found"")"
    Cells(introw, 12).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!$G$2:$G$51,MATCH($B" & introw & "&$A" & introw & ",[DATA.xlsx]File!$C$2:$C$51&[DATA.xlsx]File!$E$2:$E$51,0)),""Not Found"")"
    Cells(introw, 13).FormulaArray = "=IFERROR(INDEX([DATA.xlsx]File!$L$2:$L$51,MATCH($B" & introw & "&$A" & introw & ",[DATA.xlsx]File!$C$2:$C$51&[DATA.xlsx]File!$E$2:$E$51,0)),""Not Found"")"
    Range(Cells(introw, 10), Cells(introw, 13)).Formula = Range(Cells(introw, 10), Cells(introw, 13)).Value
    introw = introw + 1
    DoEvents
Loop

Open in new window

0
 
LVL 26

Author Comment

by:ProfessorJimJam
ID: 40336600
Philip,

the MATCH($B" & introw & "&$A" & introw are expandable, whereas the rows in DATA.xlsx]File!$C$2:$C$51 is only stuck to the 51 row.  how do i make this DATA.xlsx]File!$C$2:$C$51   expandable ?
0
 
LVL 26

Author Comment

by:ProfessorJimJam
ID: 40336618
one more encountered, when there is a empy row in between the column a then the doevent stops there, while i want it to go through the last row that has data and ignore if the blank rows are in between.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40336839
1. Change the number $51 to a suitable number.
2. Change line 3 to read
Do Until introw = 9999

Open in new window

or a suitable number.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

632 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