Trying to use VLookup across worksheet - excel

I have tried to use a VLookup code on a worksheet to use VLookup function to find prices on worksheet Parts list then fill this into worksheet Job Card Master using code in column D to fill column O to lastrow.
The code won't run the error says
"Application-defined or object-defined error"
Private Sub Up_Date_Prices_Click()
Dim JCM As Worksheet, PartsList As Worksheet
Dim JCMLastRow As Long, PartsListLastRow As Long, x As Long
Dim DataRng As Range
Set JCM = ThisWorkbook.Worksheets("Job Card Master")
Set PartsList = ThisWorkbook.Worksheets("Parts List")
JCMLastRow = JCM.Range("A" & Rows.Count).End(xlUp).row
PartsListLastRow = PartsList.Range("A" & Rows.Count).End(xlUp).row
Set DataRng = PartsList.Range("A2:B" & PartsListLastRow)
For x = 13 To JCMLastRow
On Error Resume Next
JCM.Range("O" & x).Value = Application.WorksheetFunction.VLookup( _
JCM.Range("D" & x).Value.DataRng, 2, False)
Next x
End Sub

Related

VLookup cannot find match

I try to perform VlookUp using VBA. Specifically I want to find Open prices for each Date (picture attached). But my code fails to find any matches. I Guess i have messed up ranges for vlookup but I cannot find the mistake.
Sub VlookUp()
Dim goalsWs As Worksheet, dataWs As Worksheet
Dim goalsLastRow As Long, dataLastRow As Long, x As Long
Dim dataRng As Range
Set goalsWs = ThisWorkbook.Worksheets("[FULL_TABLE][1]")
Set dataWs = ThisWorkbook.Worksheets("BTC-USD")
goalsLastRow = goalsWs.Range("A" & Rows.Count).End(xlUp).Row
dataLastRow = dataWs.Range("A" & Rows.Count).End(xlUp).Row
Set dataRng = dataWs.Range("A2:G" & dataLastRow)
For x = 2 To goalsLastRow
On Error Resume Next
goalsWs.Range("B" & x).Value = Application.WorksheetFunction.VlookUp( _
goalsWs.Range("A" & x).Value, dataRng, 4, False)
Next x
End Sub
I want to find Open prices from BTC-USD for each Date in FULL_TABLE
The problem was in the data type " the Excel worksheet function gets confused with the array of VBA dates which are always US-centric.
As suggested convert the dates to intrinsic values which you can do simply with the Value2 property, eg x = Range("Source").Value2"
So simply adding (2) after (value) has fixed the problem
goalsWs.Range("A" & x).Value2, dataRng, 4, False)

Count If in a User Defined range

I want a CountIf Formula in which JV_Rng (workbook 1 range) is the range and 9th column (and r row) of GL_Sheet (workbook 2) is the criteria. Upon running the code I receive error 1004 (application or object defined error)
The error is possibly due to my inability to include the JV_Rng in the Count If Formula. The whole code is as follows
'Filtering Range
Dim GL_Code As Single, GL_Rng As range, GL_LR As Long
Dim GL_Sheet As Worksheet
Set GL_Sheet = Workbooks("Deodar GL activities.xlsx").Worksheets("Sheet1")
GL_LR = GL_Sheet.range("B" & Rows.Count).End(xlUp).Row
GL_Code = Application.InputBox(Prompt:="Enter GL code", Title:="Generate GL", Type:=1)
Set GL_Rng = GL_Sheet.range("A4:R" & GL_LR).CurrentRegion.Offset(3, 0)
GL_Rng.AutoFilter Field:=6, Criteria1:=GL_Code
'Shift Rng into new sheet
Dim Tgt_Book As Workbook
Set Tgt_Book = Workbooks.Add
Dim tgt As Worksheet: Set tgt = Tgt_Book.Worksheets.Add
......
'Shift JV of that Code
Dim r As Long, JV_Rng As range
Set JV_Rng = tgt.range("J6:R" & GL_LR).Offset(5, 0)
For r = 5 To GL_LR
GL_Sheet.range("S" & r).Formula = "=COUNTIF(tgt.Range(JV_rng),R[r]C[9])"
Next r
The code is working successfully except for this part
GL_Sheet.range("S" & r).Formula = "=COUNTIF(tgt.Range(JV_rng),R[r]C[9])"
Please, replace:
For r = 5 To GL_LR
GL_Sheet.range("S" & r).Formula = "=COUNTIF(tgt.Range(JV_rng),R[r]C[9])"
Next r
with
tgt.range("S5:S" & GL_LR).Formula = "=COUNTIF(" & JV_Rng.Address(external:=True) & ", I5)"
No iteration needed.
In above code line I tried to convert your R[r]C[9] in A1 notation. Would you like counting I:I column cells values in S:S column? Otherwise, your formula was not targeting the correct ranges...

Setting a variable to a value produces Runtime Error 1004 : Application Defined or Object Defined Error

I am trying to get an array of values inside column A of Sheet 'Codes' and then get an array of all Sheets whose name starts with 'Price_Plan'*
My Start_Click Button is in a Sheet Named 'Macro' and I am trying to get data from column 'A' of Sheet named 'Codes'.
Private Sub Start_Click_Click()
Dim wS As Worksheet, LastRow As Long
Dim codeArray As Variant
Dim lastCell As String
Dim curSheet As Worksheet
Dim ArraySheets() As String
Dim x As Variant
Set wS = ThisWorkbook.Worksheets("Codes")
'Here we look in Column A
LastRow = wS.Cells(wS.Rows.Count, "A").End(xlUp).Row
lastCell = "A" & Str(LastRow)
'Here we create array of our row values
ThisWorkbook.Sheets("Codes").Activate
Set codeArray = ActiveSheet.range("A2:" & lastCell).Value
'Getting Array of Price Plan Sheets
For Each curSheet In ActiveWorkbook.Worksheets
If curSheet.Name Like "Price_Plan*" Then
ReDim Preserve ArraySheets(x)
ArraySheets(x) = curSheet.Name
x = x + 1
End If
Next curSheet
End Sub
I get this Runtime Error at:
Set codeArray = ActiveSheet.range("A2:" & lastCell).Value

Renaming Cell References Based on Cell Values

I am interested in renaming my document's cell references to easily perform calculations in other sheets. In the below code I am trying to name cell Di to the concatenation of the text in Ci and the year. I am using the below code and getting an error:
Sub or Function Not Defined
Any ideas?
Sub Rename()
Dim ws As Worksheet
Set ws = ThisWorkbook.ActiveSheet
For i = 1 To 50
Dim rng As Range
Dim str As String
Set rng = Range("D" & i)
rng.Name = Range("C" & i) & "_" & "2019"
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.

Resources