Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete specific columns with VBA?

Tags:

excel

vba

I am trying to delete multiple columns in VBA for Excel. I downloaded data from the Illinois Statistical analysis center about drug arrest. http://www.icjia.org/public/sac/index.cfm?metasection=forms&metapage=rawMetadata&k=170

Each of the columns I want to delete are 3 columns apart from each other.

For example:

Adams County Illinois Champaign County Illinois Estimate |percent | Percent Margin of Error |Estimate Margin| Estimate| Percent | Percent Margin of Error

D|E|F|G|H|I|J

I just want to delete all the columns the say Percent Margin of Error

Here is my macro:

Sub deleteCol()
    Columns("H,J").Delete
End Sub

I keep getting an error:

Run-time 13: type mismatch

Any suggestions?

like image 835
Zaynaib Giwa Avatar asked Oct 27 '14 01:10

Zaynaib Giwa


People also ask

How do I delete multiple columns in VBA?

Explained VBA Code to Delete Multiple Columns: 'Specifying the Columns to delete and Deleting the Columns using EntireColumn. Delete method. Ending the sub procedure to delete entire Column. Here Columns(“A:C”) is to tell excel to delete Columns from A to C of the worksheet.

How do you delete specific columns?

Right-click in a table cell, row, or column you want to delete. On the Mini toolbar, click Delete. Choose Delete Cells, Delete Columns, or Delete Rows.


1 Answers

You were just missing the second half of the column statement telling it to remove the entire column, since most normal Ranges start with a Column Letter, it was looking for a number and didn't get one. The ":" gets the whole column, or row.

I think what you were looking for in your Range was this:

Range("C:C,F:F,I:I,L:L,O:O,R:R").Delete

Just change the column letters to match your needs.

like image 52
peege Avatar answered Oct 18 '22 11:10

peege