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!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
How the final result should be:

Code.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
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:00Week 2208:00 TP07:30:0007:30:00217:30 TP09:00:0008:00:00
3John FoxWeek 1216:00 TP10:00:0008:00:00217:30 TP10:00:0008:00:00Week 2216:00 TP10:00:0008:00:00217:30 TP10:00:0008:00:00
4Matt AndersonWeek 1216:00 TP09:00:0007:30:00217:30 TP09:45:0007:15:00Week 2216:00 TP09:00:0007:30:00217:30 TP09:45:0007:15:00
5
6
7
Main
 
Upvote 0
Are always the columns to be copied are from D to P (13 columns)?
 
Upvote 0
Create a new sheet and name it as "Results". Run the following macro, the records will be on the "Results" sheet.

VBA Code:
Sub copyrange()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim dic As Object
  Dim i As Long, col As Long, lr As Long
  Dim f As Range
  Dim v As Variant
  
  Application.ScreenUpdating = False
  
  Set sh1 = Sheets("Main")
  Set sh2 = Sheets("Results")
  Set dic = CreateObject("Scripting.Dictionary")
  dic.comparemode = vbTextCompare
  sh2.Rows("2:" & Rows.Count).ClearContents
  
  For i = 2 To sh1.Range("B" & Rows.Count).End(3).Row
    v = sh1.Range("B" & i).Value
    If Not dic.exists(v) Then
      dic(v) = -1
    Else
      dic(v) = dic(v) + 1
    End If
    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 * dic(v))
      sh1.Range("D" & i & ":P" & i).Copy sh2.Cells(f.Row, col)
    End If
  Next
  
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Create a new sheet and name it as "Results". Run the following macro, the records will be on the "Results" sheet.

VBA Code:
Sub copyrange()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim dic As Object
  Dim i As Long, col As Long, lr As Long
  Dim f As Range
  Dim v As Variant
 
  Application.ScreenUpdating = False
 
  Set sh1 = Sheets("Main")
  Set sh2 = Sheets("Results")
  Set dic = CreateObject("Scripting.Dictionary")
  dic.comparemode = vbTextCompare
  sh2.Rows("2:" & Rows.Count).ClearContents
 
  For i = 2 To sh1.Range("B" & Rows.Count).End(3).Row
    v = sh1.Range("B" & i).Value
    If Not dic.exists(v) Then
      dic(v) = -1
    Else
      dic(v) = dic(v) + 1
    End If
    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 * dic(v))
      sh1.Range("D" & i & ":P" & i).Copy sh2.Cells(f.Row, col)
    End If
  Next
 
  Application.ScreenUpdating = True
End Sub
Hello Dante,

Thank you for responding. I've ran that code and I get an error code: "Run-time '429': ActiveX component can't create object"

The line stops at Set dic = CreateObject("Scripting.Dictionary")

Any assistance in debugging?

Thank you in advance
 
Upvote 0
🤦‍♂️Again I put the dictionary statement and it doesn't work in your version. I must create another macro, when I have it, then I post it.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,187
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