Extract the date and name and repeat it along the column

sofas

Active Member
Joined
Sep 11, 2022
Messages
468
Office Version
  1. 2019
Platform
  1. Windows
Hello. I want help extracting the name of the product from column C, provided there is the word "Restaurant*") The date from the H column
And it is repeated along the column down to the second condition so that it is extracted and repeated in the same way. The problem is that I used this code, it does a good job, but it is very slow, because the file consists of hundreds of rows and invoices, and it always extracts names and dates from the first invoice, even though it is not empty. Can I modify it and help me in another code that does the same task, but it starts when it finds the invoice is empty

Screenshot_3.png


VBA Code:
Sub Remplir_tout_all()
Dim nmax&, a$(), derlig&, tablo, i&, n&, C As Range, dat$, h&
Dim rng As Range
Set ST = Sheet1
 lr = ST.Range("a" & Rows.Count).End(xlUp).Row
With Sheet1
    nmax = Application.CountIf(.[c:c], "Restaurant*")
    ReDim a(nmax)
    derlig = .Range("C" & .Rows.Count).End(xlUp).Row + 1
    a(nmax) = "c" & derlig
    tablo = .Range("c1:c" & derlig)
    For i = 1 To derlig
        If Trim(tablo(i, 1)) Like "Restaurant*" Then a(n) = "c" & i: n = n + 1
    Next i
    Application.ScreenUpdating = False
     Range("a4:b" & lr).ClearContents
    .[A:B].HorizontalAlignment = xlCenter
    For n = 0 To UBound(a) - 1
        Set C = .Range(a(n))
        dat = Mid(Trim(C(5, 6)), 11, 10)
        h = .Range(a(n + 1)).Row - 1 - C(5).Row
        If h > 0 Then
            If IsDate(dat) Then C(6, -1).Resize(h) = CDate(dat)
            C(6, -0).Resize(h) = C(2)
        End If
    Next n
End With
End Sub
 
Ok, Now I understand. One question. Do you have any dates like: From Date 12/01/2023/ To Date 13/01/2023?

If yes, then how it starts and how it ends? How many days will be 12 and how many days will be 13?
The date should be extracted from column H No matter what date it is, you can run the code to see what it does
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Your code does a good job. What are you asking for then?
Yes, this is what was previously mentioned. The problem is when I put it on the original file, it works very slowly due to the large number of rows that range from 5000 rows that can be increased.
 
Upvote 0
This is as far as I can push:
VBA Code:
Sub test()
  Dim lRow As Long, ws As Worksheet, C As Range, j As Long, i As Long
  Set ws = Worksheets("Sheet1")
  Application.ScreenUpdating = False
  lRow = ws.Cells(Rows.Count, 8).End(xlUp).Row
  Set rSearch = ws.Range("C1:H" & lRow)
  Application.ScreenUpdating = False
  With rSearch
  Set C = .Find(what:="Restaurant", after:=rSearch(.Rows.Count, 1), _
        LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, _
        searchdirection:=xlNext, MatchCase:=False)
  i = C.Row + 5
  Do While j < lRow
    Set C = .FindNext(C)
    j = C.Row - 1
    ws.Range("A" & i & ":A" & j) = rSearch(i - 4, 1)
    ws.Range("B" & i & ":B" & j) = CDate(Mid(Trim(rSearch(i - 1, 6)), 11, 10))
    i = j + 6
  Loop
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
This is as far as I can push:
VBA Code:
Sub test()
  Dim lRow As Long, ws As Worksheet, C As Range, j As Long, i As Long
  Set ws = Worksheets("Sheet1")
  Application.ScreenUpdating = False
  lRow = ws.Cells(Rows.Count, 8).End(xlUp).Row
  Set rSearch = ws.Range("C1:H" & lRow)
  Application.ScreenUpdating = False
  With rSearch
  Set C = .Find(what:="Restaurant", after:=rSearch(.Rows.Count, 1), _
        LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByRows, _
        searchdirection:=xlNext, MatchCase:=False)
  i = C.Row + 5
  Do While j < lRow
    Set C = .FindNext(C)
    j = C.Row - 1
    ws.Range("A" & i & ":A" & j) = rSearch(i - 4, 1)
    ws.Range("B" & i & ":B" & j) = CDate(Mid(Trim(rSearch(i - 1, 6)), 11, 10))
    i = j + 6
  Loop
  End With
  Application.ScreenUpdating = True
End Sub
Thank you. Unfortunately, it is much slower than the first. Thank you for your interest.
 
Upvote 0
Sorry for that. While Loop was looping infinitely. Now I've fixed it. Can you test the speed like this please?
VBA Code:
Sub test()
  Dim lRow As Long, ws As Worksheet, C As Range, j As Long, i As Long
  Set ws = Worksheets("Sheet1")
  lRow = ws.Cells(Rows.Count, 8).End(xlUp).Row
  Set rSearch = ws.Range("C1:H" & lRow)
  Application.ScreenUpdating = False
  With rSearch
  Set C = .Find(what:="Restaurant", lookat:=xlPart, MatchCase:=False)
  i = C.Row + 5
  Do While j < lRow
    Set C = .FindNext(C)
    j = IIf(C.Row < j, lRow, C.Row - 1)
    ws.Range("A" & i & ":A" & j) = rSearch(i - 4, 1)
    ws.Range("B" & i & ":B" & j) = CDate(Right(Trim(rSearch(i - 1, 6)), 10))
    i = j + 6
  Loop
  End With
  Application.ScreenUpdating = True
End Sub
 
Last edited by a moderator:
Upvote 0
Sorry for that. While Loop was looping infinitely. Now I've fixed it. Can you test the speed like this please?
VBA Code:
Sub test()
  Dim lRow As Long, ws As Worksheet, C As Range, j As Long, i As Long
  Set ws = Worksheets("Sheet1")
  lRow = ws.Cells(Rows.Count, 8).End(xlUp).Row
  Set rSearch = ws.Range("C1:H" & lRow)
  Application.ScreenUpdating = False
  With rSearch
  Set C = .Find(what:="Restaurant", lookat:=xlPart, MatchCase:=False)
  i = C.Row + 5
  Do While j < lRow
    Set C = .FindNext(C)
    j = IIf(C.Row < j, lRow, C.Row - 1)
    ws.Range("A" & i & ":A" & j) = rSearch(i - 4, 1)
    ws.Range("B" & i & ":B" & j) = CDate(Right(Trim(rSearch(i - 1, 6)), 10))
    i = j + 6
  Loop
  End With
  Application.ScreenUpdating = True
End Sub
I actually tried something wrong with the first column I don't know what it is with shifting the values 5 rows up
 
Upvote 0
I can't test the code with your actual data because there are no restaurants in column C (Arabic sheet)

Donwload your sample from dropbox. I works with the test sheet (Sheet1)
 
Upvote 0
Upvote 0

Forum statistics

Threads
1,214,642
Messages
6,120,698
Members
448,979
Latest member
DET4492

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