How to Find the Next non-empty cell using vba excel (plus merge it)

killpaddy

Board Regular
Joined
Jul 31, 2012
Messages
52
Hi,

I have a afew columns of data related to eachother. Column A contains text in some cells then afew blank cells then another cell with text etc.
I would like to merge the text cells with all the blank cells below it (until the next non-empty cell is reached).
However, I dont know the code to 'find next non-empty cell'
Here's my code so far:
Code:
Dim startfrom As Long
Dim Merge1 As Long
Dim Merge2 As Long

startfrom = 1
Do
    Merge1 = Range("A" & startfrom & ":A" & LastRow).Find("").Row - 1
    Merge2 = Range("A" & Merge1 & ":A" & LastRow).Find([!""]).Row  'error here
    Range("A" & Merge1 & ":A" & Merge2 - 1).Merge
    startfrom = Merge2

Loop Until Range("A" & LastRow) Is Not Nothing 'error probably here aswell

Ive tried different combinations of the '[!""]' and none of them work. can anyone suggest code that would work?

Also how to you get it to 'merge and centre'?

And does anyone know if I save this workbook as a .CSV file whether it will still contain the merged cells when opened somewhere else?

Any help would be appreciated, Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Sample Table BEFORE Running of Macro (Foo)...


Excel 2010
ABCD
1COL_A_HEADERBCD
2AAA121359236
3395175255
4198449255
5238133151
6254208109
7BBB437464349
8477436358
9223290383
10178124148
11270335128
12CCC492249428
13170366131
14294470420
15411204261
16368107293
17DDD208300353
18129445289
19354185292
20365430455
Sheet1


Paste below into a standard Module:

Code:
Sub Foo()
Dim arr()
Dim LR As Long, ct As Long, t As Long
Dim Rng As Range, c As Range
Application.ScreenUpdating = False
LR = Range("B" & Rows.Count).End(xlUp).Row
ct = 1
Set Rng = Range("A2:A" & LR)
For Each c In Rng
    If c.Value <> "" Then
        ReDim Preserve arr(ct)
            arr(ct) = c.Row
        ct = ct + 1
    End If
Next c
For t = 1 To UBound(arr) - 1
With Range(Cells(arr(t), "A"), Cells(arr(t + 1) - 1, "A"))
    .Merge
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
End With
Next t
With Range(Cells(arr(t), "A"), Cells(LR, "A"))
    .Merge
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
End With
Application.ScreenUpdating = True
End Sub

Back up your Data before trying this...
 
Upvote 0
Thanks jim,

That code worked perfectly for what I needed, and my code updates the sheet with new data before this bit of code anyway.

I just have a couple of general interest questions about your code (as im a bit of a novice in VBA)

Does the 'ScreenUpdating' = Ture/False, speed up the program? I wasnt aware of it and I have a coulpe of other bits of code with loops in that I could use it for, are there any conditions i doesnt work under.
What is 'arr()' is it to do with arrays? I havn't used them in VBA yet.

Also, do you know if the cells will remain merged if I save as a .csv file?
 
Upvote 0
Does the 'ScreenUpdating' = True/False, speed up the program? I wasnt aware of it and I have a coulpe of other bits of code with loops in that I could use it for, are there any conditions i doesnt work under.

It eliminates any SCREEN-FLASHING/MOVEMENT WHILE MACRO CHANGES THE SHEET -- it does also speed up code (I'm pretty sure).. It falls under BEST PRACTICES (to me)..

What is 'arr()' is it to do with arrays? I havn't used them in VBA yet.

Yes arr() is an array, a one-dimensional type (used here to capture the row numbers of your Column A Text cells)

Also, do you know if the cells will remain merged if I save as a .csv file?

Not sure,,,,, Try it and Find out

Glad it helped Jim
 
Upvote 0
Ok thanks for the tips.

It does work when saving as a .csv. However I now have a new problem.

I'm saving as a .csv so I can upload it to an online stocklist (which has a system for 'bulk importing' data in a csv format), unfortunately the merged cells don't trasfer to this (I dont think it's possible, I have an alternative anyway). The only problem is that my data in column A (as can be seen in the google doc) the text has line separators in:
e.g.
Quick Turn 8; 10
8 Station; 8 Position

in csv text file appears as:
"Quick Turn 8; 10
8 Station; 8 Position",Turning Holder,C3-*CE-QT10-030026M,0,,

The website reads this as two separate cells with text in, I want it to read the same as in excel (like the google doc).

Im thinking perhaps something like '...8; 10[tab] 8 Station...' and I want to be able to do this using the VBA code rather than manually

Have any Ideas?
 
Upvote 0

Forum statistics

Threads
1,217,037
Messages
6,134,131
Members
449,861
Latest member
DMJHohl

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