Copy Range To Rows Using VBA Code

Remi909

New Member
Joined
Mar 22, 2022
Messages
29
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I have the following worksheet that I need to transpose according to the number of occurrence in a column (same name etc) within a specific column (B1) and copy the entire column and its row content on to one new row below. The 2nd occurrence of the same name should start on Column Q in this case:

Code.xlsm
ABCDEFGHIJKLMNOPQR
1SiteNameStart WeekdayWeek 1 NameWeek 1 Min PaidWeek 1 Max PaidWeek 1 1 DayOffWeek 1 1 ShiftWeek 1 1 StartWeek 1 1 PaidWeek 1 1 Activity 1Week 1 2 DayOffWeek 1 2 ShiftWeek 1 2 StartWeek 1 2 PaidWeek 1 2 Activity 1
2Green SiteAdam SmithWeek 1208:00 TP07:30:0007:30:00217:30 TP09:00:0008:00:00
3Adam SmithWeek 2208:00 TP07:30:0007:30:00217:30 TP09:00:0008:00:00
4John FoxWeek 1216:00 TP10:00:0008:00:00217:30 TP10:00:0008:00:00
5John FoxWeek 2216:00 TP10:00:0008:00:00217:30 TP10:00:0008:00:00
6Matt AndersonWeek 1216:00 TP09:00:0007:30:00217:30 TP09:45:0007:15:00
7Matt AndersonWeek 2216:00 TP09:00:0007:30:00217:30 TP09:45:0007:15:00
8
9
10
11
Main


Thanks in advance!
 
Create a new sheet and name it as "Results". Run the following macro, the records will be on the "Results" sheet.

Try this:

VBA Code:
Sub copyrange()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim i As Long, col As Long, lr As Long, n As Long
  Dim f As Range
  Dim v As Variant
 
  Application.ScreenUpdating = False
 
  Set sh1 = Sheets("Main")
  Set sh2 = Sheets("Results")
  sh2.Rows("2:" & Rows.Count).ClearContents
 
  For i = 2 To sh1.Range("B" & Rows.Count).End(3).Row
    v = sh1.Range("B" & i).Value
    n = WorksheetFunction.CountIf(sh1.Range("B1:B" & i - 1), v) - 1
    Set f = sh2.Range("B:B").Find(v, , xlValues, xlWhole, , , False)
    If f Is Nothing Then
      lr = sh2.Range("B" & Rows.Count).End(3).Row + 1
      sh1.Range("A" & i & ":P" & i).Copy sh2.Range("A" & lr)
    Else
      col = Columns("Q").Column + (13 * n)
      sh1.Range("D" & i & ":P" & i).Copy sh2.Cells(f.Row, col)
    End If
  Next
 
  Application.ScreenUpdating = True
End Sub

Hi Dante,

That worked.
Could break down each line of code and what it does just for a better understanding from my end?

Thanks in advance
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Could break down each line of code and what it does just for a better understanding from my end?

VBA Code:
Sub copyrange()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim i As Long, col As Long, lr As Long, n As Long
  Dim f As Range
  Dim v As Variant
  
  Application.ScreenUpdating = False
  
  Set sh1 = Sheets("Main")
  Set sh2 = Sheets("Results")
  sh2.Rows("2:" & Rows.Count).ClearContents
  
  For i = 2 To sh1.Range("B" & Rows.Count).End(3).Row
    'Puts the data in the variable v
    v = sh1.Range("B" & i).Value
    'Count the occurrences of v in column B, it is to know in which column you should put the next block.
    n = WorksheetFunction.CountIf(sh1.Range("B1:B" & i - 1), v) - 1
    'Look for v in the results sheet, in column B.
    Set f = sh2.Range("B:B").Find(v, , xlValues, xlWhole, , , False)
    If f Is Nothing Then
      'If the method does not find it, then it adds a record.
      lr = sh2.Range("B" & Rows.Count).End(3).Row + 1
      sh1.Range("A" & i & ":P" & i).Copy sh2.Range("A" & lr)
    Else
      'If it exists, then add the columns
      col = Columns("Q").Column + (13 * n)
      sh1.Range("D" & i & ":P" & i).Copy sh2.Cells(f.Row, col)
    End If
  Next
  
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
VBA Code:
Sub copyrange()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim i As Long, col As Long, lr As Long, n As Long
  Dim f As Range
  Dim v As Variant
 
  Application.ScreenUpdating = False
 
  Set sh1 = Sheets("Main")
  Set sh2 = Sheets("Results")
  sh2.Rows("2:" & Rows.Count).ClearContents
 
  For i = 2 To sh1.Range("B" & Rows.Count).End(3).Row
    'Puts the data in the variable v
    v = sh1.Range("B" & i).Value
    'Count the occurrences of v in column B, it is to know in which column you should put the next block.
    n = WorksheetFunction.CountIf(sh1.Range("B1:B" & i - 1), v) - 1
    'Look for v in the results sheet, in column B.
    Set f = sh2.Range("B:B").Find(v, , xlValues, xlWhole, , , False)
    If f Is Nothing Then
      'If the method does not find it, then it adds a record.
      lr = sh2.Range("B" & Rows.Count).End(3).Row + 1
      sh1.Range("A" & i & ":P" & i).Copy sh2.Range("A" & lr)
    Else
      'If it exists, then add the columns
      col = Columns("Q").Column + (13 * n)
      sh1.Range("D" & i & ":P" & i).Copy sh2.Cells(f.Row, col)
    End If
  Next
 
  Application.ScreenUpdating = True
End Sub

Thank you!!
 
Upvote 0

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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