I have the following code. What is happening is the code collects data from an OLE generated report in which the formatting is unhelpful, and reconfigures as a clean table with some data correction / manipulation along the way. For ease when modifying what happens, I essential populate an array with the required data, and then pass that data to the new worksheet as required. It may not be that elegant, but it does allow me to modify easily. However:
For some reason there is a nesting problem, the compiler throws out the last "NEXT I" line saying there is no "FOR"
I know its simple but I am missing something, can anyone help and spot the problem ?
Many thanks and much appreciated
For some reason there is a nesting problem, the compiler throws out the last "NEXT I" line saying there is no "FOR"
I know its simple but I am missing something, can anyone help and spot the problem ?
Many thanks and much appreciated
Code:
Dim I As Variant
Dim O As Variant 'Output line number
Dim IR As range ' input range
Dim LR As Variant 'Last row
Dim PC As String ' Product Code
Dim PD As String ' Product Description
Dim OA(0, 10)
'0 Product Code PC
'1 Product Description PD
'2 Period in FY2023/24
'3 Transaction Date
'4 Customer
'5 Line Quantity
'6 Line Sales Value
'7 Line Cost Value
'8 size
'9 Stock Movement Description
'10 Transaction Reference
Sub FullSort() 'Extract stock movement data and assemble
With Worksheets("Exchequer Report")
LR = .range("A65536").End(xlUp).Row
O = 2
For I = 6 To LR
Set IR = .range("A" & I)
' Populate product code and description for each line
Select Case Left(IR, 1)
Case "N", "Y" ' Lines that are product codes from the report
C = InStr(1, IR, ",")
PC = Left(IR, C - 1)
PD = Right(IR, (Len(IR) - (C + 1)))
OA(0, 0) = PC
OA(0, 1) = PD
' Extract period
Case "S", "A" ' Lines that are transactions from the report
Select Case .range("C" & I).Value
Case "01/2023"
OA(0, 2) = "May"
Case "02/2023"
OA(0, 2) = "June"
Case "03/2023"
OA(0, 2) = "July"
Case "S" ' Lines that are Sales Invoices from the report
OA(0, 9) = "Sales Invoice" ' Stock Transaction description
Case "A" ' Lines that are stock movements from the report
OA(0, 9) = .range("B" & I).Value ' Stock transaction description
OA(0, 10) = .range("A" & I).Value ' Transaction Reference
End Select
'Populate the key data from the transaction lines
OA(0, 3) = .range("D" & I).Value ' Transaction Date
OA(0, 4) = .range("F" & I).Value ' Customer if required
OA(0, 5) = .range("G" & I).Value ' Line Quantity
OA(0, 6) = .range("H" & I).Value ' Line Sales Value if required
'Line cost correction to positive
Select Case .range("I" & I).Value
Case Is >= 0
OA(0, 7) = .range("I" & I).Value ' Line Cost Value
Case Is < 0
OA(0, 7) = -.range("I" & I).Value ' Line Cost Value negative to positive
Case Else
End Select
'Populate the worksheet
With Worksheets("All Transactions")
.range("A" & O).Value = OA(0, 0) 'Product Code
.range("B" & O).Value = OA(0, 1) ' Product Description
.range("C" & O).Value = OA(0, 2) ' Period
.range("D", 0).Value = OA(0, 3) ' Transaction Date
.range("E" & O).Value = OA(0, 5) ' Line Quantity
.range("F" & O).Value = OA(0, 7) ' Line Cost Value
.range("G" & O).Value = -(OA(0, 7) / OA(0, 5)) ' ACP
.range("H" & O).Value = OA(0, 9) ' Transaction Description
.range("I" & O).Value = OA(0, 10) ' Transaction Reference
O = O + 1
End With
Next I
End With
End Sub