I have some data that is displayed in a pivot table, from that pivot table I currently copy and paste them in another table with the corresponding current day, with so many operation numbers it takes about 1hr to complete this process daily, I'm trying to make a macro in which will help me in making this process in less time
This is the pivot table in which I copy the data
and here is where I paste it (in this table I use it to create some graphs)
Both tables consist of many different numbers of operation, as you can imagine this is very tedious.
Any help or ideas that can help me solve this is appreciated, I'm willing to try some other alternatives like fomulas, vba (preferred), even closedxml for c#.
I dont know if im explaining my self?
This is my logic.
Look for operation number
Copy Sum of "Sx"
Look in other sheet for the same operation number.
Paste value in corresponding day.
Look for same operation number with next "Sx"
.
.
End
Right now I'm just focusing on Operation1, since in Operation2 I use another sheet in which I do the same thing. (will probably use the same code as Operation1).
To clarify I'm not asking for someone to just give me the solution (although it is appreciated ), but any guide on this is helpful.
You do not need VBA to do this. Use GETPIVOTDATA.
Consider below replicated data:
In above sample, I copied some of your data in your screen shot, made a pivot out of it, put it in the same sheet and also put the table you need to populate below it. Then I use GETPIVOTDATA. I feel I need to show you how it is done even though it is well explained in the link I posted.
So we use this formula:
=GETPIVOTDATA("Sum of "&$I18,$I$2,"Date",J$17,"Opt",$I$16,"Id",$J$16)
in Cell J14. Take note that instead of using Day 1, Day2.. etc on your destination table, we used the actual date. Why? Because we need that in our GETPIVOTDATA Formula. Then copy the formula to the rest of the cells.
Result:
Now, GETPIVOTDATA errors out if it does not find anything which matches the criteria you supplied, so you might want to incorporate error handling using IFERROR statement.
Final Formula:
=IFERROR(GETPIVOTDATA("Sum of "&$I18,$I$2,"Date",J$17,"Opt",$I$16,"Id",$J$16),0)
Although you prefer VBA, I don't think a better approach than the built-in Excel functionality is more suitable in your case.
I am inserting data to Excel using C#. Whenever I add a new row to Excel using C# I want the same format as above row i.e, color, font and background color everything by programmatically.
It's an OLEDB insert.
Post insert, I want to apply the format of first row to the second row. With format painter from UI it's a straightforward job, I can't find a way to do the same with C#.
1) First you Need to get the Range you want to copy for e.g. RngToCopy
2) Then Set the Range where you want to insert.
3) use the below mentioned code snippet.
Range RngToCopy = ws.get_Range(StartCell, EndCell).EntireRow;
Range RngToInsert = ws.get_Range(StartCell, Type.Missing).EntireRow;
oRngToInsert.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown, oRngToCopy.Copy(Type.Missing));
//ws is the worksheet object, set StartCell and EndCell as per your requirement
I am using Microsoft Interop to convert excel files into csv files. I use sheet.SaveAs function.
My initial excel sheet has data from A1 to AZ columns for 100 rows.
I need in the CSV just the data from A1 to AP and only for 50 rows.
Using the Range function, I delete the row51-100, I clear the contents for the same rows, still when I save as CSV, I find rows 51-100 as below: (just commas). I do not want to see these commas in CSV.
,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,
The same for column AQ-AZ as well. I do not want these data in CSV. I delete, clear contents using Range function, yet these AQ-AZ columns appears in CSV files as “,,,,,,,,,,,,,,,,,,,,,” .
Is there a way to save XLS as CSV with only Range that I want to see in the CSV file. Is there a way to control the range that goes into CSV file?
In short, I want to see in CSV file just the data for column A1 to AP for 50 rows. No empty trailing “,”s. Is there a way?
The issue you are describing seems like a "Last Cell" issue. The last cell is the original end of your data, even after you delete rows/columns.
Here is what Microsoft has to say about it: How to reset the last cell in Excel
I seem to remember a programmatic way of doing this, but for the life of me, I cannot recall how.
Having looked at that info, maybe you could rethink how you can do this.
Perhaps you could just read the data you need and write it out yourself.
i.e. For each row in range, get the row as a value which will be an array of object,
convert to array of string, string.join with the delimiter as a comma and append
to a .csv file.
Clearing the contents as suggested in another answer did not work for me, what did work was copying the populated columns in a new worksheet and overwriting the old CSV.
Simply select the trailing empty columns in Excel, right click and select: clear contents. Then save.
I'm trying to get my program to dump out data into an Excel spreadsheet. At the moment, it simply puts the information into the proper cell calculating any calculated values as needed within my code. What I would really like to do is instead of writing out the calculated values is to write out the Excel formulas to the cells that contain calculated values. That way, if I give the Excel document to someone, they can change the data in it and the calculated values will update as expected.
The problem is that I don't know how to get the "A1" style coordinates which Excel would expect. Basically, I'm hoping that there's something like this available:
calculatedCell.Value = string.Format("=SUM({0})", range.Coordinates);
I know I can get the row and column index for the range, and using those I could evaluate the coordinates myself, I was just hoping there was a pre-packaged way of doing it.
There should be an Address property on Range.
Try this:
calculatedCell.Value = string.Format("=SUM({0})", range.get_Address(true, true,
XlReferenceStyle.xlA1,
false, null));
How does one delete a column (or multiple columns) in Excel?
eg. How to delete column C and shift the rest left?
Here is the solution to make it clearer (thanks to Leniel for the link)
Excel.Range range = (Excel.Range)sheet.get_Range("C1", Missing.Value);
range.EntireColumn.Delete(Missing.Value);
System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
This was the first result I hit and deleting a column in Excel doesn't need as much code as the current answers suggest. In fact (assuming you have a Worksheet object already, listed below as mySheet) all that is needed for the original question is:
mySheet.Columns["C"].Delete();
If you want to delete multiple columns then:
mySheet.Columns["C:D"].Delete();
You can specify a variable in the Delete method (see https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.xldeleteshiftdirection?view=excel-pia) i.e. mySheet.Columns["C"].Delete(xlShiftToLeft)but there's no need as the Delete method is smart enough to realise that the Range you are selecting is a single column, so will do this automatically.
You can also uses a numeric value to designate the column i.e. mySheet.Columns[2].Delete()
Here you find how to do it:
http://bytes.com/topic/c-sharp/answers/258110-how-do-you-delete-excel-column
http://quicktestprofessional.wordpress.com/2008/02/14/delete-columns-from-xl-sheet/