Clear contents of cells based on the values of adjacent cells

davefaust78

New Member
Joined
Sep 11, 2017
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am writing a VBA code to automatically clean up some excel reports I run on a regular basis and I have a situation where I am stuck.

Column J of my data contains Country Names and column K of my data has State Names.
Here's what I need:
If the values in column J are not equal to either United States or Canada, then clear the states value name in the adjacent cell.

For example, Here's how my data currently looks:
CanadaBC
United StatesTX
BrazilRio de Janeiro
United StatesNY
CanadaSK
ChileSantiago
BrazilGoias
ItalyRoma
United StatesCA
United KingdomSurry

<tbody>
</tbody>


















For each of the countries that are not equal to United States or Canada, I need the state values cleared.

It's driving me crazy as I'm relatively new to VBA. Any help that anyone can offer would be a huge lifesaver.
I'd like to run this as it's own sub.

Thank you.
Regards,
Dave
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Welcome to the Board!

Here is one way:
Code:
Sub MyCleanData()

    Dim lastRow As Long
    Dim myRow As Long
    
    Application.ScreenUpdating = False
   
'   Find last row with data in column K
    lastRow = Cells(Rows.Count, "K").End(xlUp).Row
    
'   Loop through all rows, starting on row 2
    For myRow = 2 To lastRow
        If Cells(myRow, "J") <> "United States" And Cells(myRow, "J") <> "Canada" Then
            Cells(myRow, "K").ClearContents
        End If
    Next myRow
    
    Application.ScreenUpdating = True

End Sub
 
Last edited:
Upvote 0
Hi & welcome to MrExcel
Try
Code:
Sub DelStates()
' davefaust78

    Dim Cl As Range
    
    For Each Cl In Range("J1", Range("J" & Rows.Count).End(xlUp))
        If Not Cl.Value = "Canada" And Not Cl.Value = "United States" Then
            Cl.Offset(, 1).ClearContents
        End If
    Next Cl
    
End Sub
 
Upvote 0
Hi Joe4,

Thank you so much for your quick reply. This is exactly what I needed and it works perfectly!

Regards,
Dave
 
Upvote 0
Here is another macro for you to try...
Code:
[table="width: 500"]
[tr]
	[td]Sub ClearNonCanadaNonUSstates()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "J").End(xlUp).Row
  Range("K1:K" & LastRow) = Evaluate(Replace("=IF((J1:J#=""United States"")+(J1:J#=""Canada""),K1:K#,"""")", "#", LastRow))
End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,843
Members
449,051
Latest member
excelquestion515

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