Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA code for searching Column from first row and formating the column

Status
Not open for further replies.

rajltd

IS-IT--Management
Sep 25, 2003
38
GB
I am coding a Excel macro in VB for searching a particular column (treat as header) from a number of worksheets in the workbook. That particular column Eg. Accessories, can be in more than one worksheet of the workbook, but it is always in the 3rd column of that particular worksheet (if there) of the workbook. The reason being I want to format the column size from 10 to 50 for that particular column.

Ans:

for each ws in worksheets
with ws.cells(1,3)
if .value = "Accessories" then
.entirecolumn.columnwidth=70
end if
end with
next

I received the above code from the forum.

Can I do the same thing by searching any cell (Not just Accessories in 3rd column, but say accessories in 5th column and other attributes in other column and different worksheets) from the first row.

Any help is very much appreciated.

Thanks
 
Hi,
Code:
for each ws in worksheets
  with ws.usedrange 
    for each h in range(ws.cells(1,1), cells(1, .columns.count + .column - 1))
      if h.value = "Accessories" then
        h.entirecolumn.columnwidth=70
      end if
    next 
  end with
next
:)

Skip,
Skip@TheOfficeExperts.com
 
oops...
Code:
for each ws in worksheets
  with ws.usedrange 
    for each h in range(ws.cells(1,1), ws.cells(1, .columns.count + .column - 1))
      if h.value = "Accessories" then
        h.entirecolumn.columnwidth=70
      end if
    next 
  end with
next


Skip,
Skip@TheOfficeExperts.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top