vba code for matching a cell with range of cells in a different worksheet.

fthomas

New Member
Joined
Aug 15, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
HI. I am new to vba and i am trying to compare a value in sheet 1 which is a date eg. Aug 2022 (coming from a formula) with sheet 2 where i want to insert the Aug 2022 value if its not already there.
My code looks like this .. The Application match is not able to match values and returning False.Can someone please help
VBA Code:
Sub Button16_Click()
'In this example I am Copying the Data from Sheet1 (Source) to Sheet2 (Destination)
'Sub sbCopyRangeToAnotherSheet()
Dim CellMonth As Variant
CellMonth = Application.Match(Sheets("Month Over Month").Range("a1:$a$2000").Value, Sheets("Income and Expense").Range("c20").Value, 0)
MsgBox IsError(CellMonth)

If Not IsError(CellMonth) Then

Sheets("Income and Expense").Range("$L$17").Copy
Sheets("Month Over Month").Range("B").End(xlUp).Offset(0, 1).PasteSpecial Paste:=xlPasteValuesAndNumberFormats
Else
Sheets("Income and Expense").Range("$L$17").Copy
Sheets("Month Over Month").Range("B" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats

Application.CutCopyMode = False
End If
'End Sub
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You should try changing "Application.Match" to "Worksheetfunction.Match" and see if that works. After seeing a potential error I'm going to write this out. Worksheetfunction.Match(Sheets("Income and Expense").Range("c20").value, Sheets("Month Over Month").Range("a1:$a$2000"),0)

Tell me how this works.

Best of luck!
 
Upvote 0
Thank you for the resonse, I was able fix this by below
VBA Code:
Application.Match(Sheets("Income and Expense").Range("a12").Value, Sheets("Month Over Month").Range("a:a").Value, 0)
You should try changing "Application.Match" to "Worksheetfunction.Match" and see if that works. After seeing a potential error I'm going to write this out. Worksheetfunction.Match(Sheets("Income and Expense").Range("c20").value, Sheets("Month Over Month").Range("a1:$a$2000"),0)

Tell me how this works.

Best of luck!
 
Upvote 0
Solution

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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
Back
Top