clearing a cell's contents based on another cells content

cbye

New Member
Joined
Feb 2, 2022
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I'm trying to clear duplicate data out of cells. I have a VBA to clear columns A and C and keep the first instance. But because column B might be the same throughout the report, it will clear out what I want to keep. The only way I see to do it is to look at the same cell in column D and if it has "System Firmware" it should be kept. All other instances be cleared. So in my example I would like to keep B5 and B14 because they are the first instance. All others would be cleared. All of my reports will be different. Some may have 1 hostname. Others may have 5 different hostnames. I also posted the code I use to clear data out of columns A and C.


PERSONAL.XLSB
ABCDEFGHIJK
1
2
3
4HostnameMachine Typeserial number componentmachine datamachine datamachine datamachine datamachine datamachine datamachine data
5computer 112345678901System Firmware data data data data data data data
6computer 112345678901ent0 data data data data data data data
7computer 112345678901ent1 data data data data data data data
8computer 112345678901ent2 data data data data data data data
9computer 112345678901pdisk0 data data data data data data data
10computer 112345678901pdisk1 data data data data data data data
11computer 112345678901pdisk2 data data data data data data data
12computer 112345678901tape0 data data data data data data data
13computer 112345678901tape1 data data data data data data data
14computer 21234abc1234System Firmware data data data data data data data
15computer 21234abc1234ent0 data data data data data data data
16computer 21234abc1234ent1 data data data data data data data
17computer 21234abc1234ent2 data data data data data data data
18computer 21234abc1234pdisk0 data data data data data data data
19computer 21234abc1234pdisk1 data data data data data data data
20computer 21234abc1234pdisk2 data data data data data data data
21computer 21234abc1234tape0 data data data data data data data
22computer 21234abc1234tape1 data data data data data data data
Sheet








Sub RemoveRepeatingStringsA()

Dim BaseStr As String, CurrStr As String
Dim EndRow As Long

EndRow = Range("A" & Rows.Count).End(xlUp).Row
BaseStr = Range("A1").Value

Application.ScreenUpdating = False

For Iter = 2 To EndRow
CurrStr = Range("A" & Iter).Value
If CurrStr = BaseStr Then
Range("A" & Iter).Value = vbNullString
Else
BaseStr = Range("A" & Iter).Value
End If
Next Iter

Application.ScreenUpdating = True

RemoveRepeatingStringsB

End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
If I'm not misunderstanding you, the only Rows you want to keep would be 5 & 14 and delete the rest? If that's the case, there's several ways you could do it, here's just 2.

VBA Code:
Sub ClearDupes1()
    Dim ws As Worksheet, lr As Long, lc As Long, rng As Range
    Set ws = Worksheets("Sheet1")
    lr = ws.Cells.Find("*", , xlFormulas, , 1, 2).Row
    lc = ws.Cells.Find("*", , xlFormulas, , 2, 2).Column
    
    Set rng = ws.Range(Cells(4, 1), ws.Cells(lr, lc))
    
    With rng
        .RemoveDuplicates Columns:=Array(1, 3), Header:=1
    End With
End Sub

VBA Code:
Sub ClearDupes2()
    Dim ws As Worksheet, lr As Long, lc As Long, rng As Range
    Set ws = Worksheets("Sheet1")
    lr = ws.Cells.Find("*", , xlFormulas, , 1, 2).Row
    lc = ws.Cells.Find("*", , xlFormulas, , 2, 2).Column
    
    Set rng = ws.Range(Cells(4, 1), ws.Cells(lr, lc))
    
    With rng
        .AutoFilter 4, "<>System Firmware"
        .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        .AutoFilter
    End With
End Sub
 
Upvote 0
@cbye, I am reading the requirement a little differently to @kevin9999, posting what you want the finished product to look like would have been helpful.
My interpretation is that you do now want to delete rows but want to have column A,B,C to look more like it would when you do a pivot table.
With repeating labels only appearing on the first occurrence. (slight variation for column B)

Personally I don't think it is a good idea because you currently have a database style layout which is ideal for Pivot tables, filtering, sorting and querying.

It doesn't sound you want to rely on the 1st of component being System Firmware. What fields make it a first occurrence ?
Is the first occurrence when either columns A or C change ? Although if C always changes when A changes you could rely on just C.
Are there other fields that you want to use to decide whether it is the first occurrence ?

I am wondering if your issue is just the order in which are you doing it.
You are calling delete B from the delete A routine.
Since delete B needs columns A & C to be intact, you need to run the delete B routine first then call A & C.

VBA Code:
Sub RemoveRepeatingStrings_Control()
    Call RemoveRepeatingStringsB    'B has dependancies on A & C so do this first
    Call RemoveRepeatingStringsA
    Call RemoveRepeatingStringsC
End Sub

Sub RemoveRepeatingStringsB()

    Dim BaseStr As String, CurrStr As String
    Dim EndRow As Long, Iter As Long
    
    EndRow = Range("A" & Rows.Count).End(xlUp).Row
    BaseStr = Range("A1").Value
    
    Application.ScreenUpdating = False
    
    For Iter = 2 To EndRow
        CurrStr = Range("A" & Iter).Value & Range("C" & Iter).Value         'Criteria - change of A & C
        If CurrStr = BaseStr Then
            Range("B" & Iter).Value = vbNullString                          'Blank out B based on change in column A & C
        Else
            BaseStr = Range("A" & Iter).Value & Range("C" & Iter).Value     'Criteria - change of A & C
        End If
    Next Iter
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Solution
Hi, thanks for both of your replies. Sorry for not sending my end result Im looking for. In the vba that I posted previous, this works well for columns A and C because in my reports the different machines will always have a different hostname and serial number. But in column B, it may have the same machine type. Because of this, the code that works for A and C will clear out the machine type cell. Also these reports I get will always be different in the fact that I may only have one hostname or I may have 10 different hostnames. But the machine type could be the same. Thats why I wanted to use the cell in column D, "System Firmware" because that shows me the start of the new machine. Hopefully with my finished product this helps with the explanation?



PERSONAL.XLSB
H
28
Sheet
 
Upvote 0
PERSONAL.XLSB
ABCDEFGHIJK
1
2
3
4HostnameMachine Typeserial number componentmachine datamachine datamachine datamachine datamachine datamachine datamachine data
5computer 112345678901System Firmware data data data data data data data
6ent0 data data data data data data data
7ent1 data data data data data data data
8ent2 data data data data data data data
9pdisk0 data data data data data data data
10pdisk1 data data data data data data data
11pdisk2 data data data data data data data
12tape0 data data data data data data data
13tape1 data data data data data data data
14computer 21234abc1234System Firmware data data data data data data data
15ent0 data data data data data data data
16ent1 data data data data data data data
17ent2 data data data data data data data
18pdisk0 data data data data data data data
19pdisk1 data data data data data data data
20pdisk2 data data data data data data data
21tape0 data data data data data data data
22tape1 data data data data data data data
23
Sheet
 
Upvote 0
To add, in my example each machine has 9 rows of info. But this is not always the case. Some may only have 3 rows of info and some may have 15 rows of info. Im trying to create a vba that will cater to any circumstances.
 
Last edited:
Upvote 0
Will you ever have a new System Server line with both the same Hostname AND serial number as the previous System Server line ?

If the answer is no, my code should have worked. What happened when you tried it ?
 
Upvote 0
Alex your code works great! thanks. I have tried it on several reports and the works flawlessly. Sorry I thought that I didn't explain it properly initally. Thanks much.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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