Reference single cells in a column to the left of merged cells

wallen1605

Board Regular
Joined
Dec 15, 2017
Messages
57
Good morning

Please could someone help me with the VBA code to reference individual cells to the left of a merged cell? I know the issues around using merged cells in excel, however i need to keep them in this case.

Basically, i have code that checks column A for the same reference and then merges column H cells (e.g Cells A2, A3, A4 have the same cell value, cells H2, H3, H4 are merged and so on down the rows. Cells B2, B3, B4 each have a number and i need to add these together and show the total in the merged cell of A2, A3, A4, so i need to reference the cells from the merged cell. Obviously i can use an offset of mergearea(1).address but this only retrieves the value in B2. Please could someone help on the code line i need to reference all cells adjacent to the merge cells.

Thanks in advance.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Why not use " Center across selection" instead of merging. You can than easily reference the cell you need
 
Upvote 0
Why not use " Center across selection" instead of merging. You can than easily reference the cell you need
Yes, i thought of that as well, unfortunately my boss does not like that appearance. Is there any way to reference each cell from a merged cell (note, the amount of cells in a merged cell can differ)?
 
Upvote 0
Cells A2, A3, A4 have the same cell value
...show the total in the merged cell of A2, A3, A4,
That is not clear to me. Firstly you say A2, A3 and A4 have the same value but then you want to put a value in that merged area?


Why not use " Center across selection" instead of merging.
Yes, i thought of that as well,
Center Selection does have a vertical equivalent (A2, A3 & A4 being vertical) so I'm not sure what you actually envisage for that?


Any chance we could have a small set of dummy data and expected results with XL2BB and any further explanation in relation to that sample?
 
Upvote 0
That is not clear to me. Firstly you say A2, A3 and A4 have the same value but then you want to put a value in that merged area?




Center Selection does have a vertical equivalent (A2, A3 & A4 being vertical) so I'm not sure what you actually envisage for that?


Any chance we could have a small set of dummy data and expected results with XL2BB and any further explanation in relation to that sample?

No problem, please see below:

AWTestSystem.xlsm
ABCDEFGHIJKL
1INTERNAL REFORDER DATEBOOKED OUT ITEM SENTQTYCUSTOMER NAMEADDRESSTOTAL ITEMSPICKING COSTPOSTAGE COSTTOTAL COST BOX QTY
2SR_2019_1040_160819_09300519/07/1922/07/19ITEM NAME1NAMECUSTOMER ADDRESS
3SR_2019_1040_160819_09300519/07/1922/07/19ITEM NAME1NAMECUSTOMER ADDRESS
4SR_2019_1040_160819_09300519/07/1922/07/19ITEM NAME3NAMECUSTOMER ADDRESS
5SR_2019_1040_160819_09300519/07/1922/07/19ITEM NAME1NAMECUSTOMER ADDRESS
6
7SR_2019_1040_170819_09300529/07/1929/07/19ITEM NAME10NAMECUSTOMER ADDRESS
8
9SR_2019_1040_180819_09300506/08/1906/08/19ITEM NAME1NAMECUSTOMER ADDRESS
10SR_2019_1040_180819_09300506/08/1906/08/19ITEM NAME1NAMECUSTOMER ADDRESS
11SR_2019_1040_180819_09300506/08/1906/08/19ITEM NAME1NAMECUSTOMER ADDRESS
12
13SR_2019_1040_190819_09300515/08/1915/08/19ITEM NAME1NAMECUSTOMER ADDRESS
14SR_2019_1040_190819_09300515/08/1915/08/19ITEM NAME5NAMECUSTOMER ADDRESS
15SR_2019_1040_190819_09300515/08/1915/08/19ITEM NAME1NAMECUSTOMER ADDRESS
16SR_2019_1040_190819_09300515/08/1915/08/19ITEM NAME1NAMECUSTOMER ADDRESS
17
18SR_2019_1003_200819_10295922/08/1922/08/19ITEM NAME1NAMECUSTOMER ADDRESS
19SR_2019_1003_200819_10295922/08/1922/08/19ITEM NAME2NAMECUSTOMER ADDRESS
20SR_2019_1003_200819_10295922/08/1922/08/19ITEM NAME1NAMECUSTOMER ADDRESS
21
22SR_2019_1004_310819_14090531/08/1902/09/19ITEM NAME1NAMECUSTOMER ADDRESS
23SR_2019_1004_310819_14090531/08/1902/09/19ITEM NAME8NAMECUSTOMER ADDRESS
24SR_2019_1004_310819_14090531/08/1902/09/19ITEM NAME1NAMECUSTOMER ADDRESS
25SR_2019_1004_310819_14090531/08/1902/09/19ITEM NAME1NAMECUSTOMER ADDRESS
Sheet2
 
Upvote 0
No problem, please see below:

AWTestSystem.xlsm
ABCDEFGHIJKL
1INTERNAL REFORDER DATEBOOKED OUT ITEM SENTQTYCUSTOMER NAMEADDRESSTOTAL ITEMSPICKING COSTPOSTAGE COSTTOTAL COST BOX QTY
2SR_2019_1040_160819_09300519/07/1922/07/19ITEM NAME1NAMECUSTOMER ADDRESS
3SR_2019_1040_160819_09300519/07/1922/07/19ITEM NAME1NAMECUSTOMER ADDRESS
4SR_2019_1040_160819_09300519/07/1922/07/19ITEM NAME3NAMECUSTOMER ADDRESS
5SR_2019_1040_160819_09300519/07/1922/07/19ITEM NAME1NAMECUSTOMER ADDRESS
6
7SR_2019_1040_170819_09300529/07/1929/07/19ITEM NAME10NAMECUSTOMER ADDRESS
8
9SR_2019_1040_180819_09300506/08/1906/08/19ITEM NAME1NAMECUSTOMER ADDRESS
10SR_2019_1040_180819_09300506/08/1906/08/19ITEM NAME1NAMECUSTOMER ADDRESS
11SR_2019_1040_180819_09300506/08/1906/08/19ITEM NAME1NAMECUSTOMER ADDRESS
12
13SR_2019_1040_190819_09300515/08/1915/08/19ITEM NAME1NAMECUSTOMER ADDRESS
14SR_2019_1040_190819_09300515/08/1915/08/19ITEM NAME5NAMECUSTOMER ADDRESS
15SR_2019_1040_190819_09300515/08/1915/08/19ITEM NAME1NAMECUSTOMER ADDRESS
16SR_2019_1040_190819_09300515/08/1915/08/19ITEM NAME1NAMECUSTOMER ADDRESS
17
18SR_2019_1003_200819_10295922/08/1922/08/19ITEM NAME1NAMECUSTOMER ADDRESS
19SR_2019_1003_200819_10295922/08/1922/08/19ITEM NAME2NAMECUSTOMER ADDRESS
20SR_2019_1003_200819_10295922/08/1922/08/19ITEM NAME1NAMECUSTOMER ADDRESS
21
22SR_2019_1004_310819_14090531/08/1902/09/19ITEM NAME1NAMECUSTOMER ADDRESS
23SR_2019_1004_310819_14090531/08/1902/09/19ITEM NAME8NAMECUSTOMER ADDRESS
24SR_2019_1004_310819_14090531/08/1902/09/19ITEM NAME1NAMECUSTOMER ADDRESS
25SR_2019_1004_310819_14090531/08/1902/09/19ITEM NAME1NAMECUSTOMER ADDRESS
Sheet2

So, i have code that looks through the entire sheet and for each row with matching Internal Ref`s, it will merge the rows in columns H, I, J, K and L. The code then also adds the prices, based on the POSTAGE cost previously entered by a user. I still need each row if data to remain, but to sum the Qty cells (where the Internal Ref matches) and put the total in the row cell of column H.
 
Upvote 0
Thanks, that is much easier to understand with the sample.

Could you use something like this?

VBA Code:
Sub Tot_Qty()
  Dim rA As Range
  
  For Each rA In Columns("E").SpecialCells(xlConstants, xlNumbers).Areas
    rA.Cells(1, 4).Value = Application.Sum(rA)
  Next rA
End Sub
 
Upvote 0
Thanks, that is much easier to understand with the sample.

Could you use something like this?

VBA Code:
Sub Tot_Qty()
  Dim rA As Range
 
  For Each rA In Columns("E").SpecialCells(xlConstants, xlNumbers).Areas
    rA.Cells(1, 4).Value = Application.Sum(rA)
  Next rA
End Sub

Thank you very much, that worked brilliant. Can i ask please, if all rows had data instead of grouped with a blank row inbetween, what would i change the code to? (I assume the .Areas is where the change needs to be?).

Thanks in advance.
 
Upvote 0
if all rows had data instead of grouped with a blank row inbetween, what would i change the code to?
Try

VBA Code:
Sub Tot_Qty_v2()
  Dim rw As Long
  
  rw = 2
  Do Until rw > Range("A" & Rows.Count).End(xlUp).Row
    With Cells(rw, "H").MergeArea
      .Value = Application.Sum(.Offset(, -3).Resize(.Rows.Count))
      rw = rw + .Rows.Count
    End With
  Loop
End Sub
 
Upvote 0
Try

VBA Code:
Sub Tot_Qty_v2()
  Dim rw As Long
 
  rw = 2
  Do Until rw > Range("A" & Rows.Count).End(xlUp).Row
    With Cells(rw, "H").MergeArea
      .Value = Application.Sum(.Offset(, -3).Resize(.Rows.Count))
      rw = rw + .Rows.Count
    End With
  Loop
End Sub

Thanks Peter, that works great too. Thank you very much for your help.
 
Upvote 0

Forum statistics

Threads
1,214,879
Messages
6,122,065
Members
449,064
Latest member
scottdog129

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