Make a selection based on a date

SamyB

New Member
Joined
Sep 14, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,

I am trying to find a macro solution to the following formatting issue.

I have 2 columns in sheet1 (see attached picture); the date column and Value 1 column. I have multiple values for the same date for the whole year and I want to change the format to be as per sheet 2 (attached picture)
Basically, I want all the values of the same date to be copied and pasted in a transposed manner in the correct row on sheet 2. The selection of the values in sheet 1 should be based on the date. it would be great to have a loop that goes through the dates by increasing one day at a time.

in my own programing language, it would be:

Select range in column B where the range in column A date is equal to a specific date
copy the selection
paste transposed in sheet2
go to the next date and repeat

Thank you in advance
Regards
SamyB
 

Attachments

  • sheet 1.jpg
    sheet 1.jpg
    150.3 KB · Views: 11
  • sheet 2.jpg
    sheet 2.jpg
    177 KB · Views: 12

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Welcome to the MrExcel Message Board!

Try this:

VBA Code:
Sub TransposeValues()
  Dim a As Variant, b As Variant
  Dim dic As Object
  Dim fec As Double
  Dim i As Long, j As Long, k As Long
  
  Set dic = CreateObject("Scripting.Dictionary")
  a = Sheets("Sheet1").Range("A2", Sheets("Sheet1").Range("B" & Rows.Count).End(3)).Value2
  b = Sheets("Sheet2").Range("A2", Sheets("Sheet2").Range("C" & Rows.Count).End(3)).Value2
  ReDim c(1 To UBound(b, 1), 1 To 1000)
  
  For i = 1 To UBound(b, 1)
    fec = CDate(b(i, 3) & "/" & b(i, 2) & "/" & b(1, 1))
    dic(fec) = i & "|0"
  Next
  
  For i = 1 To UBound(a, 1)
    If dic.exists(a(i, 1)) Then
      j = Split(dic(a(i, 1)), "|")(0)
      k = Split(dic(a(i, 1)), "|")(1) + 1
      c(j, k) = a(i, 2)
      dic(a(i, 1)) = j & "|" & k
    End If
  Next
  
  Sheets("Sheet2").Range("D2").Resize(UBound(c, 1), UBound(c, 2)).Value = c
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,694
Members
448,979
Latest member
DET4492

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