PivotTable to forecast by date - excel

I trying to get this forecast by date into a PivotTable so I can drill down the info for a CSV import. The pivot wizard recognises my data all wrong.
How can I get SKU codes as ROWS, all the dates dynamic to additions as COLUMNS and the forecast values as VALUES?
Excel Data
Failure
I want the Pivot to mirror what I have, so I can drill the data down to the format
SKU, Date, Qty
SKU, Date, Qty
SKU, Date, Qty
SKU, Date, Qty
If there is an alternative but easier route that would be great.

I think the problem you got is how you are storing your original data. Right now you are doing something like this:
And to make the Pivot Table work properly, you need tabular design like this:
Notice that same SKU code can appear more than once, but at different dates (row 12 and 19, row 13 and 20, and so on).
With this tabular design, then you can do a Pivot Table like you want:
So you need to transpose the data. I've used some formulas that may help you out a little bit. If you got a lot of data,then I strongly suggest you to use them and then paste values, or the file will be heavily overcharged, slow and so on. This is a way to do it.
First, count how many SKU codes you got (in my image, there are just 7)
Second, count how many dates you got (in my image, there are just 2)
Below original data, copy the complete group of SKU codes (7) as many times as dates (twice in my case) in the same column.
In next column (DATE field), you can use this formula: =INDEX($B$1:$C$1;1;COUNTIFS($A$12:A12;A12))
In next column (VALUE field) you can use: =INDEX($B$2:$C$8;MATCH(A12;$A$2:$A$8;0);MATCH(B12;$B$1:$C$1;0))
Just drag down and you should get a new range with data transposed properly. And that new range is a perfect source for a Pivot Table and do what you want. Paste values!
If you add new extra rows to this new range, you can just change the source of your Pivot Table, and then that new data will be instantly added to your Pivot Table with the design you want.
After checking everything is right, delete old data, of course!
I've uploaded a sample to my Gdrive in case it may help for you. https://drive.google.com/open?id=1ElEPkk5V3QkME7yLPQuGnU7-wd6fQEd_
Hope you can adapt this to your needs.

Thanks for taking the time to assist me, I managed to find a solution that I thought I'd share. This doesn't go down the PivotTable route but instead gives me the final result that I needed.
Sub NormaliseTable()
Application.Calculation = xlManual
' start with the cursor in the table
Dim rTab As Range, C As Range, rNext As Range
Set rTab = ActiveCell.CurrentRegion
If rTab.Rows.Count = 1 Or rTab.Columns.Count = 1 Then
MsgBox "Not a well-formed table!"
Exit Sub
End If
Workbooks.Add ' the sheet for the results
Set rNext = Range("A1")
For Each C In rTab.Offset(2, 1).Resize(rTab.Rows.Count - 1, _
rTab.Columns.Count - 1).Cells
If Not (IsEmpty(C.Value) Or C.Value = "0") Then
rNext.Value = rTab.Cells(C.Row - rTab.Row + 1, 1)
rNext.Offset(0, 1).Value = rTab.Cells(1, C.Column - rTab.Column + 1)
rNext.Offset(0, 2).Value = C.Value
Set rNext = rNext.Offset(1, 0)
End If
Next
ChDir "\\Client\T$"
ActiveWorkbook.SaveAs Filename:="\\Client\T$\M3-FC-IMPORT.csv", FileFormat _
:=xlCSV, CreateBackup:=False
ActiveWorkbook.Save
ActiveWindow.Close
Application.Calculation = xlAutomatic
End Sub
Without the Application Calculation going to manual it would take hours, so I turn it off and on with the macro and its almost instantaneous!
Thanks
Sam

Related

VBA Transpose From Specific Number of Cells Above

I've been working on a VBA code, this is only a minor part of my project for reviewing accounting transactions.
The code not included inserts blank rows where there have been transactions using multiple account names. I want to transpose the multiple account names on each transactions where necessary. The "RowNum" code references the number count of account names on that particular transaction. I was hoping to insert that number into my FormulaR1C1.
Sub cellnum()
RowNum = "=R[-1]C[4]"
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=transpose(R[-" & RowNum.Value & "C[-1]:R[-1]C[-1])"
End Sub
If you want to put the formula in a different cell than the active cell you need to set the range for that cell first.
for example something like should give the right result
Set rng = Range("A1")
rng.FormulaR1C1 = "=transpose(R[-" & RowNum.Value & "C[-1]:R[-1]C[-1])"
Im new to VBA and still learning but I think this is the way to go.

Getting Excel to Copy Data From One Cell to Another Depending on Date

Apologies in advance as this is my first time posting something on this site and am not the best at explain issues.
I have a spread sheet, this has production data such as meters daily, meters monthly etc. These values are updated by adding TAGS from a PLC using Rockwell VantagePoint Excel add-in (if your unfamiliar with this it shouldn't matter this part is not what I am struggling with)
I need I way to copy data from one cell to another cell on the same sheet at month end. Basically the Meters monthly field needs to copied into another cell at the end of the month to record meters run for that month. The monthly meters run resets back to 0 at the end of the month.
Basically I need to copy the value in J7 into the corresponding month in W column at the end of that month. If it could ignore the year that would be advantageous as I don't need it to keep the old values and would mean I just need one column.
I have some experience at MS-Excel, also VBA but mainly in MS-Access never in MS-Excel. If answers could be explained as simply and hands on as possible it would be appreciated.
After Googling the issue I came across this formula and changed the ranges to fit my sheet but Excel doesn't like it saying it contains an error
=QUERY( A1:B6; "select B where A =date """&TEXT(TODAY();"yyyy-mm-dd")&""" "; 0
Sorry again if I haven't explained myself properly.
If your workbook isn't guaranteed to be open at the end of each month I would update the value every time it gets opened, like(Should be placed in ThisWorkbook):
'Runs when you open the workbook
Private Sub Workbook_Open()
'Loops through U3 to the last used cell in that column
For Each c In Range(Cells(3, 21), Cells(Rows.Count, 21).End(xlUp))
'Applies the J7 value to the current month and exits the sub
If Month(c) = Month(Now) Then c.Offset(, 2).Value = [J7]: Exit Sub
Next c
End Sub
Also, not that it matters but, I would apply the following formula in U3:U14 to always get the correct dates:
=EOMONTH(DATE(YEAR(TODAY()),ROW()-2,15),0)
Okay, I'm still not super sure what the question is and I know more Access VBA than Excel VBA, but here's something that might help to find a solution.
You can make a check date function that returns a Boolean value:
Public Function EoMonthCheck() As Boolean
Dim eo_month As Date, today As Date
eo_month = Format(WorksheetFunction.EoMonth(Now(), 0), "yyyy-MM-dd")
today = Format(Now(), "yyyy-MM-dd")
If today = eo_month Then
EoMonthCheck = True
Else
EoMonthCheck = False
End If
End Function
And the,, to add a value to the "W" column, we might use something like this:
Public Function AppendValue(Optional target_cell As String = "J7")
''' This could be a subroutine, too, I guess, since we're not returning anything.
Dim i As Integer
''' Activate whatever sheet you want to work with
Worksheets("Sheet1").Activate
If EoMonthCheck() = True Then
''' Look up the bottom of the 'W' column and find the first non-empty cell
''' Add 1 to that cell to get you to the next cell (the first empty one).
i = Cells(Rows.Count, "W").End(xlUp).Row + 1
''' Set the value of that empty cell in the 'W' column to the value of 'J7'
''' which will happen after we evaluate whether it is the end of the month.
Cells(i, "W").Value = Range(target_cell).Value
End If
Then, you could maybe trigger that each time the workbook opens.

Sumif loop through data groups vba

If this is answered elsewhere please advise...
I am trying to come up with a sumif that will sum column D if Column A criteria = "PL211010" & "PL203000". The problem is that I need the macro to sum by groups rather than the whole sheet (see pic)
Not sure where to even start, should i be using a For Next loop? or is a loop even necessary?
Important note This pivot dynamically changes month to month. The accounts are not static, they can either increase or decrease. So placing a static formula will not work...
The code below is my attempt to get this to work, just trying to give a baseline.
Dim nRowMax, nRowNow As Integer
Application.ScreenUpdating = False
Cells(4, 1).Select
Selection.End(xlDown).Select
nRowMax = Selection.Row
For i = nRowMax To 4 Step -1
If Left(Cells(i, 1), 8) = "PL211010" & "PL203000" Then
WorksheetFunction.SumIf (Cells(i, 4))
End If
Next i
Application.ScreenUpdating = True
This can be done without VBA by writing formulas directly on the datasource as #ashleedawg stated, and that would probably be your easiest option, or by writing them directly off the pivot. I have done a mock-up example below that will work for the case you provided:
Here's a screenshot:
Change the Pivot Table Layout as I have instructed in the picture in cell D1
Use the formula in cell H4: =SUM(SUMIFS($F$4:$F$11,$D$4:$D$11,D4,$E$4:$E$11,{"A","B"}))
Drag down as needed.
Now, it's late for me and I did this quick, so I realize it's repetitive and a bit messy. For that you can simply organize a list of single elements and write the formulas off that list.
Alternatively, you can write the formulas directly off the data source by modifying the range arguments of the formula I provided.

Formula to pull text from one sheet in excel to another to summarize (not numbers)

I have a spreadsheet that has a tab for each research location. There is a section on the sheet that has several columns. Three of the columns are as follows: 1 lists action items (text) 1 lists who is responsible (text) and 1 lists the due date (date field). The rows in this same "table" represent categories. In many cases there is an action item only in one or two categories or maybe none at all for some.
I would like to query each tab that represents a research site and pull any action items, the responsible party and date onto another tab so that we can see all action items in one place for all the sites vs. going tab by tab to review.
I thought some sort of IF or VLOOKUP function might work, or some sort of pivot table but because it is text and not numbers I am having a hard time crafting the appropriate formula. I was also told I could do some sort of reference look up (like putting a word like ACTION at the start of any text I want to find later) but this seems more complicated than it needs to be.
Any help would be deeply appreciated.
I don't think VLOOKUP can solve your problem. You definitely need VBA so something like this will get you going. Make a new sheet called as Summary and put this code in the sheet:
Sub SummarizeSheets()
Dim ws As Worksheet
Application.ScreenUpdating = False
Sheets("Summary").Activate
Range("A2").Select
For Each ws In Worksheets
If ws.Name <> "Summary" Then
If ws.Range("A2") <> "" Then 'A2 is blank means no action items found so go to next worksheet
ws.Range("A2:C100000").Copy 'Adjust your range here
ActiveSheet.Paste Range("A65536").End(xlUp).Offset(2, 0)' Paste the copied range
End If
End If
Next ws
Application.CutCopyMode = False
End Sub
You have to adjust the code to suit your needs. Assumption here is your action items starts with cell A2 and responsible person and due date are in cell B2 and C2 respectively.

Excel PivotTable Conditional Formatting

I have a pivot table in an Excel worksheet that contains the result of a query made to my database. I would like to format the information automatically based on every other data set.
The information contains 4 weeks' (1 month) worth of records for each employee sorted by an employee ID number. I would like to write a module so that it will highlight every other record (employee data set) with a different color. Is this even possible to do? Thanks for the help!
If you insist with solving your problem utilizing VBA here is an example. You'll need to specify start ranges. Please not that marking whole row will use more memory (increasing file size) so I would rather use example: range("A2:E2).select ....
Sub FormatEverySecondRow()
range("A2").EntireRow.Select
Do While ActiveCell.value <> ""
Selection.Interior.ColorIndex = 15
ActiveCell.offset(2, 0).EntireRow.Select
Loop
End Sub
use a helper column (K if I count the columns in your example)
insert into K2:
=IF(ISBlank(C2),K1,MOD(K1+1,2))
then use conditional formatting to highlight the row:
Note the formula does not have a $ sign before the 2 (i.e. $K2, not $K$2)
This might be useful to you:
Sub HighlightDifferentRows()
Dim wksht As Worksheet
Dim wkb As Workbook
Dim row As Range
Dim FloatColor As Long
FloatColor = RGB(100, 100, 100)
Set wbk = ThisWorkbook
Application.ScreenUpdating = False
For Each row In Sheets(1).UsedRange.Rows
row.Interior.Color = FloatColor
If row.Cells(1, 4).Value <> row.Cells(2, 4).Value Then
FloatColor = -FloatColor
End If
Next row
Application.ScreenUpdating = True
End Sub
It alternates row colors whenever a cell value is not the same as the one below it. Right now it is set to grayish colors but you could change it to something brighter if you wanted. You could put in your own logic to get whatever colors you wanted. Good Luck.

Resources