Transpose discontiguous ranges from one row into two columns

LuigiCortisone

New Member
Joined
Feb 12, 2009
Messages
30
Hi, I have a master worksheet with code that creates a worksheet from every row. This works well but I now want to grab two cells from each row and use them to populate another column

Here is a simple screenshot of the master worksheet (dummy data) with the cells for the third column highlighted

1655787575354.png

And here is the desired result in the first worksheet after the code has run. Columns F and G for each row must populate the 3rd and 4th row of colC. Rows 1 & 2 are to be left blank

1655787604558.png

Here is the code I have already kindly provided by a forum moderator. This creates each new worksheet and transposes the first 4 columns. But now I just need to bring the other two cells across


Cheers, Luigi


VBA Code:
Sub LuigiCortisone()
   Dim Cl As Range
   Dim Hdr As Variant
   
   With Sheets("Master")
      Hdr = .Range("A1:D1").Value2
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Evaluate("isref('" & Cl.Value & "'!A1)") Then
            Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
            With Sheets(Cl.Value)
               .Range("A1:A4").Value = Application.Transpose(Hdr)
               .Range("B1:B4").Value = Application.Transpose(Cl.Resize(, 4).Value)
            End With
         End If
      Next Cl
   End With
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi Luigi,

So using your code there's two ways I can think of (no doubt there's more):

Copy everything but then delete the extra row:

VBA Code:
Option Explicit
Sub Macro1()
   
   Dim Cl As Range
   Dim Hdr As Variant
   
   Application.ScreenUpdating = False
   
   With Sheets("master")
      Hdr = .Range("A1:G1").Value2
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Evaluate("isref('" & Cl.Value & "'!A1)") Then
            Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
            With Sheets(Cl.Value)
               .Range("A1:A6").Value = Application.Transpose(Hdr)
               .Range("B1:B6").Value = Application.Transpose(Cl.Resize(, 6).Value)
               .Rows(5).Delete
            End With
         End If
      Next Cl
   End With
   
   Application.ScreenUpdating = True
   
End Sub

Or via a cell by cell basis which will become huge if you have many columns:

VBA Code:
Option Explicit
Sub Macro2()

    Dim Cl As Range
    Dim Hdr As Range
    
    Application.ScreenUpdating = False

    With Sheets("master")
        Set Hdr = .Range("A1:D1,F1:G1")
        For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
            If Not Evaluate("isref('" & Cl.Value & "'!A1)") Then
                Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
                Sheets(Cl.Value).Range("A1:A6") = Application.Transpose(Array(.Range("A1"), .Range("B1"), .Range("C1"), .Range("D1"), .Range("F1"), .Range("G1")))
                Sheets(Cl.Value).Range("B1:B6") = Application.Transpose(Array(.Range("A" & Cl.Row), .Range("B" & Cl.Row), .Range("C" & Cl.Row), .Range("D" & Cl.Row), .Range("F" & Cl.Row), .Range("G" & Cl.Row)))
            End If
        Next Cl
    End With
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
How about
VBA Code:
Sub LuigiCortisone()
   Dim Cl As Range
   Dim Hdr As Variant
   
   With Sheets("Master")
      Hdr = .Range("A1:D1").Value2
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Evaluate("isref('" & Cl.Value & "'!A1)") Then
            Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
            With Sheets(Cl.Value)
               .Range("A1:A4").Value = Application.Transpose(Hdr)
               .Range("B1:B4").Value = Application.Transpose(Cl.Resize(, 4).Value)
               .Range("C3:C4").Value = Application.Transpose(Cl.Offset(, 5).Resize(, 2).Value)
            End With
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
Solution
Hi Luigi,

So using your code there's two ways I can think of (no doubt there's more):

Copy everything but then delete the extra row:

VBA Code:
Option Explicit
Sub Macro1()
  
   Dim Cl As Range
   Dim Hdr As Variant
  
   Application.ScreenUpdating = False
  
   With Sheets("master")
      Hdr = .Range("A1:G1").Value2
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Evaluate("isref('" & Cl.Value & "'!A1)") Then
            Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
            With Sheets(Cl.Value)
               .Range("A1:A6").Value = Application.Transpose(Hdr)
               .Range("B1:B6").Value = Application.Transpose(Cl.Resize(, 6).Value)
               .Rows(5).Delete
            End With
         End If
      Next Cl
   End With
  
   Application.ScreenUpdating = True
  
End Sub

Or via a cell by cell basis which will become huge if you have many columns:

VBA Code:
Option Explicit
Sub Macro2()

    Dim Cl As Range
    Dim Hdr As Range
   
    Application.ScreenUpdating = False

    With Sheets("master")
        Set Hdr = .Range("A1:D1,F1:G1")
        For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
            If Not Evaluate("isref('" & Cl.Value & "'!A1)") Then
                Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
                Sheets(Cl.Value).Range("A1:A6") = Application.Transpose(Array(.Range("A1"), .Range("B1"), .Range("C1"), .Range("D1"), .Range("F1"), .Range("G1")))
                Sheets(Cl.Value).Range("B1:B6") = Application.Transpose(Array(.Range("A" & Cl.Row), .Range("B" & Cl.Row), .Range("C" & Cl.Row), .Range("D" & Cl.Row), .Range("F" & Cl.Row), .Range("G" & Cl.Row)))
            End If
        Next Cl
    End With
   
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
Thanks Robert, but it doesn't populate colC. Fluff's code below does the trick

Cheers, L
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
How about
VBA Code:
Sub LuigiCortisone()
   Dim Cl As Range
   Dim Hdr As Variant
  
   With Sheets("Master")
      Hdr = .Range("A1:D1").Value2
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Not Evaluate("isref('" & Cl.Value & "'!A1)") Then
            Sheets.Add(, Sheets(Sheets.Count)).Name = Cl.Value
            With Sheets(Cl.Value)
               .Range("A1:A4").Value = Application.Transpose(Hdr)
               .Range("B1:B4").Value = Application.Transpose(Cl.Resize(, 4).Value)
               .Range("C3:C4").Value = Application.Transpose(Cl.Offset(, 5).Resize(, 2).Value)
            End With
         End If
      Next Cl
   End With
End Sub
Thanks again Fluff. Perfetto!

Cheers, L
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,010
Members
449,204
Latest member
tungnmqn90

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