VBA to create a tabular table from a data table

George_Martin_3

New Member
Joined
Sep 12, 2015
Messages
20
Hi,
I already have a chunk of code that creates the tabular table, but what I need is to insert a new data point that is not in data set.
-
For the example data below, I need a column after STATE, named METHOD, that says GROUND for columns 5 and 6, and in that same column for column 7 to say AIR.
-
Thank you for any assist.

Code:
    Sub ExampleCode()
    Dim wsSource As Worksheet
    Dim wsDest As Worksheet
    Dim recRow As Long
    Dim lastRow As Long
    Dim i As Long
    Dim lngCol As Long


    'Where does the data come from?
    Set wsSource = Worksheets("Sheet1")


    Application.ScreenUpdating = False


    'Create new sheet
    Set wsDest = ThisWorkbook.Worksheets.Add


    'Setup headers
    wsSource.Range("A1:D1").Copy wsDest.Range("A1")
    wsDest.Range("E1").Value = "Pivot Field Dimension"
    wsDest.Range("F1").Value = "PF Value"
    recRow = 2


    With wsSource
        lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row


        'Loop over our data
        For i = 2 To lastRow
            'There are 3 items per main line. In columns 5-7
            For lngCol = 5 To 7
                'Grab the four columns every time
                wsSource.Cells(i, 1).Resize(1, 4).Copy wsDest.Cells(recRow, 1)
                wsDest.Cells(recRow, 5).Value = wsSource.Cells(1, lngCol).Value
                wsDest.Cells(recRow, 6).Value = wsSource.Cells(i, lngCol).Value


                'Incremenet counter for output
                recRow = recRow + 1
            Next lngCol
        Next i
    End With


    Application.ScreenUpdating = True
End Sub

START<code id="output" data-bind="text: output()" style="padding: 0px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: inherit; border-radius: 3px; background-color: transparent; border: 0px;">
</code><code id="output" data-bind="text: output()" style="padding: 0px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: inherit; border-radius: 3px; background-color: transparent; border: 0px;">




































TypeGroupPartStateTrainTruckAirplane
OfficeFurtnitureABC1TXXX
PatioUtilityDEF2FLXX
BedroomElectronicsGHI3COXX

<tbody>
</tbody>


FINISH
</code><code id="output" data-bind="text: output()" style="padding: 0px; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; color: inherit; border-radius: 3px; background-color: transparent; border: 0px;">


























































































TypeGroupPartStateMethodColumnValue
OfficeFurnitureABC1TXGROUNDTrainX
*******UtilityDEF2FLGROUNDTrain
BedroomElectronicsGHI3COGROUNDTrainX
OfficeFurnitureABC1TXGROUNDTruck
*******UtilityDEF2FLGROUNDTruckX
BedroomElectronicsGHI3COGROUNDTruckX
OfficeFurnitureABC1TXAIRAirplaneX
*******UtilityDEF2FLAIRAirplaneX
BedroomElectronicsGHI3COAIRAirplane

<tbody>
</tbody>
</code>
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,223,897
Messages
6,175,270
Members
452,628
Latest member
dd2

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