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: 10
  • sheet 2.jpg
    sheet 2.jpg
    177 KB · Views: 10

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
14,165
Office Version
  1. 2007
Platform
  1. Windows
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,151,821
Messages
5,766,639
Members
425,364
Latest member
jstubs99

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
Top