msquared99
Board Regular
- Joined
- Mar 8, 2012
- Messages
- 52
I have this code where the user can select a file. There will also be a file open and active called "Any AR"
How would I write this INDEX and MATCH formula in VBA?
ActiveCell.FormulaR1C1 = _
"=INDEX('[Any AR 03.01.13 PM.xls]Sheet1'!R3C4:R170C4,MATCH(RC[-2],'[Any AR 03.01.13 PM.xls]Sheet1'!R3C8:R170C8,0))"
Where Any AR 03.01.13 PM = FileName as referenced below and the active workbook is Any AR in the VBA code.
VBA Code Begins here:
Sub ChooseFile()
Dim fd As FileDialog
Dim FileName As String
Dim lastRow1 As Long
Dim lastRow2 As Long
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Get the number the button chosen.
Dim FileChosen As Integer
FileChosen = fd.Show
If FileChosen <> -1 Then
'Didn't choose anything (clicked cancel).
MsgBox "No file opened."
Else
'Display name and path of file chosen.
FileName = fd.SelectedItems(1)
Workbooks.Open (FileName)
lastRow1 = Worksheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
Worksheets("Sheet1").Range("H3").Select
ActiveCell.FormulaR1C1 = "=RC[-7]&RC[-6]"
With Worksheets("Sheet1").Range("H3")
.AutoFill Destination:=Range("H3:H" & lastRow1)
End With
End If
Workbooks("Any AR.xls").Activate
lastRow2 = Worksheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row - 1
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Columns("A:C").Select
Columns("A:C").EntireColumn.AutoFit
Cells.Select
Selection.RowHeight = 12
Worksheets("Sheet1").Range("D3").Select
ActiveCell.FormulaR1C1 = "=RC[-3]&RC[-2]"
With Worksheets("Sheet1").Range("D3")
.AutoFill Destination:=Range("D3:D" & lastRow2)
End With
How would I write this INDEX and MATCH formula in VBA?
ActiveCell.FormulaR1C1 = _
"=INDEX('[Any AR 03.01.13 PM.xls]Sheet1'!R3C4:R170C4,MATCH(RC[-2],'[Any AR 03.01.13 PM.xls]Sheet1'!R3C8:R170C8,0))"
Where Any AR 03.01.13 PM = FileName as referenced below and the active workbook is Any AR in the VBA code.
VBA Code Begins here:
Sub ChooseFile()
Dim fd As FileDialog
Dim FileName As String
Dim lastRow1 As Long
Dim lastRow2 As Long
Set fd = Application.FileDialog(msoFileDialogFilePicker)
'Get the number the button chosen.
Dim FileChosen As Integer
FileChosen = fd.Show
If FileChosen <> -1 Then
'Didn't choose anything (clicked cancel).
MsgBox "No file opened."
Else
'Display name and path of file chosen.
FileName = fd.SelectedItems(1)
Workbooks.Open (FileName)
lastRow1 = Worksheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
Worksheets("Sheet1").Range("H3").Select
ActiveCell.FormulaR1C1 = "=RC[-7]&RC[-6]"
With Worksheets("Sheet1").Range("H3")
.AutoFill Destination:=Range("H3:H" & lastRow1)
End With
End If
Workbooks("Any AR.xls").Activate
lastRow2 = Worksheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row - 1
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Columns("A:C").Select
Columns("A:C").EntireColumn.AutoFit
Cells.Select
Selection.RowHeight = 12
Worksheets("Sheet1").Range("D3").Select
ActiveCell.FormulaR1C1 = "=RC[-3]&RC[-2]"
With Worksheets("Sheet1").Range("D3")
.AutoFill Destination:=Range("D3:D" & lastRow2)
End With