nested loops

TheSardOz

New Member
Joined
Nov 30, 2020
Messages
26
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I am trying to create a nested loop to sort out a price list.
I want to add the finish type abbreviation to the code number.
The issue that i am facing is that for each cell in the code column I need to add AB or BC based on the Adjicent cell value (the price).
If the column B-C-D-E-F has a value in I need to add a value to the code.
the logic is:
cells A2 has the value of 396, cell B2 has a value in so in a new sheet in cell A2 I need to paste in 396-AB and the rest of the values in the same row
then repeat this process for column C3 ( if no value skip) and check the next cell, D3 has a value in then in the new sheet in A3 i will have 396-NP ect.
I am not sure how to tackle this one i have try few nested loops but I keep on failing.

Thank you in advance for your help
mrExcel.jpg
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Perhaps a formula like below:
Book1
ABCDEFGHIJK
1CodeFinish ABFinish BCFinish NPFinish PBFinish SC
2396137.9137.9137.9396-AB137.9
3410258.9100.7258.9258.9396-NP137.9
4411343.2343.2343.2396-PB137.9
5412443443443300410-AB258.9
6410-BC100.7
7410-NP258.9
8410-PB258.9
9411-AB343.2
10411-NP343.2
11411-PB343.2
12412-AB443
13412-NP443
14412-PB443
15412-SC300
16
17
Sheet3
Cell Formulas
RangeFormula
I2:J15I2=LET(rng,Sheet3!B2:F5,codes,TOCOL(IF(SEQUENCE(,COUNTA(rng))<=rng,Sheet3!A2:A5,#N/A),3), vals,TOCOL(IF(SEQUENCE(,COUNTA(rng))<=rng,rng,#N/A),3), heads,RIGHT(TOCOL(IF(SEQUENCE(,COUNTA(rng))<=rng,Sheet3!B1:F1,#N/A),3),2), HSTACK(codes & "-" & heads,vals))
Dynamic array formulas.
 
Upvote 0
Another option
Fluff.xlsm
ABCDEFGHIJ
1CodeFinish ABFinish BCFinish NPFinish PBFinish SC
2396137.9137.9137.9396-AB137.9
3410258.9100.7258.9258.9396-NP137.9
4411343.2343.2343.2396-PB137.9
5412443443443300410-AB258.9
6410-BC100.7
7410-NP258.9
8410-PB258.9
9411-AB343.2
10411-NP343.2
11411-PB343.2
12412-AB443
13412-NP443
14412-PB443
15412-SC300
Lists
Cell Formulas
RangeFormula
I2:J15I2=HSTACK(TOCOL(IF(B2:F5="",x,A2:A5),2)&"-"&TOCOL(IF(B2:F5="",x,RIGHT(B1:F1,2)),2),TOCOL(B2:F5,1))
Dynamic array formulas.
 
Upvote 1
Perhaps a formula like below:
Book1
ABCDEFGHIJK
1CodeFinish ABFinish BCFinish NPFinish PBFinish SC
2396137.9137.9137.9396-AB137.9
3410258.9100.7258.9258.9396-NP137.9
4411343.2343.2343.2396-PB137.9
5412443443443300410-AB258.9
6410-BC100.7
7410-NP258.9
8410-PB258.9
9411-AB343.2
10411-NP343.2
11411-PB343.2
12412-AB443
13412-NP443
14412-PB443
15412-SC300
16
17
Sheet3
Cell Formulas
RangeFormula
I2:J15I2=LET(rng,Sheet3!B2:F5,codes,TOCOL(IF(SEQUENCE(,COUNTA(rng))<=rng,Sheet3!A2:A5,#N/A),3), vals,TOCOL(IF(SEQUENCE(,COUNTA(rng))<=rng,rng,#N/A),3), heads,RIGHT(TOCOL(IF(SEQUENCE(,COUNTA(rng))<=rng,Sheet3!B1:F1,#N/A),3),2), HSTACK(codes & "-" & heads,vals))
Dynamic array formulas.
Thank you
 
Upvote 0
Another option
Fluff.xlsm
ABCDEFGHIJ
1CodeFinish ABFinish BCFinish NPFinish PBFinish SC
2396137.9137.9137.9396-AB137.9
3410258.9100.7258.9258.9396-NP137.9
4411343.2343.2343.2396-PB137.9
5412443443443300410-AB258.9
6410-BC100.7
7410-NP258.9
8410-PB258.9
9411-AB343.2
10411-NP343.2
11411-PB343.2
12412-AB443
13412-NP443
14412-PB443
15412-SC300
Lists
Cell Formulas
RangeFormula
I2:J15I2=HSTACK(TOCOL(IF(B2:F5="",x,A2:A5),2)&"-"&TOCOL(IF(B2:F5="",x,RIGHT(B1:F1,2)),2),TOCOL(B2:F5,1))
Dynamic array formulas.
Thank you!
 
Upvote 0
Thank you for your solutions @Fluff and @Georgiboy. I was wondering if you might have an equivalent solution in VBA too.
Maybe something like:
VBA Code:
Sub test()
    Dim oRng As Range, iRng As Range
    Dim var As Variant, eRow As Long
    Dim x As Long, y As Long, z As Long
    
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    Set oRng = Range("A1:F" & eRow)
    Set iRng = Range("B2:F" & eRow)
    var = oRng.Value
    
    ReDim oVar(Application.CountA(iRng) - 1, 1)
    
    For x = 2 To UBound(var)
        For y = 2 To UBound(var, 2)
            If var(x, y) <> "" Then
                oVar(z, 0) = var(x, 1) & "-" & Right(var(1, y), 2)
                oVar(z, 1) = var(x, y)
                z = z + 1
            End If
        Next y
    Next x
    
    Range("I2").Resize(UBound(oVar) + 1, UBound(oVar, 2) + 1) = oVar
End Sub
 
Last edited:
Upvote 1
Solution
Maybe something like:
VBA Code:
Sub test()
    Dim oRng As Range, iRng As Range
    Dim var As Variant, eRow As Long
    Dim x As Long, y As Long, z As Long
   
    eRow = Range("A" & Rows.Count).End(xlUp).Row
    Set oRng = Range("A1:F" & eRow)
    Set iRng = Range("B2:F" & eRow)
    var = oRng.Value
   
    ReDim oVar(Application.CountA(iRng) - 1, 1)
   
    For x = 2 To UBound(var)
        For y = 2 To UBound(var, 2)
            If var(x, y) <> "" Then
                oVar(z, 0) = var(x, 1) & "-" & Right(var(1, y), 2)
                oVar(z, 1) = var(x, y)
                z = z + 1
            End If
        Next y
    Next x
   
    Range("I2").Resize(UBound(oVar) + 1, UBound(oVar, 2) + 1) = oVar
End Sub
Thank you kindly!
 
Upvote 0
May even get away with shortening the last line to:

VBA Code:
Range("I2").Resize(z, 2) = oVar
 
Upvote 1
maybe
VBA Code:
Sub AddFinishToCode()

    Dim dataSheet As Worksheet
    Set dataSheet = ThisWorkbook.Sheets("Data") ' Change "Data" to the name of your sheet
    
    Dim outputSheet As Worksheet
    Set outputSheet = ThisWorkbook.Sheets.Add(After:=dataSheet) ' Creates a new sheet for the output
    outputSheet.Name = "Output" ' Change "Output" to the name you want for the output sheet
    
    Dim lastRow As Long
    lastRow = dataSheet.Cells(Rows.Count, 1).End(xlUp).Row
    
    Dim i As Long
    Dim code As String
    Dim finish As String
    
    For i = 2 To lastRow ' Assumes data starts on row 2, change as needed
        code = dataSheet.Cells(i, 1).Value ' Get the code from column A
        If Not IsEmpty(dataSheet.Cells(i, 2)) Then ' Check if there's a price in column B
            finish = "-AB" ' Set the finish based on the adjacent price
        ElseIf Not IsEmpty(dataSheet.Cells(i, 3)) Then
            finish = "-BC"
        Else
            finish = "" ' If no price in adjacent columns, don't add a finish
        End If
        If finish <> "" Then ' Only write to output if there's a finish to add
            outputSheet.Cells(i, 1).Value = code & finish ' Write the new code to the output sheet
            outputSheet.Cells(i, 2).Resize(1, 4).Value = dataSheet.Cells(i, 2).Resize(1, 4).Value ' Copy the rest of the data to the output sheet
        End If
    Next i
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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