Macro to calculate totals and collate lists into different cells

aydanrogers

New Member
Joined
Apr 1, 2016
Messages
11
Hello all,

I am after a macro that will find certain text in column A (eg. 1D, 17, 37A) the take the text from that cell and collate it into a list in another cell (example: A1 contains"1D - Eggs", A15 contains "17 - Chickens", A28 contains "37A - Turkeys") then placing that text into another cell (eg. 1D - Eggs, 17 - Chickens, 37A - Turkeys).

The next part of the macro will then take the number figure from column B next to the cells identified from the first part and total the number and paste that number into a cell (eg. B1 + B15 + B28 cell numbers = 28)

Hopefully this makes sense to you all, I am hopeless at making macros from scratch like this

Cheers in advanced

Aydos
 

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.
Try the following, in the cell write the "certain texts" separated by commas.
Run the macro and the results will be in cells E2 and F2:

Dante Amor
ABCDEF
1TEXTVALUEcertain textResultSum
21D - Eggs11D, 17, 37A1D - Eggs - 17 - Chickens - 37A - Turkeys14
3data2
4data3
517 - Chickens4
6data5
7data6
8data7
9data8
1037A - Turkeys9
11
Hoja1


VBA Code:
Sub calculate_totals()
  Dim sText As String, cad As String, s As Variant
  Dim f As Range, sum As Double
  
  sText = Range("D2").Value
  For Each s In Split(sText, ",")
    Set f = Range("A:A").Find(Trim(s), , xlValues, xlPart, , , False)
    If Not f Is Nothing Then
      cad = cad & f & " - "
      sum = sum + f.Offset(, 1)
    End If
  Next
  If cad <> "" Then Range("E2").Resize(1, 2).Value = Array(Left(cad, Len(cad) - 3), sum)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,936
Messages
6,122,340
Members
449,079
Latest member
rocketslinger

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