I need to merge cells using a formula so that the cells only merge when cells on another tab are filled.
I have 2 tabs with the same amount of columns in each. I want cells a1-d1 to merge in tab 1 when cells a1-d1 in tab 2 are filled and for the value of d1 in tab 2 to be inputted into the newly merged cells in tab 1.
this is what I have:
I need to merge cells using a formula so that the cells only merge
Share
Excel VBA Methods and Function (Excel Macros) overview
Since you want to change cells i do not believe that you can use a formula (even not a user defined one). Therefore i wrote an excel vba macro for your problem.
Fully working demo tested with 4 columns and 10 rows
Below you can see the result. The values from sheet 2 haven been copied to sheet 1 (second image). In Sheet 1 the Cells in a row are merged (in row 2 from Cell A2 up to Cell D2 (A2-D2 is now just one cell) if in the first image (sheet 2) every cell (from column a to column d) in a row had a value.
Bugs in the modified code
There are a few things in the modifiend code that are not possible or could lead to a wrong understanding
For i = A To dis not possible. If you want to use a loop you have to use numbers:For i = 1 To 4this would repeat the code betweenForandNext4 times starting with 1Cells(curColumn, 11).Valueis technical correct but misleading. Excel uses the first value after(for the row-index and the second value for the column-index. Both values have to be a number: Cells(4,2).Value returns the Cell value from the 4th. row and the second Column (in the Excel Gui the Cell B4)Try changing this line
For i = A To dto thisFor i = 1 To 4and see if that returns the wished result.Bugs part 2
In your other modification you have some of the same bugs:
For curColumn = A to dneeds numbers instead of letters (unless A and d were a variable filled with a number but according to your code sample this is not the casecellValue = Sheets(2).Cells(curColumn, d).Valuehas the same bug, if d is just the letter d and not something liked = 4than you can not use it in a loop.This is the code from your comment:
Be carefull it is not running.