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!
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Not the most efficient bu does the job:
VBA Code:
Sub test()
  Dim myRange As Range, i As Long
  Set myRange = UsedRange
  For r = myRange.Rows.Count To 2 Step -1
  i = 0
    For c = myRange.Columns.Count To 5 Step -1
      If myRange.Cells(r, c).Value2 <> "" Then
        myRange.Rows(r + 1).EntireRow.Insert
        myRange.Cells(r + 1, 4) = myRange.Cells(r, c)
        i = i + 1
      End If
    Next
    If i > 0 Then
      myRange.Cells(r, 5).Resize(1, myRange.Columns.Count - 4) = ""
      myRange.Cells(r + 1, 1).Resize(i, 3).Value2 = myRange.Cells(r, 1).Resize(1, 3).Value2
    End If
  Next
End Sub
 
Upvote 0
Thanks for your prompt response.
Unfortunately, I got Error 424:
Error.png

Error-debug.png


What should I do?
 
Upvote 0
Could you please try to specify the sheet name?
Set myRange = Worksheets("Sum2").UsedRange
 
Upvote 0
Could you please try to specify the sheet name?
Set myRange = Worksheets("Sum2").UsedRange

I have just changed the code to this:

VBA Code:
Sub Convert_table()
  Dim myRange As Range, i As Long
  Set myRange = Worksheets("Sum").UsedRange
  For r = myRange.Rows.Count To 2 Step -1
  i = 0
    For c = myRange.Columns.Count To 5 Step -1
      If myRange.Cells(r, c).Value2 <> "" Then
        myRange.Rows(r + 1).EntireRow.Insert
        myRange.Cells(r + 1, 4) = myRange.Cells(r, c)
        i = i + 1
      End If
    Next
    If i > 0 Then
      myRange.Cells(r, 5).Resize(1, myRange.Columns.Count - 4) = ""
      myRange.Cells(r + 1, 1).Resize(i, 3).Value2 = myRange.Cells(r, 1).Resize(1, 3).Value2
    End If
  Next
End Sub

I have already create a sheet named 'Sum'. However, there is nothing happened. No table is created on sheet Sum.
 
Upvote 0
Could you please try to specify the sheet name?
Set myRange = Worksheets("Sum2").UsedRange
Also, I am currently working with Sheet1 which the original table is on. The expected result should be on sheet "Sum". Should the code indicates something about that?
Again, I have no experience in VBA coding so please don't be so surprised about my silly questions ^^!
 
Upvote 0
Oh, I figure out that it I change from "Sum" to "Sheet1", then it works!
The problem is that it changes directly data on Sheet1 which I would like to retain.
I expect to create a new sheet, named "Sum", to paste the result onto it. However, I guess the way to work around is to duplicate the sheet and then run the marco.

Another question is that, could you please change the code so that it will work on the current sheet? In this way, it is not necessary to change the code every time I run because the sheet name will vary (a lot, about more than 30 sheets), which will help me to reduce the workload drastically.
 
Upvote 0
I am so confused :) Please specify which is the source sheet and which will be the destination sheet?
 
Upvote 0
Ok this should work:
VBA Code:
Sub Test()
  Dim myRange As Range, i As Long
  Set myRange = Worksheets("Sheet1").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(r, c)) 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
Solution
Ok this should work:
VBA Code:
Sub Test()
  Dim myRange As Range, i As Long
  Set myRange = Worksheets("Sheet1").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(r, c)) 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

This code worked flawlessly! Thank you very much!
By the way, could you please help me to change to code a little bit more?
I would like to convert the table at the current worksheet without needing to know the current sheet's name (in this case, Sheet1).
For example, if I am working on the sheet XYZ, and this VBA should still work, then I will move to another sheet to repeat this code.

I tried:
VBA Code:
Sub Test()
  Dim myRange As Worksheet, i As Long
  Set myRange = ActiveSheet
  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(r, c)) 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

But the Error '438' pop out: Object doesn't support this property or method at line:
VBA Code:
 If Not IsEmpty(myRange(r, c)) Then
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,195
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