Delete Non-Contiguous Rows, Excel VBA

mkarizat

New Member
Joined
Jun 30, 2015
Messages
5
I have a spreadsheet that users can only modify through userforms. One userform is meant to allow users to select entire rows of the spreadsheet and delete the data. My current code works when one row is selected or when one contiguous block of rows is selected, but fails when several non consecutive rows are selected. The code I'm having trouble with is the following:

Range(DataExport.Source.Text).Delete

And I get the following error:

Delete method of Range class failed

To be clear, something like "Master!$22:$22,Master!$23:$23" would fail, but "Master!$22:$23" would work.
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi

I just tried it and it worked OK.

Code:
Sub Test()
Dim s As String

s = "Master!$22:$23"
Range(s).Delete

s = "Master!$22:$22,Master!$24:$24"
Range(s).Delete

End Sub
 
Upvote 0
Solution:


Text = Split(DataExport.Source.Text, ",")
For i = UBound(Text) To i = 1 Step -1
Range(Text(i)).Delete
Next i
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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