Solved Excel 2021: Extracting the last `n` row values from a non-rectangular/irregular shaped table


giftwrapped

Active member
Member
Local time
7:49 PM
Posts
57
OS
Windows 11 Pro 25H2, OS build: 26200.8655
Can anyone help me to extract the last 3 values from each row (green background) so that result is the 3x3 table (orange background). ChatGPT could not help me -- maybe I did not ask the right way. This is the example table: Thanks!

excel-2021-extract-last-3-values-of-each-row.webp

(My original table is much bigger, so copy & paste is not a solution)
 

Attachments

My Computer

System One

  • OS
    Windows 11 Pro 25H2, OS build: 26200.8655
    Computer type
    Laptop
ChatGPT could not help me -- maybe I did not ask the right way


Hi.

I asked CoPilot exactly what you posted above and provided the image you attached.

Extracting the Last 3 Values from Each Row to Form a New Table

To recreate a 3x3 table using only the last three values from each row (highlighted in green), follow this logic:

  1. Identify all rows with a green background. Each row contains multiple values.
  2. From each green row, extract the last 3 values.
  3. Create a new table with 3 rows and 3 columns. Each row corresponds to a green row, and each cell holds one of its final 3 values.

    Example Data Extracted:
    • Row 1: 10a, 11b, 12c
    • Row 2: 20a, 21b, 22c
    • Row 3: 30a, 31b, 32c

    Resulting 3x3 Table:

    Code:
    10a | 11b | 12c
    20a | 21b | 22c
    30a | 31b | 32c

    Tip: You can achieve this programmatically using Excel, Power Query, or even scripting tools depending on your source format. Let me know how you'd like to automate it!


CONTINUED… I’d suggest testing with a COPY of your excel sheet/workbook or ask CoPilot the way you did in this forum and keep the questions rolling with CoPilot​

Variate the questions to CoPilots replies, don’t be scared to call it names or just be outright mean “CoPilot, you’re so dumb” it will only apologise.


Extracting the Last 3 Values from Each Row Using PowerShell and Batch Scripts

If you have a space-delimited text file where each row contains multiple values, and you want to extract only the final 3 from each line, use the scripts below.

PowerShell Method:

Powershell:
# Assumes input.txt contains your original data
Get-Content "input.txt" | ForEach-Object {
    $fields = $_ -split '\s+'
    $lastThree = $fields[-3..-1] -join ' '
    $lastThree
} > "output.txt"

Batch Script Method:

Batch:
@echo off
setlocal enabledelayedexpansion

rem Assumes input.txt contains the data
for /f "tokens=* delims=" %%A in (input.txt) do (
    set "line=%%A"
    set "last="
    for %%B in (!line!) do (
        set /a count+=1
        set "token[!count!]=%%B"
    )
    set /a start=count - 2
    echo !token[%start%]! !token[%count%-1]! !token[%count%]!
    set count=0
) >> output.txt
endlocal

Notes:
  • These scripts output only the last 3 fields from each line.
  • PowerShell is more concise and handles edge cases better.
  • Batch is useful for environments with restricted scripting capabilities.

It says: Let me know if you'd like a version adapted for tab-delimited formats or Excel-style processing!
 

My Computers

System One System Two

  • OS
    Windows 11 Pro 25H2 Build 26200.8655
    Computer type
    PC/Desktop
    Manufacturer/Model
    Sin-built 2013
    CPU
    Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz
    Motherboard
    ASUS ROG Maximus VI Formula
    Memory
    32.0 GB of I forget and the box is in storage.
    Graphics Card(s)
    Gigabyte nVidia GeForce GTX 1660 Super OC 6GB
    Sound Card
    Onboard thingy
    Monitor(s) Displays
    5 x LG 25MS500-B - 1 x 24MK430H-B - 1 x Wacom Pro 22" Touch Screen Tablet
    Screen Resolution
    All over the place
    Hard Drives
    Too many to list.
    OS on Samsung 1TB 870 QVO SATA
    PSU
    Silverstone 1500
    Case
    NZXT Phantom 820 Full-Tower Case
    Cooling
    Noctua NH-D15 Elite Class Dual Tower CPU Cooler / 6 x EziDIY 120mm / 2 x Corsair 140mm somethings / 1 x 140mm Thermaltake something / 2 x 200mm Corsair.
    Keyboard
    Corsair K95 / Logitech diNovo Edge Wireless
    Mouse
    Logitech: G402 / G502 / Mx Masters / Mx Air Cordless
    Internet Speed
    2000/500Mbps
    Browser
    All sorts
    Antivirus
    Kaspersky Premium
    Other Info
    ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
    TP-Link BE9300 WiFi 7 Bluetooth 5.4 (Archer TBE550E)
    TP-Link TX201 V1 2.5GB Lan

    Grandstream HT812 - VoIP
    ASUS DSL-AX82U - Mesh
    ASUS RT-AC68U - Mesh
    ASUS RT-BE88U Router

    Brother MFC-L2880DW Printer

    I’m on a horse.
  • Operating System
    Windows 11 Pro 25H2 Build 26200.8655
    Computer type
    Laptop
    Manufacturer/Model
    LENOVO Yoga 7 14IRL8 - 7i EVO OLED 14" Touchscreen i5 12 Core 16GB/512GB
    CPU
    Intel Core 12th Gen i5-1240P Processor (1.7 - 4.4GHz)
    Memory
    16GB LPDDR5 RAM
    Graphics card(s)
    Intel Iris Xe Graphics Processor
    Sound Card
    Optimized with Dolby Atmos®
    Screen Resolution
    QHD 2880 x 1800 OLED
    Hard Drives
    M.2 512GB
    Antivirus
    Defender / Malwarebytes
    Other Info
    …still on a horse.
Hi.

I asked CoPilot exactly what you posted above and provided the image you attached.

Extracting the Last 3 Values from Each Row to Form a New Table

To recreate a 3x3 table using only the last three values from each row (highlighted in green), follow this logic:

  1. Identify all rows with a green background. Each row contains multiple values.
  2. From each green row, extract the last 3 values.
  3. Create a new table with 3 rows and 3 columns. Each row corresponds to a green row, and each cell holds one of its final 3 values.

    Example Data Extracted:
    • Row 1: 10a, 11b, 12c
    • Row 2: 20a, 21b, 22c
    • Row 3: 30a, 31b, 32c

    Resulting 3x3 Table:

    Code:
    10a | 11b | 12c
    20a | 21b | 22c
    30a | 31b | 32c

    Tip: You can achieve this programmatically using Excel, Power Query, or even scripting tools depending on your source format. Let me know how you'd like to automate it!


CONTINUED… I’d suggest testing with a COPY of your excel sheet/workbook or ask CoPilot the way you did in this forum and keep the questions rolling with CoPilot​

Variate the questions to CoPilots replies, don’t be scared to call it names or just be outright mean “CoPilot, you’re so dumb” it will only apologise.


Extracting the Last 3 Values from Each Row Using PowerShell and Batch Scripts

If you have a space-delimited text file where each row contains multiple values, and you want to extract only the final 3 from each line, use the scripts below.

PowerShell Method:

Powershell:
# Assumes input.txt contains your original data
Get-Content "input.txt" | ForEach-Object {
    $fields = $_ -split '\s+'
    $lastThree = $fields[-3..-1] -join ' '
    $lastThree
} > "output.txt"

Batch Script Method:

Batch:
@echo off
setlocal enabledelayedexpansion

rem Assumes input.txt contains the data
for /f "tokens=* delims=" %%A in (input.txt) do (
    set "line=%%A"
    set "last="
    for %%B in (!line!) do (
        set /a count+=1
        set "token[!count!]=%%B"
    )
    set /a start=count - 2
    echo !token[%start%]! !token[%count%-1]! !token[%count%]!
    set count=0
) >> output.txt
endlocal

Notes:
  • These scripts output only the last 3 fields from each line.
  • PowerShell is more concise and handles edge cases better.
  • Batch is useful for environments with restricted scripting capabilities.

It says: Let me know if you'd like a version adapted for tab-delimited formats or Excel-style processing!
copilot is the way to go for this stuff -- @antspants got the same procedure from copilot too !!!

BTW for those using EXCEL 2021 who want to use things like getting data from say MySql / Ms query etc sources - you need to go into OPTIONS - and then select on data - legacy -- and select the legacy data sources you want.

Note though if you use the "classical Ms query" to get data from a mariaDB / AB MySQL database -- you need to ensure that the relevant ODBC drivers are installed -- for MariaDB / AB MySql get the driver from here (version is 9.3.0) -- ensure also you've install the latest runtime visual c++ redistributable runtime modules - free BTW.




cheers
jimbo
 

My Computer

System One

  • OS
    Windows XP,11 Linux Fedora Rawhide pre-release 45
    Computer type
    PC/Desktop
    CPU
    2 X Intel i7
    Screen Resolution
    4KUHD X 2
Thanks for your help :D I went with the PowerShell method and it worked fine, as long as I removed all trailing spaces or commas from the input text file. I will check out CoPilot in the future.

Powershell:
# Input: Space-delimited text file
Get-Content "input-space-delimited.txt" | ForEach-Object {
    $fields = $_ -split '\s+'
    $lastThree = $fields[-3..-1] -join ' '
    $lastThree
} > "output-space-delimited.txt"

Result: OK

10a 11b 12c
20a 21b 22c
30a 31b 32c

Powershell:
# Input: Comma-delimited text file
Get-Content "input-comma-delimited.txt" | ForEach-Object {
    $fields = $_ -split ','
    $lastThree = $fields[-3..-1] -join ','
    $lastThree
} > "output-comma-delimited.txt"

Result: OK

10a,11b,12c
20a,21b,22c
30a,31b,32c
 
Last edited:

My Computer

System One

  • OS
    Windows 11 Pro 25H2, OS build: 26200.8655
    Computer type
    Laptop
I will check out CoPilot in the future.

I usually ask CoPilot Microsoft related questions. Info is important though. You eventually get better at prompting. And just ask like you would a teacher
 

My Computers

System One System Two

  • OS
    Windows 11 Pro 25H2 Build 26200.8655
    Computer type
    PC/Desktop
    Manufacturer/Model
    Sin-built 2013
    CPU
    Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz
    Motherboard
    ASUS ROG Maximus VI Formula
    Memory
    32.0 GB of I forget and the box is in storage.
    Graphics Card(s)
    Gigabyte nVidia GeForce GTX 1660 Super OC 6GB
    Sound Card
    Onboard thingy
    Monitor(s) Displays
    5 x LG 25MS500-B - 1 x 24MK430H-B - 1 x Wacom Pro 22" Touch Screen Tablet
    Screen Resolution
    All over the place
    Hard Drives
    Too many to list.
    OS on Samsung 1TB 870 QVO SATA
    PSU
    Silverstone 1500
    Case
    NZXT Phantom 820 Full-Tower Case
    Cooling
    Noctua NH-D15 Elite Class Dual Tower CPU Cooler / 6 x EziDIY 120mm / 2 x Corsair 140mm somethings / 1 x 140mm Thermaltake something / 2 x 200mm Corsair.
    Keyboard
    Corsair K95 / Logitech diNovo Edge Wireless
    Mouse
    Logitech: G402 / G502 / Mx Masters / Mx Air Cordless
    Internet Speed
    2000/500Mbps
    Browser
    All sorts
    Antivirus
    Kaspersky Premium
    Other Info
    ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
    TP-Link BE9300 WiFi 7 Bluetooth 5.4 (Archer TBE550E)
    TP-Link TX201 V1 2.5GB Lan

    Grandstream HT812 - VoIP
    ASUS DSL-AX82U - Mesh
    ASUS RT-AC68U - Mesh
    ASUS RT-BE88U Router

    Brother MFC-L2880DW Printer

    I’m on a horse.
  • Operating System
    Windows 11 Pro 25H2 Build 26200.8655
    Computer type
    Laptop
    Manufacturer/Model
    LENOVO Yoga 7 14IRL8 - 7i EVO OLED 14" Touchscreen i5 12 Core 16GB/512GB
    CPU
    Intel Core 12th Gen i5-1240P Processor (1.7 - 4.4GHz)
    Memory
    16GB LPDDR5 RAM
    Graphics card(s)
    Intel Iris Xe Graphics Processor
    Sound Card
    Optimized with Dolby Atmos®
    Screen Resolution
    QHD 2880 x 1800 OLED
    Hard Drives
    M.2 512GB
    Antivirus
    Defender / Malwarebytes
    Other Info
    …still on a horse.
using excel macro, .End(xlToLeft), then takes the last 3 values.

This should do.
 

My Computer

System One

  • OS
    Windows 7/11
    Computer type
    PC/Desktop
    Manufacturer/Model
    HP/Lenovo/Asus
    CPU
    Intel i7-11800H
    Motherboard
    Lenovo Legion 5i Pro Gen 6
    Memory
    32GB DDR4 3200MHz
    Graphics Card(s)
    NVIDIA GeForce RTX 3070
    Monitor(s) Displays
    LG 35WN65C-B
    Screen Resolution
    3440 x 1440
    Hard Drives
    1TB PCIe SSD
    Other Info
    Edition Windows 11 Pro
    Version 25H2
    Installed on ‎12/‎12/‎2025
    OS build 26200.7462
@siliconbeaver, thanks for the tip. I asked CoPilot how I could use the function `.End(xlToLeft)` to solve my problem, and CoPilot gave me this macro:

Code:
Sub ExtractLastThreeValues_VariedColumns()
    Dim srcSheet As Worksheet
    Dim destSheet As Worksheet
    Dim lastCol As Long
    Dim r As Long
    Dim destRow As Long

    Set srcSheet = ThisWorkbook.Sheets("Sheet1") ' Your source sheet
    Set destSheet = ThisWorkbook.Sheets("Sheet2") ' Destination sheet, or create a new one
    destRow = 1

    For r = 1 To srcSheet.Cells(srcSheet.Rows.Count, "A").End(xlUp).Row
        ' Find last column with data in the current row
        lastCol = srcSheet.Cells(r, srcSheet.Columns.Count).End(xlToLeft).Column

        ' Copy last 3 values from that row
        destSheet.Cells(destRow, 1).Value = srcSheet.Cells(r, lastCol - 2).Value
        destSheet.Cells(destRow, 2).Value = srcSheet.Cells(r, lastCol - 1).Value
        destSheet.Cells(destRow, 3).Value = srcSheet.Cells(r, lastCol).Value

        destRow = destRow + 1
    Next r
End Sub

I ran the macro, and it worked fine, here is the input table (Sheet1) and the output table (Sheet2):

elevenforum-3.webp
 

My Computer

System One

  • OS
    Windows 11 Pro 25H2, OS build: 26200.8655
    Computer type
    Laptop
Back
Top Bottom