Find function in VBA

zackii

New Member
Joined
Nov 12, 2014
Messages
22
I've tried for a few hours but have failed to accomplish what I am after so any help would be greatly appreciated PLEASE.

Here's the data that I am working with (details follow after the table):

Activity CodeDescription
Boston_Corporate

<tbody>
</tbody>
T100
Boston_CorporateT2EQ
Oahu_PVP

<tbody>
</tbody>
T2w
Oahu_PVPT3q
Oahu_PVPT585
Oahu_PVPT6qw
Oahu_PVPT7w
Overhead_General

<tbody>
</tbody>
T8ty
Overhead_General

<tbody>
</tbody>
T9af
Overhead_General

<tbody>
</tbody>
T10855

<tbody>
</tbody>


The first row is the header row.
I am trying to write a macro that;
  1. Searches for the first UNIQUE value in Column A (it's Boston_Corporate right now) and then add a blank row after that.
  2. Searches for the second UNIQUE value in Column A (it's Oahu_PVP right now) and then add a blank row after that.
  3. Searches for the third UNIQUE value in Column A (it's Overhead_General right now) and then add a blank row after that.
  4. The macro continues to do so until it has 'actioned' all unique values in Column A

Worth mentioning:
The data will always be sorted by Column A beforehand.
There may be 100s of rows involved with hundreds of unique values in Column A so values cannot be hardcoded in the macro (need to be dynamic)
 
Re: Need help with the Find function in VBA

@Rick Rothstein; how can I modify that code such that it still works if there are only 2 rows of data, the first row being the header row. Please see below:

Activity CodeDescription
Boston_CorporateT100

<tbody>
</tbody>

It should still merge cell A1 and A2 please.

Again, thanks a million for all the help so far.
Well, that is annoying... if there are multiple areas, then the first area is respected, but if there is only one area, it is changed to the CurrentRegion. The following code should work around the problem...
Code:
[table="width: 500"]
[tr]
	[td]Sub MergeCells()  Dim Ar As Range
  For Each Ar In Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlConstants).Areas
    If Split(Ar.Address(0, 0), ":")(0) = "A1" Then Set Ar = Range("A2")
    If Ar.Count > 1 Then Ar.Offset(1).Resize(Ar.Count - 1).ClearContents
    Ar.Resize(Ar.Count + 1).Merge
    Ar.HorizontalAlignment = xlCenter
    Ar.VerticalAlignment = xlCenter
  Next
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Re: Need help with the Find function in VBA

Well, that is annoying... if there are multiple areas, then the first area is respected, but if there is only one area, it is changed to the CurrentRegion. The following code should work around the problem...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub MergeCells()  Dim Ar As Range
  For Each Ar In Range("A2", Cells(Rows.Count, "A").End(xlUp)).SpecialCells(xlConstants).Areas
    If Split(Ar.Address(0, 0), ":")(0) = "A1" Then Set Ar = Range("A2")
    If Ar.Count > 1 Then Ar.Offset(1).Resize(Ar.Count - 1).ClearContents
    Ar.Resize(Ar.Count + 1).Merge
    Ar.HorizontalAlignment = xlCenter
    Ar.VerticalAlignment = xlCenter
  Next
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
@Rick Rothstein, sorry but that code still does not merge cells A2 and A3, if there are only 2 rows of data, the first row being the header row :(

Any ideas why?
 
Upvote 0
Re: Need help with the Find function in VBA

@Rick Rothstein, sorry but that code still does not merge cells A2 and A3, if there are only 2 rows of data, the first row being the header row :(

Any ideas why?
I am not sure what to tell you as it works correctly for me.

Can you describe to me what my latest code actually does for you, if anything?
 
Upvote 0
Re: Need help with the Find function in VBA

I am not sure what to tell you as it works correctly for me.

Can you describe to me what my latest code actually does for you, if anything?

Disregard my last comment please. The code is working perfectly and is doing what it is supposed to do. Thanks again for all your help. Truly appreciated.
 
Upvote 0

Forum statistics

Threads
1,216,115
Messages
6,128,923
Members
449,479
Latest member
nana abanyin

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