By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,280 Members | 2,193 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,280 IT Pros & Developers. It's quick & easy.

Excel: Check for borders of cells in all workbooks (vb.net)

P: n/a
Hi,

For a project at school we have to automate the assignment of seats in
classrooms to students during the exams.
The lady who previously did everything manually kept the layouts of the
classrooms in an excel-document: 1 sheet per classroom, and she marked
the cells which represent the seats with a border.
The problem however is that she sometimes merged cells together to get a
better layout.

Problems with current code:
-Very, very slow: vb takes 30 minutes to read 63 classrooms (the check
for the border is the bottleneck)
-If there is a merged area (ie. A1:A4) with a border, then A1, A2, A3
and A4 will be flagged as a seat when it actually just has to be A1
"cell" is a custom helper object to make traveling the cells easier: I
can set it to a row and column, it gives a range back as a string, and I
can move it to the left, right, up, down, etc...

My current code (checking the borders inside a sheet):
(I hope the layout remains somewhat readable)

'Read per row, a maximum of 47 rows
For row As Integer = 1 To 47
'read per column
'numCols= worksheet.UsedRange.Columns.Count
For col As Integer = 1 To numCols
'Check border of cells
Dim borders As Excel.Borders _
= CType(worksheet.Range(cell.getRange), _

Excel.Range).MergeArea.Borders
'borders.LineStyle = 1 gives error at a mergearea
If borders.Item(Excel.XlBordersIndex.xlEdgeBottom).Li neStyle = 1 _
And borders.Item(Excel.XlBordersIndex.xlEdgeLeft).Line Style = 1 _
And borders.Item(Excel.XlBordersIndex.xlEdgeRight).Lin eStyle = 1 _
And borders.Item(Excel.XlBordersIndex.xlEdgeTop).LineS tyle = 1 _
Then
'Current cell is a seat
End If 'Border = 1
cell.goRight()
Next 'Read per column
cell.setCol("A")
cell.goDown()
Next 'Read per row
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Small correction of comment in code:
'borders.LineStyle = 1 gives error at a mergearea
If borders.Item(Excel.XlBordersIndex.xlEdgeBottom).Li neStyle = 1 _
And borders.Item(Excel.XlBordersIndex.xlEdgeLeft).Line Style = 1 _
And borders.Item(Excel.XlBordersIndex.xlEdgeRight).Lin eStyle = 1 _
And borders.Item(Excel.XlBordersIndex.xlEdgeTop).LineS tyle = 1 _
Then
* 'Borders.Linestyle = 1' does NOT give an error, but gives false when
it checks a mergeArea. But it is however more than twice as fast than
checking each border seperately (is there a way to drop further checks
in an "AND" when the first one is false? Or can this only be done
through nested if's?).
* Checking each border separately returns each cell inside a mergeArea
with a border as a seat (this would be because of my own "Cell" object)
Glen Vermeylen wrote: Hi,

For a project at school we have to automate the assignment of seats in
classrooms to students during the exams.
The lady who previously did everything manually kept the layouts of the
classrooms in an excel-document: 1 sheet per classroom, and she marked
the cells which represent the seats with a border.
The problem however is that she sometimes merged cells together to get a
better layout.

Problems with current code:
-Very, very slow: vb takes 30 minutes to read 63 classrooms (the check
for the border is the bottleneck)
-If there is a merged area (ie. A1:A4) with a border, then A1, A2, A3
and A4 will be flagged as a seat when it actually just has to be A1
"cell" is a custom helper object to make traveling the cells easier: I
can set it to a row and column, it gives a range back as a string, and I
can move it to the left, right, up, down, etc...

My current code (checking the borders inside a sheet):
(I hope the layout remains somewhat readable)

'Read per row, a maximum of 47 rows
For row As Integer = 1 To 47
'read per column
'numCols= worksheet.UsedRange.Columns.Count
For col As Integer = 1 To numCols
'Check border of cells
Dim borders As Excel.Borders _
= CType(worksheet.Range(cell.getRange), _

Excel.Range).MergeArea.Borders
'borders.LineStyle = 1 gives error at a mergearea
If borders.Item(Excel.XlBordersIndex.xlEdgeBottom).Li neStyle = 1 _
And borders.Item(Excel.XlBordersIndex.xlEdgeLeft).Line Style = 1 _
And borders.Item(Excel.XlBordersIndex.xlEdgeRight).Lin eStyle = 1 _
And borders.Item(Excel.XlBordersIndex.xlEdgeTop).LineS tyle = 1 _
Then
'Current cell is a seat
End If 'Border = 1
cell.goRight()
Next 'Read per column
cell.setCol("A")
cell.goDown()
Next 'Read per row

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.