ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Delete columns (https://www.excelbanter.com/excel-programming/292890-delete-columns.html)

Elsie

Delete columns
 
Hi, I am trying to get my macro to delete a particular column when the first
row of that column contains a certain keyword, however I am only able to
delete the first row of that column. In addition, how come " Workbooks.Open
Filename:=fi" keeps giving me error 1004 - runtime error that says it can't
find the file?

Sub redundantCols()

Dim fi As String, sh As String
Dim c As String
Dim i As Integer, j As Integer, k As Integer
fi = InputBox("Enter the name of file", "Source file")
sh = InputBox("Enter the name of the sheet", "Source sheet")
fi = fi & ".xls"

Workbooks.Open Filename:=fi
Windows(fi).Activate
Sheets(sh).Select

With Workbooks(fi).Worksheets(sh)
.Cells(1, 9) = "Item Description"
i = 1
For j = 1 To 16
c = Cells(i, j)

If c = "Description" Or c = "Item" Then
.Cells(i, j) = ""
End If

Next j

End With

End Sub



RWN

Delete columns
 
Regarding the second question (err 1004);
You have to supply the path as well as the name.

--
Regards;
Rob
------------------------------------------------------------------------
"Elsie" wrote in message
...
Hi, I am trying to get my macro to delete a particular column when the

first
row of that column contains a certain keyword, however I am only able

to
delete the first row of that column. In addition, how come "

Workbooks.Open
Filename:=fi" keeps giving me error 1004 - runtime error that says it

can't
find the file?

Sub redundantCols()

Dim fi As String, sh As String
Dim c As String
Dim i As Integer, j As Integer, k As Integer
fi = InputBox("Enter the name of file", "Source file")
sh = InputBox("Enter the name of the sheet", "Source sheet")
fi = fi & ".xls"

Workbooks.Open Filename:=fi
Windows(fi).Activate
Sheets(sh).Select

With Workbooks(fi).Worksheets(sh)
.Cells(1, 9) = "Item Description"
i = 1
For j = 1 To 16
c = Cells(i, j)

If c = "Description" Or c = "Item" Then
.Cells(i, j) = ""
End If

Next j

End With

End Sub






All times are GMT +1. The time now is 05:43 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com