Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I Have to insert 2 empty column between two specified column in Excel and need to do calculation using C#.Could you please guide me how to do this.

Thanks
Posted

1 solution

The actual task of inserting a new column is fairly trivial. Unfortunately, it's near impossible to ascertain what will be considered an acceptable (useful) answer.

What have you tried and what was the result?

Using the macro-recorder in Excel, we can see that if we right-click on column C then select "Insert", we get the following VBA code:

VB
Sub Macro1()
'
' Macro1 Macro
'
    Columns("C:C").Select
    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub


Searching the Help for "Columns", we find it to be a member of the Worksheet object. The result of a call to the Columns method is a Range.
This range is first specified
Then is selected. Once done, the Insert method is called on the range.

Looking at the Excel help for Range.Insert, we see that it takes two (optional) parameters - Shift and CopyOrigin (both VARIANTs)

We're lazy, so we'll see if it produces the right result without either of the two params. Back to the VBA editor, remove the two params and run the macro. Beautifull! Just the same result (in this case) as it was when we recorded it earlier. So, we'll ditch the params for now (may have to add them again later for situations not imagained yet)

So, armed with this knowledge we're ready to put it into a standalone app.

I don't use C#, so can't guide you on specifics - but this is the process when used from C++ in gcc. Since you're using C#, it will be much simpler and cleaner since you'll be able to Add a Reference to the MS Office COM object (I forget it's name) and just use the objects themselves, without all of the nasty IDispatch gobble-d-gook.


C++
// Initialize COM for this thread...
   CoInitialize(NULL);

   // Get CLSID for our server...
   CLSID clsid;
   HRESULT hr = CLSIDFromProgID(L"Excel.Application", &clsid);

   if(FAILED(hr)) {

      MessageBox(NULL, L"CLSIDFromProgID() failed", L"Error", 0x10010);
      return -1;
   }

   // Start server and get IDispatch...
   IDispatch *pXlApp;
   hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pXlApp);
   if(FAILED(hr)) {
      MessageBox(NULL, L"Excel not registered properly", L"Error", 0x10010);
      return -2;
   }

// ommitted code
// Add code here to make the application visible

// ommited code
// Add code here to either 
//  (a) load a pre-existing file 
// OR
//  (b) get the workbooks object and call the "Add" method on it

    {
        VARIANT result, var1;
        wstring rangeStr;
        IDispatch *pRange;

        // Specify a range that selects a single column
        VariantInit(&result);
        rangeStr = L"C:C";
        var1.bstrVal = SysAllocString(rangeStr.c_str());
        var1.vt = VT_BSTR;
        AutoWrap(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Range", 1, var1);
        pRange = result.pdispVal;

        // select this range
        AutoWrap(DISPATCH_METHOD, NULL, pRange, L"Select", 0);

        // insert a new column
        AutoWrap(DISPATCH_METHOD, NULL, pRange, L"Insert", 0);

        // done with the range. let excel know
        pRange->Release();
    }
 
Share this answer
 
Comments
Member 12483338 11-Jul-16 2:39am    
How to add row like column
enhzflep 11-Jul-16 3:51am    
Exactly the same way. Instead of recording a macro where you add a column, record the addition of a row. The range is different, as is the shift and
copyorigin.
Here's the VBA to add a row.


Sub Macro1()
'
' Macro1 Macro
'
Rows("3:3").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
Member 12483338 11-Jul-16 5:21am    
Please help me with c++ code. like column mention below.
Need to add below or above row number.
{
VARIANT result, var1;
wstring rangeStr;
IDispatch *pRange;

// Specify a range that selects a single column
VariantInit(&result);
rangeStr = L"C:C";
var1.bstrVal = SysAllocString(rangeStr.c_str());
var1.vt = VT_BSTR;
AutoWrap(DISPATCH_PROPERTYGET, &result, pXlSheet, L"Range", 1, var1);
pRange = result.pdispVal;

// select this range
AutoWrap(DISPATCH_METHOD, NULL, pRange, L"Select", 0);

// insert a new column
AutoWrap(DISPATCH_METHOD, NULL, pRange, L"Insert", 0);

// done with the range. let excel know
pRange->Release();
}
enhzflep 12-Jul-16 1:34am    
I dont know what your problem is. If you'd like me to try to help, you'll have to stop expecting me to guess. Please state what you have tried, what happened and why you're stuck.
Failing that, you'll need to look elsewhere. All you've done is said please help me and re-posted my code (skipping important code - it's all necessary). This is useless - it provides no clue as to what your problem is.

Information available allowed me to write this code. The information you've given allows me to do basically nothing. Please be reasonable and sensible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900