Sub getCk13N()
'this sub needs to be changed to export tab spaced like COR3
'this will solve problem of having heaps of excel windows open up
Dim materialNumber As String
Dim numberOfMaterials As Integer
Dim materialSheet As Worksheet
Dim currentmaterialTextFile As Workbook
Dim BOMsWbk As Workbook
Set BOMsWbk = ActiveWorkbook
Set session = connectSAP
'add summary sheet
'Sheets.Add
'ActiveSheet.Name = "Summary"
Worksheets("MaterialsList").Activate
numberOfMaterials = Cells(Rows.Count, 1).End(xlUp).Row - 1
For x = 1 To numberOfMaterials
materialNumber = Worksheets("MaterialsList").Cells(x + 1, 1).Value
Application.ScreenUpdating = False
'start Ck13n
session.StartTransaction "CK13N"
'Enter Material Number, STD1, date etc.
session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/subKOPF:SAPLCKDI:4620/ctxtCKI64A-MATNR").Text = materialNumber
session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/subKOPF:SAPLCKDI:4620/ctxtCKI64A-WERKS").Text = Worksheets("MaterialsList").Range("E2")
session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/tabsREITER/tabpALLG/ssubALLGEMEIN:SAPLCKDI:4612/ctxtCKI64A-KLVAR").Text = "STD1"
session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/tabsREITER/tabpALLG/ssubALLGEMEIN:SAPLCKDI:4612/ctxtCKI64A-TVERS").Text = "1"
session.findById("wnd[0]/usr/subALL:SAPLCKDI:4611/tabsREITER/tabpALLG/ssubALLGEMEIN:SAPLCKDI:4612/ctxtCKI64A-AMDAT").Text = Worksheets("MaterialsList").Range("E3")
session.findById("wnd[0]/tbar[0]/btn[0]").press
'export file as tab delemited text
session.findById("wnd[0]/shellcont[1]/shell").pressToolbarContextButton "&MB_EXPORT"
session.findById("wnd[0]/shellcont[1]/shell").selectContextMenuItem "&PC"
session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select
session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").SetFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\TempVariancesFolder\"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Material#" & materialNumber & "-CK13N" & ".txt"
session.findById("wnd[1]/tbar[0]/btn[11]").press
Set currentmaterialTextFile = Workbooks.Open("C:\TempVariancesFolder\Material#" & materialNumber & "-CK13N" & ".txt")
'add a new sheet for that material
BOMsWbk.Activate
Sheets.Add
ActiveSheet.Name = materialNumber
Set materialSheet = ActiveSheet
'this was to get it all in one sheet
'Set materialSheet = BOMsWbk.Sheets("All CK13N")
Call CopyTXT(materialSheet, currentmaterialTextFile)
'store the cell addresses for the totals for that SKU
Dim materialName As String
Dim rawPackMaterialTotal As String
Dim conversionTotal As String
Dim productCostTotal As String
Dim packMaterialTotal As String
Dim packMaterialRange As Range
Dim packMaterialSumComponents As String
Dim finalCK13Nrow As Long
Dim headerRow As Integer
Dim materialSubtotalRow As Integer
Dim internalActivityRow As Integer
'which i values to cycle (need to automate this to count how many items there are)
For i = 3 To 100
'find material name
If Cells(i, 1).Value = "Material" Then
materialName = Cells(i, 6).Address
End If
'Find Totals locations
If Cells(i, 3).Value = "Material" Then
'replace materials total with a sum formula'(insert code to do that here)
rawPackMaterialTotal = Cells(i, 18).Address
materialSubtotalRow = i
End If
If Cells(i, 3).Value = "Internal Activity" Then
conversionTotal = Cells(i, 18).Address
internalActivityRow = i
End If
If Cells(i, 2).Value = "**" Then
productCostTotal = Cells(i, 18).Address
finalCK13Nrow = i
End If
'Find pack material totals cell addresses
If Cells(i, 8).Value = "Z004" Then
If Not packMaterialRange Is Nothing Then
Set packMaterialRange = Union(packMaterialRange, Cells(i, 18))
Else
Set packMaterialRange = Cells(i, 18)
End If
End If
'find header row (used later to replace the formulas for cost)
If InStr(Cells(i, 18).Value, "Total") > 0 Then
headerRow = i
End If
'Find kg, mhr and DVL
Dim kgBaseQty As String
Dim mHR As String
Dim dVL As String
'find machine hours
If Cells(i, 7).Value = "MHR1" Then
mHR = Cells(i, 14).Address
End If
'find kg
If Cells(i, 7).Value = "FIX1" Then
kgBaseQty = Cells(i, 14).Address(ReferenceStyle:=xlR1C1)
End If
'find DVL
If Cells(i, 7).Value = "DVL1" Then
dVL = Cells(i, 14).Address
End If
Next i
'get the cell addresses for pack material totals to be summed
If Not packMaterialRange Is Nothing Then
packMaterialSumComponents = packMaterialRange.Address
Else
packMaterialSumComponents = ""
End If
'make table which lists pack, raw, conversion and total
Dim tableStartRow As Integer
Dim tableStartColumn As Integer
tableStartRow = 16
tableStartColumn = 23
'record address of the new pack material total
packMaterialTotal = Cells(tableStartRow + 1, tableStartColumn + 1).Address
'add row titles
Cells(tableStartRow + 1, tableStartColumn - 1) = "$/Base Qty"
Cells(tableStartRow + 2, tableStartColumn - 1) = "$/kg"
'Raws = total materials - pack
Cells(tableStartRow, tableStartColumn) = "Raw (inc SFG)"
Cells(tableStartRow + 1, tableStartColumn).FormulaLocal = "=" & rawPackMaterialTotal & "-" & packMaterialTotal
'Pack (checking that there is pack materials and setting to 0 if not)
Cells(tableStartRow, tableStartColumn + 1) = "Pack"
If packMaterialSumComponents <> "" Then
Cells(tableStartRow + 1, tableStartColumn + 1).FormulaLocal = "=SUM(" & packMaterialSumComponents & ")"
Else
Cells(tableStartRow + 1, tableStartColumn + 1) = 0
End If
'Conversion
Cells(tableStartRow, tableStartColumn + 2) = "Conversion"
Cells(tableStartRow + 1, tableStartColumn + 2).FormulaLocal = "=" & conversionTotal
'Total
Cells(tableStartRow, tableStartColumn + 3) = "Total"
Cells(tableStartRow + 1, tableStartColumn + 3).FormulaLocal = "=" & productCostTotal
'make $/kg line
Cells(tableStartRow + 2, tableStartColumn).FormulaR1C1 = "=(R[-1]C[0])" & "/" & kgBaseQty
Range(Cells(tableStartRow + 2, tableStartColumn), Cells(tableStartRow + 2, tableStartColumn + 3)).FillRight
'format as $$$
Range(Cells(tableStartRow + 1, tableStartColumn), Cells(tableStartRow + 2, tableStartColumn + 3)).NumberFormat = "$#,##0.00"
'add materialName
Cells(tableStartRow, tableStartColumn - 1).FormulaLocal = "=" & materialName
'replace the hardcoded values with formulas
'replace the total price with formula then fill down for materials
Cells(headerRow + 2, 18).FormulaR1C1 = "=(R[0]C[-4]/R[0]C[-1])*R[0]C[-2]"
Range(Cells(headerRow + 2, 18), Cells(materialSubtotalRow - 2, 18)).FillDown
'for conversion
Cells(materialSubtotalRow + 2, 18).FormulaR1C1 = "=(R[0]C[-4]/R[0]C[-1])*R[0]C[-2]"
Range(Cells(materialSubtotalRow + 2, 18), Cells(finalCK13Nrow - 4, 18)).FillDown
'replace the subtotals'replace materials subtotal
Cells(materialSubtotalRow, 18).FormulaLocal = "=sum(" & Range(Cells(headerRow + 2, 18), Cells(materialSubtotalRow - 2, 18)).Address & ")"
'replace conversion subtotal
Cells(internalActivityRow, 18).FormulaLocal = "=sum(" & Range(Cells(materialSubtotalRow + 2, 18), Cells(internalActivityRow - 2, 18)).Address & ")"
'replace total
Cells(finalCK13Nrow, 18).FormulaLocal = "=" & Cells(materialSubtotalRow, 18).Address & "+" & Cells(internalActivityRow, 18).Address
'tidy up each material page and add layout.
Columns("B:E").AutoFit
Columns("G:U").AutoFit
Columns("W:Z").AutoFit
Range("A16:z16").Interior.ColorIndex = 48
'add links to summary page
Worksheets("Summary").Activate
Cells(x + 1, 1).FormulaLocal = "=" & materialNumber & "!" & Cells(tableStartRow, tableStartColumn - 1).Address
Cells(x + 1, 2).FormulaLocal = "=" & materialNumber & "!" & Cells(tableStartRow + 2, tableStartColumn).Address(True, False)
Range(Cells(x + 1, 2), Cells(x + 1, 5)).FillRight
Columns("A").AutoFit
'show progress of working through list as you go
Application.ScreenUpdating = True
Worksheets("MaterialsList").Activate
Worksheets("MaterialsList").Cells(x + 1, 2).Value = "done"
Worksheets("MaterialsList").Range("B" & x + 1).Select
Next x
End Sub