Search to blank and merge macro help needed

rickyckc

Active Member
Joined
Apr 1, 2004
Messages
327
Hi everyone,

I need your excel macro expertise help. Suppose I have a column of data like this:

A1: Router A
A2: Router B
A3: Router B
A4: Router B
A5: Switch A
A6: Switch B
A7: Switch C
A8: Switch C
A9: Switch C

Now, I need a macro that will first of all, search and blank off A3, A4, A8 and A9 since they are repeating and stops when no more repeats are found. And later part is to merge A2-A4 and A7-A9. Important is the 'search & blank' and 'merge' part is not cell specified. Hope you understand what I am trying to accomplish. Thanks for your attention and time.

:rolleyes:

Best Regards,
Ricky
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Assuming your list is sorted, try this (not neat but will work):

Code:
Sub macro()
Range("A100").Select
Selection.End(xlUp).Select
counter = 0
Test1 = ActiveCell.Text
Test2 = ActiveCell.Offset(-1, 0).Text
Test3 = ActiveCell.Row
Do Until counter = Test3
If Test1 = Test2 Then
Selection.ClearContents
End If
ActiveCell.Offset(-1, 0).Select
counter = counter + 1
Test1 = ActiveCell.Text
On Error GoTo Finish
Test2 = ActiveCell.Offset(-1, 0).Text
Loop

Finish:
MsgBox ("Done")
End Sub
 
Upvote 0
Hi tactps,

I have tried yur scripts, works great but I still have 3 questions :P

1) Your script will not work if the column A is not sorted, corect ?
(sorry, I have not tried that out yet)

2) I need to set the range ? Can it somehow be 'auto' (meaning no need to set range)

3) How about the merge part ? Based on my column example, I need to merge A2, A3 and A4 together as one cell.

Thanks again.

Best Regards,
Ricky
 
Upvote 0
Your post:
1) Your script will not work if the column A is not sorted, corect ?
(sorry, I have not tried that out yet)

2) I need to set the range ? Can it somehow be 'auto' (meaning no need to set range)

3) How about the merge part ? Based on my column example, I need to merge A2, A3 and A4 together as one cell.

Re:
1) - correct. you would need to sort the code in column A
2) - I have it going to the bottom of column A (assuming that you have less than 100 rows).
If not, change:
Range("A100").Select
to
Range("A60000").select

3) I am not sure what you mean by merge. Do you have data in other columns?

Perhaps you need to post your spreadsheet (details changed to protect the guilty) using Colo's HTML Maker (bottom of screen).

Then I'm sure we can help you out better.
:biggrin:
 
Upvote 0
Hi tactps,

I have created a html sample .....DUH.....now how do I post/show it ?

:oops:

Best Regards,
Ricky
 
Upvote 0
Hi tactps,
sample.xls
ABCD
1HostnameIPAddressDeviceTypeLocation
2AIX_Server_A4.3.2.1AXIHongKong
3Switch_A5.6.7.8SWITCHSingapore
4Switch_A5.6.8.7SWITCHSingapore
5Switch_A5.6.8.9SWITCHSingapore
6Netware_Server_A1.1.1.1NETWAREJapan
7Router_A6.7.3.4ROUTERUSA
8Router_A6.7.8.9ROUTERUSA
9Router_A6.7.9.8ROUTERUSA
10SUN_Server_A2.2.2.2SUNKorea
11Linux_Server_A3.3.3.3LINUXAustralia
12Router_B9.8.7.6ROUTERThailand
13Router_B9.8.1.2ROUTERThailand
14Router_B9.8.3.4ROUTERThailand
15
16HostnameIPAddressDeviceTypeLocation
17AIX_Server_A4.3.2.1AXIHongKong
18Switch_A5.6.7.8SWITCHSingapore
195.6.8.7
205.6.8.9
21Netware_Server_A1.1.1.1NETWAREJapan
22Router_A6.7.3.4ROUTERUSA
236.7.8.9
246.7.9.8
25SUN_Server_A2.2.2.2SUNKorea
26Linux_Server_A3.3.3.3LINUXAustralia
27Router_B9.8.7.6ROUTERThailand
289.8.1.2
299.8.3.4
All Devices


OK. I have managed to figure it out how to post the html file.

Now.....the top portion is the raw file

and the bottom portion is my preferred final output

Thanks.

Best Regards,
Ricky
 
Upvote 0
This should be close to what you want. I'm sure an MVP out there can clean it up a fair bit as it is far longer than it needs to be:

Code:
Sub macro()
Range("A100").Select
Selection.End(xlUp).Select
counter = 0
Test1 = ActiveCell.Text
Test2 = ActiveCell.Offset(-1, 0).Text
Test3 = ActiveCell.Row
Do Until counter = Test3
If Test1 = Test2 Then
Selection.ClearContents
End If
ActiveCell.Offset(-1, 0).Select
counter = counter + 1
Test1 = ActiveCell.Text
On Error GoTo Step2
Test2 = ActiveCell.Offset(-1, 0).Text
Loop


Step2:
Range("A1").Select
countermerge = 0
Do While countermerge < 100
On Error GoTo Finish
Merge1 = ActiveCell.Text
Merge2 = ActiveCell.Offset(1, 0).Text
Merge3 = ActiveCell.Offset(2, 0).Text
Merge4 = ActiveCell.Address
Merge5 = ActiveCell.Offset(2, 0).Address

If Merge1 <> "" And Merge2 = "" And Merge3 = "" Then
Range(Merge4 & ":" & Merge5).Select
    With Selection
        .HorizontalAlignment = xlLeft
        .VerticalAlignment = xlCenter
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .MergeCells = True
    End With
If Selection.Interior.ColorIndex = ActiveCell.Offset(-1, 0).Interior.ColorIndex Then
If Selection.Interior.ColorIndex = xlNone Then
Selection.EntireRow.Select
Selection.Interior.ColorIndex = 6
Else
ActiveCell.EntireRow.Select
Selection.Interior.ColorIndex = xlNone
End If
End If
End If

countermerge = countermerge + 1
ActiveCell.Offset(1, 0).Select
Loop

Finish:
MsgBox ("Done")
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,566
Messages
6,120,266
Members
448,953
Latest member
Dutchie_1

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