I have an Excel workbook which has 3 worksheets with a named range defined for address A1:F10 in each sheet and have some values.
I wanted to have a range defined and set all the 3 worksheet range values to this.
Example
TempRange = Test1Range + Test2Range + Test3Range
Any help would be appreciated
IF all four ranges are the same size, then you can add 'em up with an array formula. Select the entire area of "TempRange", and type
=Test1Range + Test2Range + Test3Range
... and then, to enter it, hit CTRL+SHIFT+ENTER, not plain ENTER.
It's been a long time, and I use OpenOffice these days, so I might have details wrong. The general idea works, though, so even if this exact thing isn't right, googling for "Excel array formula" will probably turn up some helpful information.
I found the answer
Application.Union(Range1,Range2)
Related
How to read the value of range that is Merged with EPPlus?
Lets say the range "G15:G18" is merged. How do I retrieve the text inside that range?
I've tried this, but without success:
string txt = ws.Cells["G15:G18"].Value.ToString();
Thanks.
Looking better at the issue, I finally understood that what I was doing was actually bringing a collection of results, where only the first item has a value.
So, basically, this code:
string txt = ws.Cells["G15:G18"].Value.ToString();
would return an array like with the text for all the cells in the range.
But except for the first cell in the array, all cells are empty. Only the first cell hold the Value for the whole range.
What I did is as simple as this:
string val = ws.Cells["G15:G18"].First().Value.ToString();
It worked fine.
Unless I missed the boat, I think it might be even easier than you think... just look for the value for the first cell in the range:
string txt = ws.Cells["G15"].Value.ToString();
Also, if you know it's text or just want the text representation of the cell, you can use the Text property:
string txt = ws.Cells["G15"].Text;
I think this concept transcends EPPlus also -- you can reference it in Excel formulas, and I believe it works this way in Interop as well.
I want to copy an 1D array to a column range in excel. I'm using interop for this purpose.
I have already tried these things:
range.get_Resize(Ary.Length, 1).Value2 = Ary;
range.set_Value(Excel.XlRangeValueDataType.xlRangeValueDefault, Ary);
and as simple as range.Value = Ary;
I have tried using even range.value2, but these things copy the very first index value in the array to the entire range.
So say suppose, if there are 200 rows in the range and the array contains integers 101-300, than only 101 is copied throughout the range with the above tried methods.
Can anyone please help me with this? It would be more helpful if someone can explain me this strange behavior! Thanks in advance.
Note: I know I can do it through a for loop, but that takes time. I would surely like to know a method which takes less than a minute to iterate a million rows.
I seriously don't know what exactly is wrong with the above methods. But I found the solution:
Excel.Range range = sheetSource.UsedRange.Columns[columnIndx];
range.Value = application.WorksheetFunction.Transpose(Ary);
I hoped and guessed we could probably have something like this:
ws.Range("D").VerticalAlignment
Which appeared to be wrong a bit later.
Edit:
previously in question I mentioned:
ws.Range("A:F").VerticalAlignment
And given a feedback which mentioned that was correct.
As you might see, I'm going to select the whole columns of A to F, what could be the right way doing so?
Hi this confused me a bit when I needed to do it as well. I'll do my best to explain!
You can select single cells (still classed as a Range I believe) using
WorkSheet.Cells[row, column]
To select multiple Cells you could do something like this
Range startCell = excelSheet.Cells[1,1];
Range endCell = excelSheet.Cells[3,3];
Range myCellCollection = excelSheet.Range[startCell, endCell];
Hope that helps a bit!
Let me know if you want any more clarification, it may not be the most efficient way but it is still running in my application :)
As you have done is fine, but you haven't passed an argument to VerticalAlignment, assuming ws is set correctly too.
Range("D:D").VerticalAlignment = xlVAlignCenter
Range("A:F").VerticalAlignment = xlVAlignTop
As a few examples.
How about selecting A1:F1 then use
activecell.EntireColumn.select();
I have an ETL that's saving data to an Excel file. The issue is that the decimals are not being written out for integers. Example:
14.00
is being written out as
14
My code for writing out that line is
loWorksheet.Cells[liRowNum, 5] = lcAmount.ToString("0.00");
When I step through the code, it shows as 14.00, but on the Excel file it is not retaining the decimal places. Is this something that can be fixed in my code or is this an Excel issue? Any suggestions?
I'm quite sure you have to set format for your cells. I can't check right now, but it will be something like
xlYourRange.NumberFormat = "0.00";
You can check this question Set data type like number, text and date in excel column using Microsoft.Office.Interop.Excel in c#
If you really want the data to be displayed literally the way it is in the source file, you have to deal with trade-offs. The simplest way is to format the data as text. You can do this a cell at a time or for entire columns:
loWorksheet.Columns["A:E"].NumberFormat = "#";
The trade-off is it's just text at this point. You can't add, sum, average, whatever.
On the other hands, if your data looks like this:
4.0
4.00
4.000
You can't really keep it as numbers and expect to retain the original format without doing some funny business.
If it's consistently two decimal places, and you know it's going to be, then I agree with #RenatZamaletdinov's solution.
And you might want to consider other strings and what Excel might to do them
0000123 becomes 123
10/23 will probably render as a date, depending on your localization
12345678901234567890 will render as scientific notation probably
These are all avoided if you make the numeric format text (#), but again without knowing what you plan to do with the data, it's hard to say if this is the correct approach.
Wrap lcAmount.ToString("0.00");in a pair of quotes and put an equal sign in front of it. This will prevent excel from overriding the format.
loWorksheet.Cells[liRowNum, 5] = "=" + '"' lcAmount.ToString("0.00") + '"';
I am using Interop.Excel for doing this task.
I am trying to copy a range of cells from one workbook's sheet and paste it into a separate workbook's sheet, but I want to keep the formatting from the source sheet and paste it into the destination sheet. I found the PasteSpecial, and tried to use that, but when I try it, I get an unhelpful exception message : "PastSpecial methos of Range class failed", which has an HResult of -2146827284. Could not find anything via google, so here I am to hopefully get some help...
I use the code below to try this. I also did try using Excel.XlPasteType.xlPasteAll as the initial parameter to the PastSpecial method, and that did actually Paste the data into the destination sheet, and so I thought great, this works, but when I looked closer, I could see, very fiently, what looked like extra gridlines, and when I clicked on the pasted data, it was a whole object which could be moved around, much like an image pasted in that can be moved. This of course is not what I wanted. The idea is that I need to preserve the formatting and the border styles and the colors used.
Many thanks for reading - apologies if my formatting in the post has messed up.
Excel.Range rng6 = sht6.Cells.get_Range("A1", "O55");
Excel.Range rng1 = sht.get_Range("A" + rowStart6.ToString(), "O" + rowEnd6.ToString());
rng6.Copy(mis);
/** Fails here at PasteSpecial**/
rng1.PasteSpecial(Excel.XlPasteType.xlPasteFormats, Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);