Need help with VBA for Index Match from one table to another

spgexcel

New Member
Joined
Mar 16, 2016
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
Hi Guys,

I am trying to formulate a VBA code for index match formula applied on a table in my worksheet.
My requirement is to pull data from Table1 to Table 2 based on the change in order of Column1 in table 2.
I applied normal formula to it. It works but I was wondering if you could help me get a vba code for the same so that my file size is reduced by some bits.
I tried a lot of threads but did not find a suitable answer that has multiple columns to be populated based on criteria in 1st column.

Table1RegionJan-18Feb-18Mar-18Apr-18May-18Jun-18Jul-18Aug-18Sep-18Oct-18Nov-18Dec-18
USA89%75%82%80%59%28%87%39%81%62%76%32%
Germany70%65%98%100%81%49%91%36%61%62%34%23%
France44%93%55%84%71%67%96%26%54%29%46%70%
India63%56%67%56%67%99%69%66%29%65%91%96%
Japan29%47%41%62%33%32%60%35%64%40%55%33%
UK54%100%48%78%32%21%49%82%86%46%23%33%

Table2RegionJan-18Feb-18Mar-18Apr-18May-18Jun-18Jul-18Aug-18Sep-18Oct-18Nov-18Dec-18
Germany70%65%98%100%81%49%91%36%61%62%34%23%
France44%93%55%84%71%67%96%26%54%29%46%70%
France44%93%55%84%71%67%96%26%54%29%46%70%
India63%56%67%56%67%99%69%66%29%65%91%96%
UK54%100%48%78%32%21%49%82%86%46%23%33%
Japan29%47%41%62%33%32%60%35%64%40%55%33%

Your help is highly appreciated.

Thanks
Sumant
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
423
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Sumant,

"change in order of Column1 in table 2" meaning sorting of column, right ?

Thanks,
Saurabh
 

Saurabhj

Active Member
Joined
Jun 6, 2020
Messages
423
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Sumant,

Please check below code. Add a button in a sheet and add below macro to button.

Assuming Table 1 is in Sheet1 and Table 2 is in Sheet2.

VBA Code:
Sub calculate()
            Dim totalRows As Integer, rowno As Integer, colno As Integer
             totalRows = WorksheetFunction.CountA(Sheets("Sheet2").Range("A:A"))

            Sheets("Sheet2").Cells(2, 2).Formula = "=INDEX(Sheet1!$B$1:$M$7,MATCH(Sheet2!$A2,Sheet1!$A:$A,0),MATCH(Sheet1!B$1,Sheet1!$B$1:$M$1,0))"
            Sheets("Sheet2").Cells(2, 2).Activate
            ActiveCell.AutoFill Destination:=ActiveCell.Range("A1:L1"), Type:= _
            xlFillDefault
            ActiveCell.Range("A1:L1").Select
            Selection.AutoFill Destination:=ActiveCell.Range("A1:L" & totalRows)

End Sub

Thanks,
Saurabh
 
Solution

Watch MrExcel Video

Forum statistics

Threads
1,130,170
Messages
5,640,577
Members
417,151
Latest member
ChickenTenderer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top