Use of VBA code or otherwise

Jar888

Board Regular
Joined
Jan 15, 2022
Messages
61
Office Version
  1. 2016
Platform
  1. Windows
Hi there, is there a way to use VBA code or otherwise to make the left hand side look like the right as per the example excel snip I've attached?

I pull the data in at the moment as a block from another sheet into the left hand sides format. The only thing I really care about is the drill/single/twin/cut&drop numbers though. Everything else is mute. But the tricky part comes where the name might not match the heading in the same row. Generally speaking, each 4 rows of a machine are operated by the person at the top of the machine# and the others are just trainees.

Extract.PNG


1644645043101.png
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
As an example of what the data looks like today, and a bit better color representation on which cells are associated with what;
1644711289463.png
 
Upvote 0
Try this code
Note it requires two cells to be named, to tell the code where to find the data, and where to put the result
The rangename topData must be given to the top left title cell for the data - in the example provided, it would be A1
The rangename Result must be given to the top left title cell where the result must go- in the example provided, it would be P1

To insert the code,
  • go to the VBA editor (Alt F11)
  • insert a code module from the Insert menu item
  • a blank sheet comes up (if not blank, delete what is there)
  • paste the code below
  • assign the CollateData macro to your button
VBA Code:
Option Explicit
Option Base 1

Sub CollateData()
  Dim D, rows, r, cols, c, sets, s, n
  With Range("topData")
    D = .Offset(1, 0).Resize(.End(xlDown).Row - .Row, .End(xlToRight).Column - .Column + 1)  'read in data
  End With
  rows = UBound(D, 1): sets = rows / 4  'get dimensions
  cols = UBound(D, 2)
  ReDim A(sets, cols)
 
  For s = 1 To sets 'for each set of 4 rows
    For r = 0 To 3 'for each row in set
      n = n + 1
      For c = 1 To cols 'for each col of that row
        If A(s, c) = "" And D(n, c) <> "" Then A(s, c) = D(n, c) 'insert value from data if not already set
      Next c
    Next r
  Next s
 
  With Range("Result").Offset(1, 0)
    .Resize(1000, cols).ClearContents
    .Resize(sets, cols) = A
  End With
 
End Sub
 
Upvote 0
That would be a massive amount of typing for a helper to test with that actual sample data! ;)
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in

Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.


Here is another option to try with a copy of your workbook.

VBA Code:
Sub Jar888()
  Dim rCrit As Range
  Dim lr As Long
  
  Set rCrit = Range("N1:N2")
  rCrit.Cells(2).Formula = "=COUNT(I2:L2)"
  lr = Range("A" & rows.Count).End(xlUp).Row
  Range("A1:L" & lr).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rCrit, CopyToRange:=Range("P1"), Unique:=False
  rCrit.ClearContents
  With Range("S2:S" & Range("P" & rows.Count).End(xlUp).Row)
    .Formula = Replace("=INDEX(D$2:D$#,MATCH(1,INDEX((C$2:C$#=R2)*(D$2:D$#<>""""),0),0))", "#", lr)
    .Value = .Value
  End With
End Sub
 
Upvote 0
Hey thanks for that! I feel dumb now for not doing this sooner.

Here's a post of above, below;

Test.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1DateShiftMachine #OperatorDH/UHHeadingPlanDiameterDrillSingleTwinCut & DropDateShiftMachine #OperatorDH/UHHeadingPlanDiameterDrillSingleTwinCut & Drop
212/02/2022DS08156UH54mm12/02/2022DS8159JamieUH45 EM 284 523 D DAC W1INFILLS54mm12
312/02/2022DS08156UHSERVICE DAY54mm12/02/2022DS8159JamieUH52 OL 453 570 DPCB UH V1D/P CABLES54mm63
412/02/2022DS08156UH54mm12/02/2022DS8158SophieUH26 GRN 580-520-GSIN# 8229 QBC STORE54mm152
512/02/2022DS08156UH54mm12/02/2022NS8156KateUH57 CY 645 585 GSIN # 7968INCYCLE CABLES54mm9090
612/02/2022DS08159JamieUH54mm12/02/2022NS8159NigelUH52 OL 453 570 DPCB UH V1D/P CABLES54mm63125.7
712/02/2022DS08159ShaneUH45 EM 284 523 D DAC W1INFILLS54mm1212/02/2022NSDRI004ChrisUH45 SCA 518-537-A-DDR-W1CBS 154mm90
812/02/2022DS08159UH52 OL 453 570 DPCB UH V1D/P CABLES54mm63
912/02/2022DS08159UH54mm
1012/02/2022DS08158SophieUH26 GRN 580-520-GSIN# 8229 QBC STORE54mm152
1112/02/2022DS08158DavidUH57 CY 645 585 GSIN # 7968INCYCLE CABLES54mm
1212/02/2022DS08158UH54mm
1312/02/2022DS08158UH54mm
1412/02/2022DSDRI004AlexUHSERVICE DAY54mm
1512/02/2022DSDRI004UH54mm
1612/02/2022DSDRI004WillyUH54mm
1712/02/2022DSDRI004TristanUH54mm
18
1912/02/2022NS08156UH57 CY 645 585 GSIN # 7968INCYCLE CABLES54mm9090
2012/02/2022NS08156KateUH54mm
2112/02/2022NS08156UH54mm
2212/02/2022NS08156UH54mm
2312/02/2022NS08159UH52 OL 453 570 DPCB UH V1D/P CABLES54mm63125.7
2412/02/2022NS08159NigelUH54mm
2512/02/2022NS08159UH54mm
2612/02/2022NS08159UH54mm
2712/02/2022NS08158UH54mm
2812/02/2022NS08158JakeUH54mm
2912/02/2022NS08158UH54mm
3012/02/2022NS08158UH54mm
3112/02/2022NSDRI004ChrisUH45 SCA 518-537-A-DDR-W1CBS 154mm90
3212/02/2022NSDRI004UH54mm
3312/02/2022NSDRI004JesseUH54mm
3412/02/2022NSDRI004TarleaUH54mm
CB EXCTRACTOR
 
Upvote 0
Thanks for the XL2BB sample. Given that, my previous code will not work. The sample data shows that the apparently vacant cells in columns I:L actually contain hidden zeros. Same with column D. ;)

Further, I hadn't picked up that machine numbers may repeat further down column C. I will rethink my code and post again if I can come up with a solution. :)
 
Upvote 0
See how this one goes. For that sample data in A:L it produces the same values as you have in P:AA.
Changed code lines highlighted

Rich (BB code):
Sub Jar888()
  Dim rCrit As Range
  Dim lr As Long
  
  Set rCrit = Range("N1:N2")
  rCrit.Cells(2).Formula = "=COUNTIF(I2:L2,"">0"")"
  lr = Range("A" & rows.Count).End(xlUp).Row
  Range("A1:L" & lr).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=rCrit, CopyToRange:=Range("P1"), Unique:=False
  rCrit.ClearContents
  With Range("S2:S" & Range("P" & rows.Count).End(xlUp).Row)
    .Formula = Replace("=INDEX(D$2:D$#,MATCH(1,INDEX((C$2:C$#=R2)*(D$2:D$#<>0)*(B$2:B#=Q2),0),0))", "#", lr)
    .Value = .Value
  End With
End Sub
 
Upvote 0
If it helps any, the machine numbers will always be in the same spot every time I pull the information. So rows 2 - 5 will always be assosciated with DS (Dayshift), 6-9 etc. etc.

Also, it's probably picking up hidden 0's as it's pulling the data from another sheet using;

='[ProdTrak CB SEC Sheet.xlsm]Shift Plan'!C25

Would changing it to the below solve this?;

=IFERROR(='[ProdTrak CB SEC Sheet.xlsm]Shift Plan'!C25,"")
 
Upvote 0
Sorry, no. I displayed the 6 dates at the start, then #N/A for operator and blank everything else.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,948
Members
449,095
Latest member
nmaske

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