Aspose.Cells - Set Border For Range - vb.net

I'm currently working with Aspose.Cells. I've largely managed to do what I need to do however one issue remains; I can't see an easy way to apply a border to a defined range. I can for a cell, however when applying the style to the range all cells in the range are formatted.
The only way I can see around this is to write a method to enumerate all cells within the range to determine the right border attributes. This seems a slightly hideous way to achieve what I see as a simple task, so am hoping there is a better way!
Is there?

Ok let's start.
Assuming we want to add borders to a range from A2 cell to H6 cell range.
Always remember that cell index in Aspose is Zero based.
So the A2 cell has index of row 1 and column 0.
Define the range
Dim range = current_worksheet.Cells.CreateRange(1, 0, 5, 8)
Set the borders
range.SetOutlineBorder(Aspose.Cells.BorderType.TopBorder,Aspose.Cells.CellBorderType.Thick,Drawing.Color.Blue)
range.SetOutlineBorder(Aspose.Cells.BorderType.BottomBorder,Aspose.Cells.CellBorderType.Thick, Drawing.Color.Blue)
range.SetOutlineBorder(Aspose.Cells.BorderType.LeftBorder,Aspose.Cells.CellBorderType.Thick, Drawing.Color.Blue)
range.SetOutlineBorder(Aspose.Cells.BorderType.RightBorder,Aspose.Cells.CellBorderType.Thick, Drawing.Color.Blue)
That's it!

Related

Loop and Add Range in a place holder

Hello,
First of all, thanks for everyone who's helped me with my previous concern. I have another loop issue though. What I'm trying to do is store a certain range on a place holder and use for Charts. My code loops through column A to check cells that contain Product/URL and if found, will offset to the next column and will store the range highlighted on a place holder.
What I was able to do so far is to just select the cell with content in-line with cell with Product/URL. Here's my code so far.
Dim ProdCell as Range
For Each ProdCell in [A:A].SpecialCells(xlCellTypeConstants)
If Not IsEmpty(Prodcell) and Prodcell.Cells.Value = [A1] Then
ProdCell.Offset(0,1).Cells.End(xlToRight).Select
End if
Next ProdCell
I believe that if I can find a way to highlight the range using VBA coding, I will be able to loop through it. Please help.

How to make dynamic cell in formula based on pointed value in excel spreadsheet

I have an issue with the cell range in formula and I don't know how to change it based on a predefined value in the spreadsheet. For example, from figure I have cells B8:B12=0 (5 cells), however, if I want to change range to range B10:B12=0 (3 cells) I should delete them from formula. How can I do the reference to a specific cell in a spreadsheet where I can simply change value 5 to 3 and it will change automatically, without interfering formula each time? I'm new to VBA, any help is appreciated.
As it was mentioned before, you should try offset function and do something like:
AND(SUM(OFFSET(D13,-1,-2,-(G6),1))=0). Then the cells in the range B8:B12 would be possible to change inserting range in cell G6.
Use the =INDIRECT function to define your target cells, e.g.
=TEXT(INDIRECT(A1), "")
If I entered the text B3 into cell A1, then this formula would return the text value in cell B3.
Let me know if it works for you.

How to keep named range formula from changing cell values with VBA

I noticed an interesting problem while trying to debug a VBA routine that sorts the list of worksheets in a range and then redraws the border around that range.
The range containing is defined in Name Manager with the formula as shown below
=Tables!$L$2:$L$22
The problem that is occurring is that while doing the sheet name work, sometimes cells are deleted and sometimes cells are inserted. This CHANGES the cell address values in the named range. So if I deleted two cells and inserted one cell, the formula changes to
=Tables!$L$2:$L$21
And if I happen to insert into the first cell (L2), then the formula changes to
=Tables!$L$3:$L$22
I'm certain that problem can be solved using the header range name and offsetting by one, but I'm not sure how to do that in the formula for a named range as I've tried numerous ways and can't get it right. But I also need the ending range address to be static.
Any help appreciated.
One thing to try is to use:
=Indirect("Tables!$L$2:$L$22")
rather than
=Tables!$L$2:$L$22
It is an acceptable Name, but I am not sure you will get the same functionality.
Found a better way than the comment using the static header range. You simply define the whole range using the offset function from the header.
=OFFSET(AllSheetsHeader,1,0,21,1)
Where the 1,0,21,1 are 1 row offset from header, 0 column offset, 21 is row height, and 1 is just that one column

Excel VBA how to select all cells in a dynamic range were the colour index is not 0

I have a SAP Report embedded in a worksheet, it is refreshed via a macro using variables defined in another worksheet. That all works fine, but i am having trouble selecting the data the report generates.
The headings of the report are in and always will fall in this range ("A17:K17"), but the results rows will vary making the total range I want to capture anywhere from ("A17:K18") to (A17:K1000").
The solutions I've already tried didn't work i think because there is almost no consistency in the result data, it's a mixture of text and numbers with empty cells all over the place, in both the rows and columns. Including the occasional completely empty row. This means the methods I have tried before reach a point where it thinks it's reached the end of the populated rows - but it hasn't.
The only factor that remains the same throughout the report is that the cells in the range I want to capture are all filled with a color as default and anything outside the range is unfilled.
To me the simplest solution would be to use VBA to select all the cells beneath and including the headers on ("A17:K17") where the color index is not 0 (blank?) regardless of their contents as I don't mind capturing empty cells. Except I don't know how to do this.
At this point I'd just like to select this range I haven't decided if I'm going to copy it into a new workbook or into an email yet, but that I can do. I've just hit a dead end selecting it.
Quite unsure exactly what it is you require but here's a solution. It's worth noting that both the ColorIndex and Color properties are not necessarily zero with no fill, so if you just change blankCell to a cell with the fill which you define to be blank you'll be good to go.
Sub test()
Set blankCell = Range("A1") ' change this to a cell that you define to be blank
blankIndex = blankCell.Interior.Color
Set cellsDesired = Range("A17:K17")
For Each cell In Range("A17:K1000")
If cell.Interior.Color <> blankIndex Then
Set cellsDesired = Application.Union(cellsDesired, Range(cell.Address))
End If
Next cell
cellsDesired.Select
End Sub

Format One Cell with ColorScale Based on Range of Cell Values

I need to format a single cell with a gradient color based on the values of a range of cells. Similar to the conditional formatting with a ColorScale, except only applying the formatting to a single cell based on the values of a range of cells.
Most likely this will need to be done in VBA. I can't seem to find a simple way to do this without rewriting a similar script to the built-in ColorScale methods.
Example:
B10 formatted with a color in a scale based upon the values of B10:B40. ColorScale would be type 3 with Low, Mid (50%), and High. But I don't want to format any cells other than B10, just use the values of the range B10:B40
I will then do the same for B11:B41, but only formatting B11, then B12:B42, B13:B43, etc..
All I need is to figure out how to create a ColorScale based on a range of values but only apply the formatting to one of the cells.
Edit:
If someone knows a way to create a color scale script similar to the conditional formatting one built into Excel, that would also be a good fix. If I can just calculate the scale from the values in the range of cells and apply to the one cell with the color grade, that would be a solution.
I think what you are after is a 3-color scale for B10 to B40 (so that B10 acquires a colour based on that cell's ranking in the range) together with a separate rule that applies white formatting (in appearance, no formatting) to B11:B40. The latter rule should be at the top of the list of rules and Stop if True checked.