Need a VBA Macro get data from one file to another + calculation

Could it be possible that because these originally are data dumps from SAP that they are formatted as text and not numbers
Yes, but the CPreport you posted, column "B" is not all text.
Some are numbers and those were the only ones the previous code was recognizing. The rest are text and all have an invisible leading and trailing chr160.
Column "I" of the DataSchedule.xlsm are 10 numeric characters formatted as text with no invisible characters.


Try adding
Code:
Dim order as Range
to the declarations
and changing the part setting the source to
Code:
'set where to find it in CPreport
    With srcWB
    src_lr = .Sheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
    Set src = srcWB.Sheets("Sheet1").Range("B4:B" & src_lr)
    End With
    'doctor the data
        For Each order In src
            order.Value = Replace(order.Value, Chr(160), "") * 1
        Next order

If that doesn't work you'll need to supply what you're really working with.
 
Great, glad to finally get it.

Now for Step 2

Your DSTool data now starts at row 7. Are rows 5 and 6 the same as the originally posted rows 1 and 2 ?
You haven't mentioned anything here about column K, are there any other "goal post moves" I should know about ?
Do you really need the duplicate rows deleted ? They can be hidden with filtering, once deleted there's no going back.
Is your intent to run the step2 macro separately (at a different time) or have Step1 and Step2 combined ?
 
I hadn't even thought about hiding the duplicates...That is a great idea. So here we go.

I have made one other minor change and DSTool now starts on Row 6. Row 5 is column headers. I also made a minor change to CPreport and deleted the headers and as a result your code was altered to look at (B1:B).

Ultimately, L is a helper Column that will drive step 2. So I want the result of Step 2 to populate into K.
The sum of the CP values associated with the Master in H will only need to populate into any one of the coordinating cells in K and the others will be hidden, but if its easier to code, it can go to all of them since the duplicates will be being hidden anyway.
Can the hiding be coded in as an automatic process?

Yes...I would like to merge Steps 1 and 2 into a single function.
 
NoS,
I had also posted this on MR.Excel and have received no response and was wandering if you might help with it as well.

What I need is VBA code to put a thick border between time groups of 06:00 - 13:59, 14:00 - 20:59, & 21:00 - 05:59 in order to visually separate chunks of time that align with my facility's shifts.

If you can help with this too, please just blend it in to the step 1 & 2 process. No need for a separate macro.

Again. Thank you very much for all of your assistance.
 
Hmmm... need to think about that last one.

Think for thick line would need to delete the duplicates. Would different colors of the date/time cell be acceptable ?
 
Actually, yes. Hadn't thought of that either. Since it is only serving as a visual definition a 3 color variance would be fine,

It would only need to affect column B then instead for going across the sheet. That being said. Hold off on this for now. I am going to try to make it work with "Conditional Formatting" Excel may just be able to do this on its own. Thank you.
 
Here's the workbook I've been using.

The CPSteps1and2 procedure and the Private Function MUST be in the same module.

In the VBA environment you can copy an entire module from one project(workbook) to another by dragging the module between projects.

Good luck with the project.

NoS
 

Attachments

  • MOCK_PERSONAL.xlsb
    16.2 KB · Views: 8
Robert, are you succeeding in coloring those time cells ?
 
Haven't really had a chance to look other than just trying to pull up the conditional formatting tab and looking at the options. What my thought was is that if I can figure out how tho make it work manually then I would just record a Macro with the steps needed and then cut and paste it into the macro I already have that formats the sheet with colors and font styles.

I had a question...

I like the automatic set up of the filtering, but what do I need to change to flip it from Default On to Default Off so that it does not automatically hide the duplicates but waits till I flip the switch?
 
I like the automatic set up of the filtering, but what do I need to change to flip it from Default On to Default Off so that it does not automatically hide the duplicates but waits till I flip the switch?
Why would you want to do that ?
How do you initiate the CPSteps1and2 procedure ?
 
I initiate the CP function with a control button. i Have 6 buttons across the top of the Sup-DSToo;l Reset, Get Data, Format, Stylize, Get CP Data. I do plan on consolidating them down to just Reset & Run but I haven't gotten there yet still working out little kinks and issues and it suits my needs at the moment to have them separate.

Why do I want the filter off by default?

The duplicate masters need to ultimately be hidden prior to printing the report; however, they need to be seen at first. It helps to identify if Load is Food, Mix, or Bev only in the instance that the scheduler does not mark it correctly. Also, I noticed that the "0" quantities in "K" are being hidden automatically. I need that not to happen because there are a considerable amount of orders that do not have CPs and if the "0" QTYs are hidden, so are those orders. I plan on doing a conditional format and turning the Zero's font white so it is not visible.
 
Where the macros are initiated from I like, there's no chance of DSTool not being the active sheet.

The second part about duplicate masters and zeros in the K column has me confused.


Knowing what I know now, I'm going back to your original post.

Step 1, return the value in "C" from CP to "L" in DS based on the order# in column "I". OK, I've got that.

Step 2, let's eliminate "I want to delete the duplicate lines", because you really don't want that as part of this macro.
So, what' left ? (the part I didn't include) ---> A SUMIF formula in column "M" giving the "L" quantities based on Master Ship# in "H".
Mentioning the largest value for each would indicate it's a running total wanted.

Here's a macro that gets to that point
Code:
Sub CPSteps()
    Dim srcWB As Workbook, dstWB As Workbook
    Dim strPath As String, srcName As String, dstName As String
    Dim src As Range, order As Range
    Dim rng As Range, cel As Range, fndON As Range
    Dim src_lr As Long, dst_lr As Long, Pick As Long
    
Application.ScreenUpdating = False

'path and name for workbooks
'strPath = "C:\Users\Family\My Documents\Dads\Work\Dock Schedule\"
strPath = "D:\Forum Stuff\2016\2016_07_01\RobertWP\"
srcName = "CPReport.xlsx"
dstName = "Sup-DSTool.xlsm"

'set source
    On Error Resume Next    'disable error notification
    Set srcWB = Workbooks(srcName)
    On Error GoTo 0         're-enable error notification
    'if not already open
    If srcWB Is Nothing Then
        Set srcWB = Workbooks.Open(strPath & srcName)
    End If

'set destination
    On Error Resume Next    'disable error notification
    Set dstWB = Workbooks(dstName)
    On Error GoTo 0         're-enable error notification
    'if not already open
    If dstWB Is Nothing Then
        Set dstWB = Workbooks.Open(strPath & dstName)
    End If

'set what to loop through on DSTool
    With dstWB.Sheets("DSTool")
    dst_lr = .Cells(Rows.Count, "I").End(xlUp).Row
    Set rng = .Range("I6:I" & dst_lr)
    End With

'set where to find it in CPreport
    With srcWB
    src_lr = .Sheets("Sheet1").Cells(Rows.Count, 2).End(xlUp).Row
    Set src = srcWB.Sheets("Sheet1").Range("B4:B" & src_lr)
    End With
    'doctor the data
        For Each order In src
            order.Value = Replace(order.Value, Chr(160), "") * 1
        Next order

'do the loop and find
    For Each cel In rng
        If cel.Value <> "" Then
            Set fndON = src.Find(What:=CDbl(cel.Text), _
                                LookIn:=xlValues, _
                                LookAt:=xlWhole, _
                                SearchOrder:=xlByRows, _
                                SearchDirection:=xlNext, _
                                MatchCase:=False)
            
            If Not fndON Is Nothing Then 'was found
                'transfer data
                Pick = Trim(fndON.Offset(0, 1).Value)
                cel.Offset(0, 3).Value = Pick
            End If
        End If
    Next cel

'close the source
    srcWB.Close False
    Set srcWB = Nothing

'put SUMIF formula into column M
    With ActiveSheet
        .Range("M6").Formula = "=SUMIF($H$6:$H6,H6,$L$6:$L6)"
        .Range("M6").AutoFill Destination:=Range("M6:M" & .Cells(Rows.Count, 2).End(xlUp).Row)
    End With

Application.ScreenUpdating = True

End Sub

From this point, can you re-explain what you're needing ?
 
This works perfectly! I changed the M to K. Originally when we first started this K was in use for something else but that went away and K works better for me than M. I used conditional formatting and set up a macro that works with a Finalize button (See Attached Files) that turns all Zero Values in K to white font so they are hidden along with a few other formatting issues that I did not want to include with the Stylize button.

The Macro Recorder makes a lot of Big Fat Ugly code, but it all works so I'm good. :)

I was also able to get the time slots to change color. I had to record a macro while I inserted a blank column between B & C, then I used "Text to Columns" to split the time away from the date. This allowed me to isolate the time slots in their groups and use conditional formatting. Works good except for a pop-up asking if I am sure I want to continue which is no issue.

The only thing I was wanting at this point to find out is if a separate macro could be created that, when its command button was used, would hide the rows with the duplicate Master Ship #s but only the ones that did not have the Grand Total of the SUMIF function.

Basically, whichever row of the Duplicate MS's that has the largest value in K for that grouping would remain un-hidden.

If this is not possible or just a considerable pain in the butt to try and code, then no worries. I am already very happy with it.


View attachment Sup-DSTool.xlsm & View attachment PERSONAL.XLSB

Thanks again NoS
 
Last edited:
I forgot to include the Raw Data file DockSchedule.xls. The Get Raw Data Button uses this file to pull from. View attachment DockSchedule.xls The other dockSchedule.xlsx that you have seen is semi-improved via the "Get Data" and "Format Sheet" Buttons. Each Command button works in sequence from Left to Right.
 
This works perfectly! I changed the M to K.
Don't think so, well, not yet.

The column using the running tally SUMIF formula for each MS that you have adjusted to go into column K can't be filtered to achieve what you want.
To accommodate the filtering you'll still need what I originally put into column K. You just need it somewhere else.

For what I had before, this is what I was going to suggest be assigned to a button on the sheet to toggle the filter On-Off. Changing the ">0" to "<>" will hide only the blanks.
Code:
Sub ToggleFilter()
    With ActiveSheet
        If .FilterMode = True Then
            .ShowAllData
        Else
            .Range("K5:K" & .Cells(Rows.Count, 2).End(xlUp).Row).AutoFilter Field:=1, Criteria1:=">0", VisibleDropDown:=False
        End If
    End With
End Sub


Should you decide splitting your original date/time, and changing the column of everything to the right of column B, is not what you really wanted, here's a way the shifts could be identified in the original B column without applying conditional formatting to 1,048,576 rows.
Code:
Sub ColorShifts()
    Dim t As Integer, shift As Integer, cel As Range
With ActiveSheet
    For Each cel In .Range("B6:B" & .Cells(Rows.Count, 2).End(xlUp).Row)
        t = Mid(cel.Value, 10, 2)
            If t >= 6 And t < 14 Then
                shift = 36 'yellow    'day shift
            ElseIf t >= 14 And t < 21 Then
                shift = 35 'green    'afternoon shift
            Else
                shift = 0 'no color    'night shift
        End If
        cel.Interior.ColorIndex = shift
    Next cel
End With
End Sub
 
Thank you NoS. I will take a look at those and see what I can figure out. As it sits I have 99% of what I need and I am going to run with it for a bit and try to work out a couple of other kinks.

Can you look at the Macro DSFormat1 and you'll see a section with the comment ' Re-configures Values of G based upon content of A.

I want to add in another set of IFs based upon the syntax of those already there but I am not sure how to write them because they have multiple criteria.

Here is what I have (not sure if you can nest IF/AND's like this in VBA):

Code:
If Range(" [B]A[/B] " & r ).Value = "[B]L[/B]" And  
    If InStr(Range(" [B]E[/B] " & r).Value, "") And 
    If InStr(Range(" [B]J[/B] " & r).Value, "") Then
        Range(" [B]H[/B] " & r).Value ="[B]No Docs[/B]" 

End If

I know the syntax isn't right, but the idea is that IF A="L", E="", and J="" then H="No Docs". All 3 criteria need to be met


Code:
If Range("[B]A[/B]" & r[B]).[/B]Value = "[B]L[/B]" And 
    If InStr(Range("[B]C[/B]" & r).Value, " ") And
    If InStr(Range("[B]H[/B]" & r).Value, " ") And
    If InStr(Range("[B]I[/B]" & r).Value, " ") And 
    If InStr(Range("[B]J[/B]" & r).Value, " ") Then
        Rows(r).Delete

End If

This one is the same sort of thing; however 5 criteria need to be met. A= "L", C, E, H, I, and J all need to = " "
 
Two different ways to write things like that...
Code:
With only the first IF.

If  first thing  And  second thing  And  third thing  Then
     Do something
End If
or
Code:
Using IF for each requirement.

If first thing  Then
     If second thing  Then
         If third thing  Then
             Do something
         End If
     End If
End If
 
NoS,
Can I run a hypothetical past you?
What if the Case Pick Macro did this:
1.) Opens another Tab in Sup-DSTool (Sheet2)
2.) Makes an exact copy of Columns H & I of DSTool and put them in A & B of Sheet 2
3.) Then your code that finds the matching data in CPReport does exactly what it does now but uses Sheet2 as its working platform and puts its results in Column C
4.) Then the running total for the Master Ships would work in Column D of Sheet2.
5.) Meanwhile, since Sheet2 has all the working data…The Duplicate Masters on DSTool (Sheet1) can be hidden or deleted; whichever one works best.
6.) Then the only the Grand Total of the SUMIF for the MS groupings would be matched to the remaining MS# on DSTool and placed in Column K accordingly.
7.) Reset Button Code modified to Kill Sheet 2 for next run of file with new data

Would this work?
 
Don't know, I can't even visualize that.


I'd just take what I had going into K and use a different column, say maybe P, then apply the filtering to that column instead of K.

Go back to the module in post #28.
After adjusting everything to accomodate the new column for the time, in this part near the end
Code:
            filtRng.AutoFilter Field:=1, Criteria1:=crit
            'put L total in last K showing
            .Cells(GetFilteredRangeBottomRow, "K").Value = Ltot
        End If
    Next j
    
    'remove last filter
    filtRng.AutoFilter
    'show results
    filtRng.AutoFilter Field:=4, Criteria1:=">0"
End With
change the "K" to whatever column you decide to use
and remove
'show results
filtRng.AutoFilter Field:=4, Criteria1:=">0"

also change the ToggleFilter procedure to use the appropriate column.
 
Back
Top