VBA Clear Cells for one range if cell in other range is blank

DoodlesMama

New Member
Joined
Aug 26, 2012
Messages
14
Hi Everyone,

Currently using Office 2010, Windows 7

I'm looking for code that will clear only the cells in named range [TOT_TIME1] if the cells (same row) in named range [CAT1] are empty.

I've tried the code below, but keep getting a "Type Mismatch" error.
If [CAT1].Value = "" Then
[TOT_TIME1].ClearContents
End If
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Maybe this?

Sub Test()
x = [Category1].Rows.Count
For i = 1 To x
If [Category1].Cells(i, 1).Value = "" Then
[TotTime1].Cells(i, 1).ClearContents
End If
Next i
End Sub
 
Upvote 0
If the row range for both named ranges is the same, then this should work...
Code:
Sub MatchBlanksInTOT_TIME1toBlanksInCAT()
  On Error Resume Next
  Intersect(Range("CAT1").SpecialCells(xlBlanks).EntireRow, Range("TOT_TIME1")).Clear
  On Error GoTo 0
End Sub
 
Upvote 0
If the row range for both named ranges is the same, then this should work...
Code:
Sub MatchBlanksInTOT_TIME1toBlanksInCAT()
  On Error Resume Next
  Intersect(Range("CAT1").SpecialCells(xlBlanks).EntireRow, Range("TOT_TIME1")).Clear
  On Error GoTo 0
End Sub
Another, probably better, way (a one-liner, by the way)...
Code:
Sub MatchBlanksInTOT_TIME1toBlanksInCAT()
  Range("TOT_TIME1") = Evaluate("IF(LEN(" & Range("CAT1").Address & ")," & Range("TOT_TIME1").Address & ","""")")
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,229
Messages
6,170,881
Members
452,364
Latest member
springate

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