Inserting new rows base on cell value, copying cell value on the first 3 column and transpose data for the rest

llbac

New Member
Joined
Jul 20, 2023
Messages
30
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,
I have a similar question with other posts but different a little bit.
I have no experience in VBA and English is not my native language, so please accept my apologize and tell me if my question is unclear.
I would like to write the VBA code that can convert Table 1 to Table 2, which are:
1/ Table 1 locate on the Sheet1, start from A1 cell.
The first rows is column's titles
There are at least 4 columns. The first 3 columns are always text. From the 4th column onwards are always numeric (integer) value.
Data on the first 4 columns are always filled. On the next column onwards, maybe have one or more numeric values.
Table1.png

Fomular-test.xlsx
ABCDEFGHI
1Title ATitle BTitle CTitle DTitle ETitle FTitle GTitle HTitle I
2Context A1Context B1Context C1Number D1Number E1Number F1
3Context A2Context B2Context C2Number D2Number E2Number F2Number G2Number H2Number I2
4Context A3Context B3Context C3Number D3
5Context A4Context B4Context C4Number D4Number E4Number F4
6Context A5Context B5Context C5Number D5Number E5
Sheet1


2/ Table 2 locate on the new sheet (for example, named Sum).
In short, table 2 will transpose data on the 5th column onwards (if available) to the 4th column, while the first 3 columns' cell values will be copied from their corresponding cells above.
Table2.png

In my real data, value of column E (the 5th) will appear randomly, however, always in order (i.e: D-E, D-E-F, or D-E-F-G-H-H-J etc.).

Fomular-test.xlsx
ABCDEFGHI
1Title ATitle BTitle CTitle DTitle ETitle FTitle GTitle HTitle I
2Context A1Context B1Context C1Number D1
3Context A1Context B1Context C1Number E1
4Context A1Context B1Context C1Number F1
5Context A2Context B2Context C2Number D2
6Context A2Context B2Context C2Number E2
7Context A2Context B2Context C2Number F2
8Context A2Context B2Context C2Number G2
9Context A2Context B2Context C2Number H2
10Context A2Context B2Context C2Number I2
11Context A3Context B3Context C3Number D3
12Context A4Context B4Context C4Number D4
13Context A4Context B4Context C4Number E4
14Context A4Context B4Context C4Number F4
15Context A5Context B5Context C5Number D5
16Context A5Context B5Context C5Number E5
Sum2


Thank you very much!
 
This could work:

VBA Code:
If Not IsEmpty(myRange.Cells(r, c)) Then
or
VBA Code:
If Not IsEmpty(myRange.Cells(r, c).Value2) Then
 
Upvote 0

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
You must also say:
Set myRange = ActiveSheet.UsedRange
I tried both lines:

VBA Code:
If Not IsEmpty(myRange.Cells(r, c)) Then
and
VBA Code:
If Not IsEmpty(myRange.Cells(r, c).Value2) Then
But it always shows error '13': Type mismatch at this line:
VBA Code:
Set myRange = ActiveSheet.UsedRange

VBA Code:
Sub Test()
  Dim myRange As Worksheet, i As Long
  Set myRange = ActiveSheet.UsedRange
  i = 2
  With myRange
    Worksheets("Sum").Cells(1, 1).Resize(, .Columns.Count).Value2 = .Cells(1, 1).Resize(, .Columns.Count).Value2
    For r = 2 To .Rows.Count
      For c = 4 To .Columns.Count
        If Not IsEmpty(myRange.Cells(r, c).Value2) Then
          Worksheets("Sum").Cells(i, 1).Resize(, 3).Value2 = .Cells(r, 1).Resize(, 3).Value2
          Worksheets("Sum").Cells(i, 4).Value2 = .Cells(r, c).Value2
          i = i + 1
        End If
      Next
    Next
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,193
Members
452,616
Latest member
intern444

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