vba date partial text string look up

tonyotter

Board Regular
Joined
Dec 14, 2006
Messages
58
I Have been away from using excel / vba for quite some time, and would really appreciate some assistance from my old friends at the Mr Excel community.

I am looking to run a vba code to output some data from a large spreadsheet based on date, sitename, and a textstring lookup.

The basics are: from the source sheet I need to summarise the sum of the values in column W "post duration" based on all entries by date, per site (based on the last 4 characters of the text string in column R "post name").
And finally to sum any entries (By date) that have the term "SUB" (Including any variation of the term. e.g. "SUBS") in column B "Employee ID"

Source data is as below:


Book1
ABCD
3ABRW
4DateEmployee IDPost's namePost's duration
505/08/2018SUB6SO DBT112
605/08/2018SUB1SO EDI412
705/08/2018SUB2SO EDI412
805/08/2018SUB3SO EDI412
905/08/2018SUB4SO EDI412
1005/08/201828223SO GLA112
1106/08/201828458SO DBT112
1206/08/2018SUB6SO DBT112
1306/08/2018SUB1SO EDI410.5
Sheet2



Output data I am looking achieve:



Book1
ABCD
1Output
2DateSiteHoursSub hours
3Multiple occasions of date (Column A)Based on cell values (Text string) last 4 characters end with site codeSum of column W based on Date and siteHour where text in column employee ID include the text "SUB"
4
5Example output
601/02/2019EDI4225.548
Sheet1


Any assistance gratefully received
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I can't see any relationship between the sample source data and the output data.
- If there is a relationship can you spell it out some more?
- If there isn't a relationship, can you provide some sample data and expected output that are related?
 
Upvote 0
The below is some sample data of a single site (Although the data is normally a mix of numerous sites)


Book1
ABCD
1DateEmployee IDPost's namePost's duration
201/08/201821662SO EDI412
302/08/201821662SO EDI412
407/08/201821662SO EDI412
508/08/201821662SO EDI412
609/08/201821662SO EDI412
701/08/201821666SO EDI410
802/08/201821666SO EDI410
903/08/201821666SO EDI410
1006/08/201821666SO EDI44.5
1108/08/201821666SO EDI410
1209/08/201821666SO EDI410
1305/08/2018SUB3SO EDI412
1405/08/2018SUB4SO EDI412
1506/08/2018SUB1SO EDI410.5
1606/08/2018SUB2SO EDI412
1706/08/2018SUB3SO EDI412
1806/08/2018SUB4SO EDI412
1907/08/2018SUB1SO EDI412
2007/08/2018SUB2SO EDI412
2107/08/2018SUB3SO EDI412
2207/08/2018SUB4SO EDI412
2308/08/2018SUB1SO EDI412
2408/08/2018SUB2SO EDI412
2508/08/2018SUB3SO EDI412
2608/08/2018SUB4SO EDI412
2709/08/2018SUB1SO EDI412
2809/08/2018SUB2SO EDI412
2909/08/2018SUB3SO EDI412
3009/08/2018SUB4SO EDI412
3110/08/2018SUB1SO EDI412
3210/08/2018SUB2SO EDI412
3310/08/2018SUB3SO EDI412
3410/08/2018SUB4SO EDI412
3511/08/2018SUB1SO EDI412
3611/08/2018SUB2SO EDI412
3711/08/2018SUB3SO EDI412
3811/08/2018SUB4SO EDI412
Sheet4


The required out puts based on the sample below would be


Book1
ABCD
1DateSiteHoursSub hours
201/08/2018EDI4220
302/08/2018EDI4220
403/08/2018EDI4100
505/08/2018EDI42424
606/08/2018EDI45146.5
707/08/2018EDI46048
808/08/2018EDI47048
909/08/2018EDI47048
1010/08/2018EDI44848
1111/08/2018EDI44848
Sheet3


Where the hours data refers to the sum of all numbers in column D "posts duration" which have EDI4 as part of the text string in the cell in column C "posts name" for the particular date. The number in column D "Sub hours" are made up of the data in column D "posts duration" which have EDI4 in the text string AND has SUB as part of the text string in column B "employee ID"

Hope this makes a little more sense.

Many thanks for the assistance
 
Upvote 0
Thanks for the additional data.

This code assumes
- original data in Sheet4
- results go in Sheet3

Change the sheet names in the code if required and test in a copy of your workbook (the code removes any existing data from the results sheet).

Rich (BB code):
Sub Calc_Hours()
  Dim a As Variant, b As Variant
  Dim dh As Object, dsh As Object
  Dim i As Long
  Dim s As String
  
  Set dh = CreateObject("Scripting.Dictionary")
  Set dsh = CreateObject("Scripting.Dictionary")
  With Sheets("Sheet4")
    a = .Range("A2", .Range("D" & .Rows.Count).End(xlUp)).Value
  End With
  For i = 1 To UBound(a)
    s = a(i, 1) & " " & Right(a(i, 3), 4)
    dh(s) = dh(s) + a(i, 4)
    dsh(s) = dsh(s) + IIf(Left(a(i, 2), 3) = "SUB", a(i, 4), 0)
  Next i
  Application.ScreenUpdating = False
  With Sheets("Sheet3")
    .UsedRange.ClearContents
    With .Range("A2").Resize(dh.Count)
      .Value = Application.Transpose(dh.Keys)
      .TextToColumns DataType:=xlDelimited, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 4), Array(2, 1))
      .Offset(, 2).Resize(, 2).Value = Application.Transpose(Array(dh.Items, dsh.Items))
    End With
    .Range("A1:D1").Value = Array("Date", "Site", "Hours", "Sub Hours")
    .UsedRange.Columns.AutoFit
    .Activate
  End With
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Peter.

Many thanks for this. The code works great with my test spreadsheet. However except that I have tried to change the source sheet ranges to pull the data for use in my original sheet without any success.

Whilst the columns in the sample data were columns A,B,C and D. The data I need to pull from in the actual source sheet are non consecutive columns - As in the original post - Date (Column A), Employee ID (Column B), post's name (Column R) posts duration (Column W).

Any further help very much appreciated.
 
Upvote 0
Get the source data like this instead. Array(1, 2, 18, 23) represents column A, B, R & W
Rich (BB code):
With Sheets("Sheet4")
  <del>a = .Range("A2", .Range("D" & .Rows.Count).End(xlUp)).Value</del>
  a = Application.Index(.Cells, Evaluate("row(2:" & .Range("A" & .Rows.Count).End(xlUp).Row & ")"), Array(1, 2, 18, 23))
End With
 
Upvote 0
Solution
Hi Peter,

Apologies for the delay in responding. That now works perfectly. Thank you. I have been away from excel and vba for a while, so I will spend a bit of time breaking down the vba for my own understanding.

Thanks again.
 
Upvote 0
Hi Peter,

Apologies for the delay in responding. That now works perfectly. Thank you. I have been away from excel and vba for a while, so I will spend a bit of time breaking down the vba for my own understanding.

Thanks again.
You're welcome. Thanks for letting us know. :)
 
Upvote 0

Forum statistics

Threads
1,214,872
Messages
6,122,025
Members
449,060
Latest member
LinusJE

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