Simple nesting issue

SimonHow

New Member
Joined
Aug 31, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
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

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
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hi & welcome to MrExcel.
You are missing an End Selectline in this part of the code
VBA Code:
        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

Also those last 2 Cases are redundant as both are covered on the 2nd Case
 
Upvote 0
Solution
That sorted it, I must have deleted that "end" during one of the many iterations.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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