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.
 

Some videos you may like

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,877
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
 

mkarizat

New Member
Joined
Jun 30, 2015
Messages
5
Solution:


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

Watch MrExcel Video

Forum statistics

Threads
1,122,228
Messages
5,594,931
Members
413,953
Latest member
Arthur1471

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
Top