Preciso escrever um script VBA usando um loop ou alguns loops, que no final imprimirão os dados, por debug.print, parecendo algo assim:
lata, kg, verde, ervilha, 24,79
Aqui está a imagem de como ficou minha planilha
Sub ExtractTableData()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim row As Integer, col As Integer
Dim lastRow As Integer, lastCol As Integer
Dim category As String, unit As String
Dim color As String, item As String, value As Variant
lastRow = ws.Cells(Rows.Count, 1).End(xlUp).Row
lastCol = ws.Cells(2, Columns.Count).End(xlToLeft).Column
For row = 3 To lastRow
category = ws.Cells(row, 1).Value ' Category (can/bag/jar)
unit = ws.Cells(row, 2).Value ' Unit (kg/piece)
For col = 3 To lastCol
If ws.Cells(1, col).MergeCells Then
color = ws.Cells(1, col).MergeArea.Cells(1, 1).Value
Else
color = ws.Cells(1, col).Value
End If
item = ws.Cells(2, col).Value
value = ws.Cells(row, col).Value
If Not IsEmpty(value) Then
Debug.Print category & ", " & unit & ", " & color & ", " & item & ", " & value
End If
Next col
Next row
End Sub
Posso fazer versões mais simples deste exercício definindo um intervalo e fazendo um loop por ele, mas quando se trata de células mescladas, na verdade não tenho esperança. Por favor, não vote negativamente na minha pergunta novamente, estou realmente procurando ajuda :((