Originally posted by TheUnheardHuman
bassicly im making a stats keeping program for a voleyball team at a gym which the guy wants to be made in vbasic so he can later edit anything he might need to. each stat of each player like how many serves is held in a varibale and how many succfull serves in another. and so on for each player (theres more stats then that to keep) and i want to have a button to use all the numbers form the variables to be in a excel table. well thats what he wants to happen. is this possible?
Its not too difficult...you need to include the library for your version of Excel in the reference settings...then do something like
Code:
Private Sub Form_Load()
On Error GoTo err_handler
Dim oApp As Excel.Application
Dim oWkBk As Excel.Worksheet
Set oApp = New Excel.Application 'Open Excel
oApp.Visible = True 'Make it visible
oApp.Workbooks.Add 'Add a new sheet
Set oWkBk = oApp.ActiveSheet 'Create reference to new sheet
oWkBk.Range("A1").Value = "Forename" 'Set column names
oWkBk.Range("B1").Value = "Score"
oWkBk.Range("A2").Value = "Dave" 'Set Data
oWkBk.Range("B2").Value = 40
oWkBk.Range("A3").Value = "Andrew"
oWkBk.Range("B3").Value = 35
oWkBk.Range("A4").Value = "Simon"
oWkBk.Range("B4").Value = 29
Set oWkBk = Nothing
Set oApp = Nothing
Exit Sub
err_handler: 'if error
MsgBox Err.Description
End Sub
Or even with VC++
Code:
#pragma warning (disable:4146)
//I am assuming you have a setting under Tools->Options->Directories
//For "Program Files\Common Files\Microsoft Shared"
//And the Office folder (wherever it is)
#import "Office10\mso.dll"
#import "VBA\VBA6\VBE6EXT.olb"
#import "excel.exe" \
rename("DialogBox", "ExDialogBox") rename("RGB", "ExRGB")
#include <windows.h>
#include <iostream>
struct COMINIT{//Start and end COM
COMINIT(){CoInitialize(0);}
~COMINIT(){CoUninitialize();}
}CI;
inline std::ostream& operator<<(std::ostream& out,
const _bstr_t& str){//Just to stop having to cast to Char*
out << static_cast<char*>(str);
return out;
}
int main(){
try{
Excel::_ApplicationPtr lpApp("Excel.Application");
Excel::_WorksheetPtr lpWkst = 0;
lpApp->PutVisible(0,TRUE);
lpApp->Workbooks->Add();
lpWkst = lpApp->ActiveSheet;
lpWkst->Range["A1"]->Value2 = "Forename";
lpWkst->Range["B1"]->Value2 = "Score";
lpWkst->Range["A2"]->Value2 = "Dave";
lpWkst->Range["B2"]->Value2 = 40L;
lpWkst->Range["A3"]->Value2 = "Andrew";
lpWkst->Range["B3"]->Value2 = 35L;
lpWkst->Range["A4"]->Value2 = "Simon";
lpWkst->Range["B4"]->Value2 = 29L;
lpWkst = 0;
lpApp = 0;
}
catch(_com_error& e){
std::cout << "Error ";
std::cout << (e.Description().length() ? e.Description() : "");
return 1;
}
return 0;
}
(Both Assume ExcelXP)