need macro

FrankSit

New Member
Joined
Nov 13, 2024
Messages
6
Office Version
  1. 2021
Platform
  1. Windows
  2. Mobile
Hello, Anybody can help me on how to make a macro to modify the lay out of my worksheet as shown in Sheet1, to be like the one shown in "To be" sheet. This is the format of report from a database that can be
copied to clip board in excel. Each person has multifield downward, and each group has a blank row cell in excel. In "to be", I want the period total to the line of the name. So that I can use vlookup each person's donation for 2023 compared with 2024

Thank you for any help

Frank
 

Attachments

  • Sheet1.jpg
    Sheet1.jpg
    103.4 KB · Views: 9
  • Tobe sheet.jpg
    Tobe sheet.jpg
    9.6 KB · Views: 9

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Sorry, the image should be this:
 

Attachments

  • Sheet1.jpg
    Sheet1.jpg
    91.7 KB · Views: 8
  • Tobe sheet.jpg
    Tobe sheet.jpg
    13.6 KB · Views: 8
Upvote 0
try
Code:
Sub test()
    Dim a, x
    Sheets("to be").[a1].CurrentRegion.ClearContents
    With Sheets("sheet1")
        With .Range("a1", .Range("a" & Rows.Count).End(xlUp)(1, 6))
            x = Filter(.Parent.Evaluate(Replace("transpose(if(#<>"""",row(#)))", "#", .Columns(6).Address)), False, 0)
            If UBound(x) < 0 Then MsgBox "No data to be Exit Sub"
            a = Application.Index(.Value, Application.Transpose(x), [{1,6}])
        End With
    End With
    Sheets("to be").Cells(1).Resize(UBound(a, 1), 2) = a
End Sub
 
Upvote 0
Ignore above due to data layouts I made was wrong...
Code:
Sub test()
    Dim x, y, i&
    Sheets("to be").[a1].CurrentRegion.ClearContents
    With Sheets("sheet1")
        x = Filter(.[transpose(if(a1:a50000<>"",row(1:50000)))], False, 0)
        y = Filter(.[transpose(if(f1:f50000<>"",row(1:50000)))], False, 0)
        If (UBound(x) < 1) + (UBound(y) < 1) Then Exit Sub
        If UBound(x) <> UBound(y) Then Exit Sub
        ReDim a(1 To UBound(x) + 1, 1 To 2)
        For i = 0 To UBound(x)
            a(i + 1, 1) = .Cells(x(i), "a")
            a(i + 1, 2) = .Cells(y(i), "f")
        Next
    End With
    Sheets("to be").Cells(1).Resize(UBound(a, 1), 2) = a
End Sub
 
Upvote 0
Solution
Welcome to the MrExcel board!

For the future, I suggest that you investigate XL2BB for providing sample data to make it easier for helpers by not having to manually type out sample data to test with.
(If you have trouble with XL2BB, review the "XL2BB Icons greyed out" link in the 'Known XL2BB issues' section near the top of the XL2BB Instructions page linked above.)


Anybody can help me on how to make a macro
Do you really need a macro?

FrankSit.xlsm
ABCDEF
1Donor NamePeriod Total
2Mr. Adateinfoamountsubtotal53.00
3
4Mr.Bdateinfoamount
5infoamount
6infoamount
7infoamount
8infoamount
9infoamountsubtotal
10dateinfoamount
11infoamountsubtotal
12dateinfoamount
13infoamount
14infoamountsubtotal
15dateinfoamount
16infoamount
17infoamountsubtotal
18dateinfoamount
19infoamountsubtotal
20dateinfoamount
21infoamount
22infoamount
23infoamountsubtotal13,282.00
24
25MR.Cdateinfoamountsubtotal1,500.00
Sheet1


Instead, could you use a couple of filter formulas like I have in columns A & B below and then do your lookups like the examples in columns E:F?

FrankSit.xlsm
ABCDEF
1Donor NamePeriod TotalLookup NameAmount
2Mr. A53.00Mr.B13,282.00
3Mr.B13,282.00Mr.Znone
4MR.C1,500.00Mr. A53.00
5
6
To be
Cell Formulas
RangeFormula
A1:A4A1=FILTER(Sheet1!A1:A1000,Sheet1!A1:A1000<>"")
B1:B4B1=FILTER(Sheet1!F1:F1000,Sheet1!F1:F1000<>"")
F2:F4F2=XLOOKUP(E2:E4,A1#,B1#,"none")
Dynamic array formulas.
 
Upvote 0
Ignore above due to data layouts I made was wrong...
Code:
Sub test()
    Dim x, y, i&
    Sheets("to be").[a1].CurrentRegion.ClearContents
    With Sheets("sheet1")
        x = Filter(.[transpose(if(a1:a50000<>"",row(1:50000)))], False, 0)
        y = Filter(.[transpose(if(f1:f50000<>"",row(1:50000)))], False, 0)
        If (UBound(x) < 1) + (UBound(y) < 1) Then Exit Sub
        If UBound(x) <> UBound(y) Then Exit Sub
        ReDim a(1 To UBound(x) + 1, 1 To 2)
        For i = 0 To UBound(x)
            a(i + 1, 1) = .Cells(x(i), "a")
            a(i + 1, 2) = .Cells(y(i), "f")
        Next
    End With
    Sheets("to be").Cells(1).Resize(UBound(a, 1), 2) = a
End Sub
Thank you Fuji, I will try the codes. Can I put the macro in another sheets with a button to run the macro?. I will let you know if it can work for me.

God bless!

Frank
 
Upvote 0
Yes Peter, so that anytime the data impoted from the database, we do not have to chane the filter range.
Not sure exactly how you are getting rid of the old data and inserting the new data but depending on that method, if you make the range big enough to start with to be bigger than any amount of data you are ever likely to have you would never need to change the filter range? For example, if your data is typically tens of thousands of rows, change where I have 1000 to say 200000 and it would always be big enough.

If you really do want a macro then try this after checking/editing sheet names.
VBA Code:
Sub Donations()
  Sheets("Sheet1").Range("A:A,F:F").Copy Sheets("To be").Range("A1")
  Sheets("To be").Columns("A:B").SpecialCells(xlBlanks).Delete Shift:=xlUp
End Sub
 
Upvote 0
Ignore above due to data layouts I made was wrong...
Code:
Sub test()
    Dim x, y, i&
    Sheets("to be").[a1].CurrentRegion.ClearContents
    With Sheets("sheet1")
        x = Filter(.[transpose(if(a1:a50000<>"",row(1:50000)))], False, 0)
        y = Filter(.[transpose(if(f1:f50000<>"",row(1:50000)))], False, 0)
        If (UBound(x) < 1) + (UBound(y) < 1) Then Exit Sub
        If UBound(x) <> UBound(y) Then Exit Sub
        ReDim a(1 To UBound(x) + 1, 1 To 2)
        For i = 0 To UBound(x)
            a(i + 1, 1) = .Cells(x(i), "a")
            a(i + 1, 2) = .Cells(y(i), "f")
        Next
    End With
    Sheets("to be").Cells(1).Resize(UBound(a, 1), 2) = a
End Sub
Thank you very much Fuji, it works for me. My question is if the number of row is more than 50000, I just change the a50000?.

Thank you,

Frank
 
Upvote 0
Did you try the other simple macro suggestion? (Already deals with any number of rows & clearing out any previous data.)
 
Upvote 0

Forum statistics

Threads
1,224,754
Messages
6,180,749
Members
452,996
Latest member
nelsonsix66

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