Clear Dynamic Range

ionelz

Active Member
Joined
Jan 14, 2018
Messages
264
Office Version
  1. 365
Platform
  1. Windows
Hello,
I would like a simple code for Clear Content of Range
Dynamic Value is in cell C3
Dynamic Columns are F,G,H,I,J,K,L,M,N,O,P,Q so 12 Columns
If C3=3 then Clear Range I4:Q6
If C3=10 then Clear Range P4:Q6
If C3=6 then Clear Range L4:Q6
Basic, Clear Content of end range
Thank you
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi ionelz,

you can try this code

VBA Code:
Sub Test()

    Dim Rif As Long
    Dim rRng As Range
 
    ' check the contents of cell "C3"
    With Range("C3")
        If Len(.Text) Then
            If IsNumeric(.Value) Then
                Rif = .Value
            End If
        End If
    End With
 
    If Rif = 3 Then
        Set rRng = Range("I4:Q6")
    ElseIf Rif = 10 Then
        Set rRng = Range("P4:Q6")
    ElseIf Rif = 6 Then
        Set rRng = Range("L4:Q6")
    End If

    If Not rRng Is Nothing Then
        rRng.ClearContents
    End If

End Sub

Let me know if this is what you need.
 
Last edited:
Upvote 0
sorry, no, I just give some example
I need to offset any C3 value end clear
Whole range is F4:Q6
So if like C3=3 then skip F,G,H and clear I4:Q6 or if example C3=5 then skip F,G,H,I,J and clear K4:Q6
 
Upvote 0
Hi ionelz,

Before proceeding, can you provide me with the following information:
  1. Values to delete ranges, can I also read them from cells other than "C3"?
  2. If the cell content (e.g. "C3") is 0: should the entire range from F4:Q6 be deleted?
Thanks in advance
 
Upvote 0
sorry, no, I just give some example
I need to offset any C3 value end clear
Whole range is F4:Q6
So if like C3=3 then skip F,G,H and clear I4:Q6 or if example C3=5 then skip F,G,H,I,J and clear K4:Q6
So if C3 = 12 then skip everything ??
 
Upvote 0
Try:
VBA Code:
Sub ClearRange()
    Range("F4").Offset(0, Range("C3").Value).Resize(3, 12 - Range("C3").Value).ClearContents
End Sub
 
Upvote 0
Hi ionelz,

let me know if the code below meets your requirements

VBA Code:
Sub Test1()

    Dim Rif As Long
    
    ' check the contents of cell "C3"
    Rif = -1
    With Range("C3")
        If Len(.Text) Then
            If IsNumeric(.Value) Then
                Rif = .Value
            End If
        End If
    End With
    If Rif > -1 And Rif < 11 Then
        Range(Cells(4, 6 + Rif), Cells(6, 17)).ClearContents
    End If

End Sub

Bye
 
Upvote 0
I have copy the code in my Sheet but when I change at C3 it doesn't update
Saved file as xlsb but still code do not work
 
Upvote 0

Forum statistics

Threads
1,225,349
Messages
6,184,437
Members
453,233
Latest member
bgmb

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