Index and Match in VBA - excel

I don't know where I'm missing. I'm trying to provide the formula from a specific row to the last row in an excel table. I'm getting "unable to get the match property of the worksheetfunction class: 1004" error
when I use worksheetfunction.match and "Run time Error - 13 - Type Mismatch" when I use Application.match
Below is the code I have tried:
Sub PTO_Calculations()
Dim UBSht As Worksheet
Dim x As Long
'Variables for PTO Data
Dim PTOSht As Worksheet
Dim PTORows As Long, PTOCols As Long
Dim PTOOldRows As Long
Dim PTOWholeRange As Range
Dim PTOFirstColumnRange As Range
Dim PTOFirstRowRange As Range
'Variables for Table in UB Sht
Dim UBTable As ListObject
Dim UBTableRows As Long, UBTableCols As Long
Dim UBTableOldRows As Long
Set UBSht = ThisWorkbook.Sheets("UB - US & IND")
Set PTOSht = ThisWorkbook.Sheets("PTO Data")
Set UBTable = UBSht.ListObjects("UB_US_IND")
UBTableRows = UBTable.ListRows.Count + 1
UBTableCols = UBTable.ListColumns.Count
UBTableOldRows = UBTableRows - WorksheetFunction.CountIf(UBSht.Range("A:A"), Format(DateAdd("m", -1, Date), "mmmm - yyyy")) 'UBSht.Range("XFD1").Value
PTORows = PTOSht.Range("A" & Rows.Count).End(xlUp).Row
PTOCols = PTOSht.Cells(1, Columns.Count).End(xlToLeft).Column
Set PTOWholeRange = PTOSht.Range(Cells(1, 1).Address, Cells(PTORows, PTOCols).Address)
Set PTOFirstColumnRange = PTOSht.Range("A:A")
Set PTOFirstRowRange = PTOSht.Range(Cells(1, 1).Address, Cells(1, PTOCols).Address)
'I'M FACING ERROR IN THE BELOW STEP
Range(UBTable.Range(UBTableOldRows + 1, 6), UBTable.Range(UBTableRows, 6)).Value = _
Application.WorksheetFunction.Index(PTOWholeRange, Application.Match([#[Team Member Name]], PTOFirstColumnRange, 0), Application.Match([#Month], PTOFirstRowRange, 0))
end sub
Thank you for your help in advance.

Related

UDF linked to external workbook

I am trying to create a UDF using the SUMPRODUCT function which will reference data in an external closed workbook and return a value based on a couple of different criteria.
The reason I am tyring to do this is because some of the formula in the file while using the SUMPRODUCT feature could end up quite long.
So far I have come up with the following and appear to be just getting a #value error returning.
Public Function ReceiptTotal(Criteria1 As Variant, Criteria2 As Variant, Criteria3 As Variant) As Variant
Dim wbk As Workbook
Dim wks As Worksheet
Dim Criteria1Column As Range
Dim Criteria2Column As Range
Dim Criteria3Column As Range
Dim ResultColumn As Range
Dim LastRow As Long
Set wbk = Workbooks("J:\Users\Sarah\New Cash Sheet Ideas\[Receipt Log Test.xlsm]")
Set wks = wbk.Worksheets("1819")
With wbk
With wks
LastRow = wbk.wks.Range("A" & Rows.Count).End(xlUp).Row
Set Criteria1Column = wbk.wks.Range("E5:E" & LastRow) 'Criteria1Column = Bank Account
Set Criteria2Column = wbk.wks.Range("F5:F" & LastRow) 'Criteria2Column = Receipt Type
Set Criteria3Column = wbk.wks.Range("J5:J" & LastRow) 'Criteria3Column = Date
Set ResultColumn = wbk.wks.Range("H5:H" & LastRow) 'ResultColumn = Receipt Value
ReceiptTotal = [SUMPRODUCT((ResultColumn) * (Criteria1Column = Criteria1) * (CriteriaColumn2 = Criteria2) * (CriteriaColumn3 = Criteria3)]
End With
End With
End Function
I would really appreciate if someone can advise where I am going wrong.

Error 1004 Vlookup VBA

I have yet again another question to ask regarding VBA. I'm currently trying to use the VLookUp function. Here is all the information relevant to the code:
Dim Template_Sheet As Worksheet
Dim Database_Sheet As Worksheet
Dim Source_Sheet As Worksheet
Dim FileUpdate As Worksheet
Set Template_Sheet = Sheets("Template")
Set Database_Sheet = Sheets("Database")
Set Source_Sheet = Sheets("Source")
Set FileUpdate = Sheets("NewFile")
Dim Database_Row_Count As Integer
Dim Database_Column_Count As Integer
Dim id_temp As String
Dim Row_Count As Integer
Dim Lat_Index As Integer
For i = 1 To FileUpdate.Rows.Count 'Count Rows
If IsEmpty(FileUpdate.Cells(i, 1)) Then
Row_Count = i - 1
Exit For
End If
Next i
Lat_Index = Source_Sheet.Cells(6, 1).Value
Database_Row_Count = Source_Sheet.Cells(6, 4).Value
Database_Column_Count = Source_Sheet.Cells(6, 5).Value
For i = 2 To (1 + Row_Count)
id_temp = Template_Sheet.Cells(i, 1).Value
Template_Sheet.Cells(i, 2).Value = Application.WorksheetFunction.VLookup(id_temp, Database_Sheet.Range(Database_Sheet.Cells(2, 1), Database_Sheet.Cells(Database_Row_Count, Database_Column_Count)), Lat_Index, False)
Next i
I get, you guessed it, Error 1004 on run. I've used almost all of these values for other applications within the sub, so I suspect my error must be coming from a miss-defined parameter when calling the VLookup function. Help is much appreciated.
Thanks!
D.

.PivotCache.SourceData causing Application or object defined error

I have written the following code to edit the range of a two pivot tables. Up until the line of stars my code functions properly, however I get a "Run-time error '1004': Application-defined or object-defined" error on the .PivotCache.SourceData = rng.Address(True, True, xlR1C1, True) line. I have no idea what the cause of the error is as I copied the code from above and only changed the sheet name and pivot table name (they both definitely exist in my file). Any help?
Dim RowCount As Integer
Dim ColCount As Integer
Dim rng As Range
Dim CurrentPeriod As String
Dim PivotList As Variant
Dim Piv As String
Dim PivotSht As String
Dim PivotNme As String
RowCount = WorksheetFunction.CountA(Sheets("Data").Range("A:A"))
ColCount = WorksheetFunction.CountA(Sheets("Data").Range("1:1"))
Set rng = Sheets("Data").Range(Sheets("Data").Cells(1, 1), Sheets("Data").Cells(RowCount, ColCount))
CurrentPeriod = Sheets("Static").Range("CurrentPeriod")
With Sheets("Val Cat Current Returns (Adj)").PivotTables("CatCurrentPivot1")
.PivotCache.SourceData = rng.Address(True, True, xlR1C1, True)
.PivotFields("Period_id").CurrentPage = CurrentPeriod
.PivotCache.Refresh
End With
**********************************
With Sheets("Val Cat Trend Returns (Adj)").PivotTables("CatTrendPivot1")
.PivotCache.SourceData = rng.Address(True, True, xlR1C1, True)
.PivotCache.Refresh
End With
After hours of investigation I couldn't work out what was causing the error in my file. I ended up using an alternative to .PivotCache.SourceData = rng.Address(True, True, xlR1C1, True) which is Sheets(PivotSht).PivotTableWizard SourceType:=xlDatabase, SourceData:=rng. In order to use this approach you must have a cell within the chosen pivot table selected. To get around this you can locate the top left cell of the pivot table using Sheets(PivotSht).PivotTables(PivotNme).TableRange2.Cells(1).Address. My full code is below, it references the "Static" sheet, which is where all of my pivot table names and sheets are listed. (Also see the comment from Siddharth Rout for a better way to find the range of data).
Sub CurrentPeriodPivots()
Dim RowCount As Integer
Dim ColCount As Integer
Dim rng As Range
Dim CurrentPeriod As String
Dim PivotList As Variant
Dim Piv As String
Dim PivotSht As String
Dim PivotNme As String
Dim PivotLoc As String
RowCount = WorksheetFunction.CountA(Sheets("Data").Range("A:A"))
ColCount = WorksheetFunction.CountA(Sheets("Data").Range("1:1"))
Set rng = Sheets("Data").Range(Sheets("Data").Cells(1, 1), Sheets("Data").Cells(RowCount, ColCount))
CurrentPeriod = Sheets("Static").Range("CurrentPeriod")
For i = 8 To 9
PivotSht = Sheets("Static").Cells(i, 1).Value
PivotNme = Sheets("Static").Cells(i, 2).Value
PivotLoc = Sheets(PivotSht).PivotTables(PivotNme).TableRange2.Cells(1).Address
Sheets(PivotSht).Select
Range(PivotLoc).Select
Sheets(PivotSht).PivotTableWizard SourceType:=xlDatabase, SourceData:=rng
Sheets(PivotSht).PivotTables(PivotNme).PivotFields("Period_id").CurrentPage = CurrentPeriod
Sheets(PivotSht).PivotTables(PivotNme).PivotCache.Refresh
Next i
For i = 10 To 16
PivotSht = Sheets("Static").Cells(i, 1).Value
PivotNme = Sheets("Static").Cells(i, 2).Value
PivotLoc = Sheets(PivotSht).PivotTables(PivotNme).TableRange2.Cells(1).Address
Sheets(PivotSht).Select
Range(PivotLoc).Select
Sheets(PivotSht).PivotTableWizard SourceType:=xlDatabase, SourceData:=rng
Sheets(PivotSht).PivotTables(PivotNme).PivotCache.Refresh
Next i
End Sub

Macro to delete column if match get error

I am searhing values in Col A of sheet "YYY" with values from Col A sheet"XXX" if a match is found delete the entire row of the matched cell on sheet"YYY"
I get object required on this line FindValues(i, 1).Row.Delete I have beeen tring to correct this for a while now but failing
Thanks
Edit: updated
Sub FindReplace_Updated_Blanks()
Dim FindValues As Variant, SearchValues As Variant
Dim wsSource As Worksheet, wsTarget As Worksheet
Dim sLR As Long, tLR As Long, i As Long
Set wsSource = ThisWorkbook.Worksheets("XXX")
Set wsTarget = ThisWorkbook.Worksheets("YYY")
sLR = wsSource.Range("A" & wsSource.Rows.Count).End(xlUp).Row
tLR = wsTarget.Range("A" & wsSource.Rows.Count).End(xlUp).Row
SearchValues = wsSource.Range("A2:A" & sLR).Value
FindValues = wsTarget.Range("A2:A" & tLR).Value
For i = LBound(FindValues) To UBound(FindValues)
If Not IsError(Application.match(SearchValues(i, 1), wsTarget.Range("A2:A" & tLR), 0)) Then
wsTarget.Rows(i + 1).Delete
End If
Next i
End Sub
Change it for:
Sheets("YYY").Rows(i + 1).Delete
Since your range starts with a static "2" you don't need to use the range to find the row.

Macro to match colums is getting error

I want to check if cells in a column on sheet1 are in a column in sheet2 if it is then do nothing if it is not then past its value in the last row + 1
I get type mismatch on this line
If Application.Match(FindValues(i, 1), wsTarget.Range("A2:A" & sLR), 0) = False Then
Edit: This works
Thanks
'\\ IF cell found in match range then do sothing if not dosomthing else
Sub FindReplace_Updated_Blanks()
Dim FindValues As Variant
Dim ReplaceValues As Variant
Dim wsSource As Excel.Worksheet
Dim wsTarget As Excel.Worksheet
Dim sLR As Long
Dim tLR As Long
Dim i As Long
Sheets("Updated_Blanks").Select
Set wsSource = ThisWorkbook.Worksheets("Blanks")
Set wsTarget = ThisWorkbook.Worksheets("Updated_Blanks")
sLR = wsSource.Range("B" & wsSource.Rows.Count).End(xlUp).Row
tLR = wsTarget.Range("A" & wsSource.Rows.Count).End(xlUp).Row
FindValues = wsSource.Range("B2:B" & sLR).Value
For i = LBound(FindValues) To UBound(FindValues)
If Not IsError(Application.Match(FindValues(i, 1), wsTarget.Range("A2:A" & tLR), 0)) Then
Else
wsTarget.Range("A" & (tLR + 1)) = FindValues(i, 1)
End If
Next i
End Sub
You'll see generic messages like this when you use Application.WorksheetFunctions, or in this case Application.Match(). Most likely your Application.Match is returning a #N/A error, which VBA says is a "Type Mismatch" which is pretty generic.
You can see this error (Error 2042) if you assign the result of your Application.MAtch() formula to a variable and then print that variable value out. No worries though since #N/A is an expected result of the Match() Excel formula.

Resources