VBA - If match, fill in Date and time in cell

Engalpengal

New Member
Joined
May 10, 2023
Messages
43
Office Version
  1. 365
Platform
  1. Windows
Hello
I need help from your clever minds....again.
What I want is the following-

Areas:
Search key: Sheets("PL").Range("J7")
- This is a order no that is manualy typed in

Search area: Sheets("Ordre").Range("C13:C2000")
- Ordre->Listed product lines, Column "C" is the first culumn in product row, last column is "P"
- There can be more than one product line with the same order no

Task:
I want the program to look through Sheets("Ordre").Range("C13:C2000") and see if there is a match between "Search key" and the listed products (can be more then one hit).
If there is a match, the program needs to fill inn "Today`s date and time" in the same matched row, column "AK"
When this is done, i also want the program to clear the value in Sheets("PL").Range("J7"). The cell formatting must be intact.

unfortunately, i can not share the content of the excel sheets. The reason is that there is some information that is sensitive.

Hope that the info i have granted is good enough for you to understand the issue and help me out with a solution :)
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Try this on a copy of your Workbook as it may cause unexpected results.
VBA Code:
Sub getMatch()
Dim wb As Workbook, wsPL As Worksheet, wsOrd As Worksheet
Dim srchRange As Range, found As Variant, foundNext As Variant, start As Variant
srchKey As String
Set wb = ThisWorkbook
Set wsPL = wb.Worksheets("PL"): Set wsOrd = wb.Worksheets("Ordre")
srchKey = wsPL.Range("J7")
Set srchRange = wsOrd.Range("C13:C200")
start = srchRange.Find(srchKey).Address
found = start
Do
foundNext = srchRange.FindNext(Range(found)).Address
found = foundNext
foundRow = Right(foundNext, Len(foundNext) - InStr(2, foundNext, "$"))
ws.Cells(foundRow, "AK") = Date
Loop Until start = foundNext
wsPL.Range("J7").ClearContents
End Sub
 
Upvote 0
Solution
unfortunately, i can not share the content of the excel sheets. The reason is that there is some information that is sensitive.
If the suggestion above does not do what you want, surely it would not be too hard to create a small dummy data set with any sensitive data replaced by non-sensitive data?
 
Upvote 0
Hello again and thanks for the response. The above programming gave some Error. Will it help if i share a print screen? We are not allowed to download any programs thats not been through security check (that can take months).
 

Attachments

  • Page2_LP-1.jpg
    Page2_LP-1.jpg
    249 KB · Views: 8
  • Page1_Ordre-1.jpg
    Page1_Ordre-1.jpg
    245.7 KB · Views: 9
Upvote 0
Try this on a copy of your Workbook as it may cause unexpected results.
VBA Code:
Sub getMatch()
Dim wb As Workbook, wsPL As Worksheet, wsOrd As Worksheet
Dim srchRange As Range, found As Variant, foundNext As Variant, start As Variant
srchKey As String
Set wb = ThisWorkbook
Set wsPL = wb.Worksheets("PL"): Set wsOrd = wb.Worksheets("Ordre")
srchKey = wsPL.Range("J7")
Set srchRange = wsOrd.Range("C13:C200")
start = srchRange.Find(srchKey).Address
found = start
Do
foundNext = srchRange.FindNext(Range(found)).Address
found = foundNext
foundRow = Right(foundNext, Len(foundNext) - InStr(2, foundNext, "$"))
ws.Cells(foundRow, "AK") = Date
Loop Until start = foundNext
wsPL.Range("J7").ClearContents
End Sub
FIrst i god an error on following line:
srchKey As String --> Compile error: Statement invalide outside Type Block
I typed Dim in front

Then i got an arror at following line:
ws.Cells(foundRow, "AK") = Date --> Run-time error '424' Object required

What do you recommend doing here?
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

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