2008年8月23日星期六

VC++ - 使用沒有命名空間

#include<iostream>
using namespace std;

namespace  { 
int v1;  
float v2(float); 

float v2(float a) 

v1=(int)a*a;
return v1;



void main()

cout << "請輸入變數v1:";
cin>>v1;
cout << "變數v1的值為:" << v1 << endl;
cout << "v2的值為:" << v2(20.33) << endl;
cout << "v2之後,變數v1的值為:" << v1 << endl;

VC++ -  例外 try - catch

#include<iostream>
using namespace std;

void main()

{ 
cout<<"例外"<<endl;

try

int n1;
cout<<"請輸入除數:";
cin>>n1; 

if (n1==0) 

throw 1;  
} 
cout<<"不是例外"<<endl; 


catch(int e)  

cout<<"除零例外"<<endl;

VC++ - 使用命名空間內的成員

#include<iostream>
using namespace std;

namespace newspace  

int a1=40;
int a2=50;
void newsum(); }

void newspace::newsum() 

int mysum;
mysum=newspace::a1 + newspace::a2;
cout << "a1+a2=" << mysum << endl; 

void main()

newspace::newsum(); 

VC++ - 例外 -說明

#include<iostream>
using namespace std;

void main()

char ch; // 宣告變數num
try 

cout << "輸入字元變數:"; 
cin>>ch;

if (ch>(char)78) 

throw 1;  // 丟出整數型別的例外


catch(char) 

cout << "執行字元區塊" << endl;


cout << "當沒有丟出例外時,會直接執行此行" << endl;

VC++ - 多載化建構子函式

#include<iostream>
using namespace std;

class Data

public:
Data(int a)

cout << "數值:" << a << endl;

Data(char b)

cout << "字碼:" << b << endl;

Data(double c)

cout << "數字:" << c << endl;

};

void main()

    int a;
char p,p1;
cout<<"請輸入一個數值"<<endl;
cin>>a;
     p=a;
 p1=a+26;

Data d1(p);
Data d2(p1);
Data d3(a);

VC++ - 多載類別成員函式

#include<iostream>
using namespace std;

class Score

int china;
int english;
int math;
public:
int sum;
void total_score(int a,int b,int c,char d)

china=a;
english=b;
math=c;
if (d=='Y'||d=='y')
sum=china+english+math;
else
sum=0;
} void total_score(int a,int b,int c)

china=a;
math=b;
english=c;
sum=china+english+math;

};

void main()
{   char d;
    cout<<"請輸入Y或y , 可執行計算"<< endl;
cin>>d;
Score s1;
s1.total_score(90,80,67,d);

cout << "sum=" << s1.sum << endl;
s1.total_score(100,88,88);
cout << "sum=" << s1.sum << endl;

VC++ - 多載函式

#include<iostream>
using namespace std;


int multisum(int a,int b)  

int sum=0;
sum=a*b;
return sum;



int multisum(int a,int b,int c) 

int sum=0;
sum=a*b*c;
return sum;


void main()

cout << "面積:" << multisum(12,12) << endl;
cout << "體積:" << multisum(3,4,5) << endl;
};

VC++ - 多重繼承

#include<iostream>
using namespace std;


class my_score

private:
int china;
int english;
int math;
public:

void set_china(int china)

this->china=china;

int get_china()

return china;


void set_english(int english)

this->english=english;

int get_english()

return english;


void set_math(int math)

this->math=math;

int get_math()

return math;

};

class my_student : public my_score

private:
int sum;
public:
my_student() 

int sum=0;


void show_score()


cout << "國文:" << get_china() << endl;
cout << "英文:" << get_english() << endl;
cout << "數學:" << get_math() << endl;

void add1()


sum=get_china()+get_english()+get_math();
cout << "總成績:" << sum << endl;

};
void main()


my_student st1;
int s1,s2,s3;


cout << "請輸入國文分數:";
cin >> s1;

st1.set_china(s1);

cout << "請輸入英文分數:";
cin >> s2;

st1.set_english(s2);
cout << "請輸入數學分數:";
cin >> s3;

st1.set_math(s3);
cout << "*****************************************" << endl;

st1.show_score();
st1.add1();

VC++ - 單一繼承private型別

#include<iostream>
using namespace std;

class my_data

private:
int no;

protected:
int china;
int narth;
public:
int math;
int english;
my_data()  

no=0;

china=0;
narth=0;
math=0;
english=0;


void set_no(int n1)

no=n1;



void show_no()

cout << "學號 : " << no << endl;



};

class my_data1:private my_data

public:
void set_data1(int n3,int n4)

china=n3;   
narth=n4;

void set_data2(int n5 , int n6)

english=n5;  
math=n6;

void setno(int n7)


set_no(n7); 

void show_data()

show_no();
cout << "國文 :" << china << endl;
cout << "自然 :" << narth << endl;
cout << "英文 :" << english << endl;
cout << "數學 :" << math << endl;

};
void main()

 int i=0,no[3],ch[3],ma[3],en[3],na[3];
while(i<3){
    cout << "學號 :" ;
cin>>no[i] ;
     cout << "國文 :" ;
cin>>ch[i] ;

cout << "自然 :" ;
cin>>na[i];

cout << "英文 :" ;
cin>>en[i];

cout << "數學 :" ;
cin>>ma[i];
i++;
 }
i=0;
my_data1 obj1;
while(i<3){
obj1.setno(no[i]); 

obj1.set_data1(ch[i],na[i]);
obj1.set_data2(en[i],ma[i]);
obj1.show_data();
i++;

VC++ - 單一繼承陣列輸入 - protected型別

#include<iostream>
using namespace std;

class my_data

private:
int no;

protected:
int china;
int narth;
public:
int math;
int english;
my_data()  

no=0;
china=0;
narth=0;
math=0;
english=0;

void set_no(int n1)

no=n1;


void show_no()

cout << "學號 : " << no << endl;


};

class my_data1:protected my_data

public:
void set_data1(int n3,int n4)

china=n3;   
narth=n4;

void set_data2(int n5 , int n6)

english=n5;   
math=n6;

void setno(int n7)


set_no(n7); 

void show_data()

show_no();

cout << "國文 :" << china << endl;
cout << "自然 :" << narth << endl;
cout << "英文 :" << english << endl;
cout << "數學 :" << math << endl;

};
void main()

 int i,no[3],ch[3],ma[3],en[3],na[3];
 for(i=0;i<3;i++){
    cout << "學號 :" ;
cin>>no[i] ;
     cout << "國文 :" ;
cin>>ch[i] ;

cout << "自然 :" ;
cin>>na[i];

cout << "英文 :" ;
cin>>en[i];

cout << "數學 :" ;
cin>>ma[i];
 }
my_data1 obj1;

for(i=0;i<3;i++){
obj1.setno(no[i]); 
obj1.set_data1(ch[i],na[i]);
obj1.set_data2(en[i],ma[i]);

obj1.show_data();

VCC++ - 單一繼承 - protected型別

#include<iostream>
using namespace std;

class my_data

private:
int no;

protected:
int china;
int narth;
public:
int math;
int english;
my_data()  

no=0;
china=0;
narth=0;
math=0;
english=0;


void set_no(int n1)

no=n1;


void show_no()

cout << "學號 : " << no << endl;


};

class my_data1:protected my_data

public:
void set_data1(int n3,int n4)

china=n3;  
narth=n4;

void set_data2(int n5 , int n6)

english=n5;   
math=n6;

void setno(int n7)

set_no(n7); 

void show_data()

show_no();

cout << "國文 :" << china << endl;
cout << "自然 :" << narth << endl;
cout << "英文 :" << english << endl;
cout << "數學 :" << math << endl;

};
void main()

my_data1 obj1;
obj1.setno(1); 
obj1.set_data1(90,99);

obj1.set_data2(88,89);
obj1.show_data();

VC++ - 單一繼承 - public

#include<iostream>
using namespace std;

class my_score

private:
int china;
int math;
int english;
public:
my_score();  
void get_score();
void show_score();
};
my_score::my_score()

china=0;  
math=0; 
english=0;

void my_score::get_score()

cout << "輸入國文成績:";
cin >> china;
    cout << endl<<"輸入英文成績:";
cin >> english;
cout <<endl<< "輸入數學成績:";
cin >> math;

void my_score::show_score()

cout << "國文:" << china << endl;
    cout << "英文:" << english << endl;
cout << "數學:" << math << endl;


class st1:public my_score

private:
int no;
char name[20];
public:
void show_data();
void set_data();
};
void st1::show_data()

cout << "********************************" << endl;
show_score();
cout << "座號:" << no << endl;
cout << "姓名:" << name << endl;

void st1::set_data()

cout << "輸入座號:";
cin >> no;
cout << "輸入姓名:";
cin >> name;

void main()

st1 s1;
s1.get_score();
s1.set_data();
s1.show_data();

VC++- 類別- 物件資料回傳

#include <iostream.h>
#include "MYADD.H"
#include "MYSUB.H"


void main()

int n1,n2,c1;
cout<<"請輸入數值一:";
cin>>n1;
cout<<"請輸入數值二:";
cin>>n2;
cout<<endl<<"請輸入計算方式 : 1 -> +,2 -> -"<<endl;
cin>>c1;
if (c1==1){

Addsum a(n1),b(n2),c(0);
c=a.sum(b);        
    cout <<"a + b = " <<c.x << endl;

else{

subsum d(n1),e(n2),f(0);
f=d.sum(e);       
    cout <<"a - b = " <<f.x << endl;



以下儲存成 MYSUB.H
class subsum

 public:
    int x;
subsum(int); 
subsum sum(subsum);  
};

subsum::subsum(int s)

x=s;   

subsum subsum::sum(subsum obj) 

         x-=obj.x;  
return *this;  
 }

以下儲存成 MYADD.H

class Addsum

 public:
    int x;
Addsum(int); 
Addsum sum(Addsum);  
};

Addsum::Addsum(int s)

x=s;   


Addsum Addsum::sum(Addsum obj) 

         x+=obj.x;  
return *this;  
 }

VC++ - 類別 - 傳遞物件參數 - 指標

#include <iostream.h>
class Addsum

 public:
    int x;
Addsum(int);
Addsum sum(Addsum);   
};
Addsum::Addsum(int s)
{ x=s;}
Addsum Addsum::sum(Addsum obj)

x+=obj.x;  
return *this;

class subsum

 public:
    int x;
subsum(int);
subsum sum(subsum);   
};
subsum::subsum(int s)
{ x=s;}
subsum subsum::sum(subsum obj)

x-=obj.x;  
return *this;

void main()

int x,y;
int*p =&x;
int*p1=&y;
cout<<"請輸入數值一"<<endl;
cin>>x;
cout<<"請輸入數值二"<<endl;
cin>>y;
Addsum a(*p),b(*p1),c(0);
c=a.sum(b);
cout<<"a+b="<<c.x<<endl;
subsum d(*p),e(*p1),f(0);
f=d.sum(e);
cout<<"a-b="<<f.x<<endl;

VC++ - 傳遞物件參數 = 指標

#include <iostream.h>


class Addsum

int x;
 public:
   
Addsum(int);

void sum(Addsum);   
void show();
};


Addsum::Addsum(int s)

x=s;



void Addsum::sum(Addsum obj)

x+=obj.x;  


void Addsum::show()

cout<<x;


void main()

int x,y;
int*p =&x;
int*p1=&y;
cout<<"請輸入數值一"<<endl;
cin>>x;
cout<<"請輸入數值二"<<endl;
cin>>y;
Addsum a(*p),b(*p1);
a.show();
cout<<"+";
b.show();
cout<<"=";
a.sum(b);   
a.show();
cout<<endl;

2008年8月22日星期五

VC++ - 類別 - 建構子參數回圈

#include<iostream>
#include<string>

using namespace std;

class basedata {
private:
char no[6];     
char name[20];  
int math;
int china;  
    int english; 
public:

void showbasedata();
     basedata();
 basedata(char *no1,char *na1, int ma1 , int ch1 , int en1);
};

basedata::basedata()

strcpy(no,"1234");
strcpy(name,"楊妹咕");
math=90;
china=90;
english=89;

};

basedata::basedata(char *no1,char *na1, int ma1 , int ch1 , int en1)


strcpy(no,no1);
strcpy(name,na1);
math=ma1;
china=ch1;
english=en1;

};


void basedata::showbasedata(void)  

cout << "***************************" << endl;

cout << "學號:" << no << endl;
cout << "姓名:" << name << endl;
cout << "數學:" << math << endl;
cout << "國文:" << china << endl;
cout << "英文:" << english << endl;
cout << "***************************" << endl;


void main()

char no2[6];     
char name2[20];  
int math2;
int china2;  
    int english2; 
int i;
for(i=0;i<2;i++){
         cout << "學號:" ;
cin>> no2 ;
cout << "姓名:" ;
cin>> name2;

cout << "數學:" ;
cin>> math2;
cout << "國文:" ;
cin>>china2;
cout << "英文:" ;
cin>>english2;
basedata a1(no2,name2,math2,china2,english2);

a1.showbasedata(); }

vc++ - 類別 自訂計算子函數

#include<iostream>

using namespace std;

class basedata {
public:
char no[6];     
char name[20];  
int math;
int china;  
    int english; 
int sumdata;
void indata(); 
void showbasedata();
void calcdata();
void psdata(); 
};

void basedata::showbasedata(void)  

cout << "***************************" << endl;

cout << "學號:" << no << endl;
cout << "姓名:" << name << endl;
cout << "數學:" << math << endl;
cout << "國文:" << china << endl;
cout << "英文:" << english << endl;
             cout << "總分:" << sumdata << endl;
cout << "***************************" << endl;

void basedata::calcdata(void)  

if (china<60)
china=60;
if (english<60)
english=60;
if (math<60)
math=60;
sumdata = (china+english+math);

    


void basedata::psdata(void)  

cout << "***************************" << endl;


cout << "數學佔總分:" << (float)math/sumdata*100<< "%" << endl;
cout << "國文佔總分:" <<(float)china/sumdata*100<< "%" << endl;
cout << "英文佔總分:" <<(float)english/sumdata*100<< "%" << endl;
            
cout << "***************************" << endl;


void basedata::indata(void)  




cout << "學號:" ;
cin >> no;
cout <<endl<< "姓名:";
cin >> name ;
            cout <<endl<< "數學:";
cin >> math;
cout <<endl<< "國文:";
cin >> china;
cout <<endl<< "英文:";
cin >> english;


void main()

basedata a,b;
cout << "****************************" << endl;
            a.indata();
                a.calcdata();
a.showbasedata();
a.psdata();
cout << "****************************" << endl;
            b.indata();
                b.calcdata();
b.showbasedata();
b.psdata();

VC++ - 類別 計算比例

#include<iostream>

using namespace std;

class basedata {
public:
char no[6];     
char name[20];  
int math;
int china;  
    int english; 
int sumdata;

void showbasedata();
void calcdata();
void psdata(); 
};

void basedata::showbasedata(void)  

cout << "***************************" << endl;

cout << "學號:" << no << endl;
cout << "姓名:" << name << endl;
cout << "數學:" << math << endl;
cout << "國文:" << china << endl;
cout << "英文:" << english << endl;
             cout << "總分:" << sumdata << endl;
cout << "***************************" << endl;

void basedata::calcdata(void)  

if (china<60)
china=60;
if (english<60)
english=60;
if (math<60)
math=60;
sumdata = (china+english+math);

    


void basedata::psdata(void)  

cout << "***************************" << endl;


cout << "數學佔總分:" << (float)math/sumdata*100<< "%" << endl;
cout << "國文佔總分:" <<(float)china/sumdata*100<< "%" << endl;
cout << "英文佔總分:" <<(float)english/sumdata*100<< "%" << endl;
            
cout << "***************************" << endl;


void main()

basedata a;
cout << "****************************" << endl;
         cout << "學號:" ;
cin >> a.no;
cout <<endl<< "姓名:";
cin >> a.name ;
            cout <<endl<< "數學:";
cin >> a.math;
cout <<endl<< "國文:";
cin >> a.china;
cout <<endl<< "英文:";
cin >> a.english;

                a.calcdata();
a.showbasedata();
a.psdata();

VC++ - 類別 - public 計算

#include<iostream>

using namespace std;

class basedata {
public:
char no[6];     
char name[20];  
int math;
int china;  
    int english; 
int sumdata;

void showbasedata();
void calcdata();
};

void basedata::showbasedata(void)  

cout << "***************************" << endl;

cout << "學號:" << no << endl;
cout << "姓名:" << name << endl;
cout << "數學:" << math << endl;
cout << "國文:" << china << endl;
cout << "英文:" << english << endl;
             cout << "總分:" << sumdata << endl;
cout << "***************************" << endl;

void basedata::calcdata(void)  

if (china<60)
china=60;
if (english<60)
english=60;
if (math<60)
math=60;
sumdata = (china+english+math);

    



void main()

basedata a;
cout << "****************************" << endl;
         cout << "學號:" ;
cin >> a.no;
cout <<endl<< "姓名:";
cin >> a.name ;
            cout <<endl<< "數學:";
cin >> a.math;
cout <<endl<< "國文:";
cin >> a.china;
cout <<endl<< "英文:";
cin >> a.english;

                a.calcdata();
a.showbasedata();

2008年8月21日星期四

excel - VBA 工作表事件

Private Sub Worksheet_Activate()

Selection.AutoFilter
Selection.AutoFilter Field:=2, Criteria1:="圍巾"
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Sheets(1).Select
ActiveSheet.Paste
Application.CutCopyMode = False



End Sub

excel -vba 自訂函數 (計算體重)

Public Function 標準(身高)
標準 = 身高 * 身高 * 22 / 10000
End Function


Public Function 評語(您的體重, 標準體重)

If 您的體重 > 標準體重 Then

評語 = "太重"
Else

If 您的體重 < 標準體重 Then

評語 = "太輕"
Else
評語 = "標準"

End If
End If


End Function

excel - vba -相減(自訂)

Public Function 相減(數量1, 數量2, 數量3, 數量4)

If 數量1 = Empty Then
數量1 = 0
End If
If 數量2 = Empty Then
數量2 = 0
End If
If 數量3 = Empty Then
數量3 = 0
End If
If 數量4 = Empty Then
數量4 = 0
End If

相減 = 數量1 - 數量2 - 數量3 - 數量4


End Function

excel - vba 比例函數(自訂)

Public Function 比例函數(數量1, 數量2, 數量3, 數量4)

If 數量1 < 60 Then
數量1 = 0
End If
If 數量2 < 60 Then
數量2 = 0
End If
If 數量3 < 60 Then
數量3 = 0
End If
If 數量4 < 60 Then
數量4 = 0
End If





比例函數 = 數量1 * 0.2 + 數量2 * 0.2 + 數量3 * 0.1 + 數量4 * 0.5


End Function

excel - vba 新增工作表 , 欄 , 列

Sub 新增工作表()

x = InputBox("請輸入新增工作表的張數", "職訓局")
For i = 1 To x Step 1
Sheets(1).Select
Sheets.Add
Sheets(2).Move Before:=Sheets(1)

Next

End Sub
Sub 插入列()
x = InputBox("請輸入新增的列數", "職訓局")
For i = 1 To x Step 1
ActiveCell.Offset(1, 0).Select

Selection.EntireRow.Insert

Next

End Sub
Sub 插入欄()
x = InputBox("請輸入新增的欄數", "職訓局")
For i = 1 To x Step 1
ActiveCell.Offset(0, 1).Select

Selection.EntireColumn.Insert

Next



End Sub

2008年8月20日星期三

vc++ - 資料連續輸入至檔案

#include<iostream>
using namespace std;

struct data {
char no[6];     
char name[20];  
int math;
int china;
    int english;    
};
typedef struct data student;
void main()

FILE *file1;  
char c_name[20],c_no[6];
int c_math,c_china,c_english,ans;
student myst;

file1=fopen("mydata.txt","wb"); 

for(;;){

    cout<<"請輸入學號:";
cin>>c_no;
cout<<"請輸入姓名:";
cin>>c_name;
     cout<<"請輸入數學成績:";
cin>>c_math;
cout<<"請輸入國文成績:";
cin>>c_china;
cout<<"請輸入英文成績:";
cin>>c_english;

strcpy(myst.no,c_no);
strcpy(myst.name,c_name);
myst.math=c_math;
myst.china=c_china;
myst.english=c_english;

fwrite(&myst,sizeof(myst),1,file1);
if (ferror(file1))
cout << "資料寫入錯誤" << endl;
else
cout << "資料寫入完成!!" << endl;


cout << "請問還要輸入學生資料嗎!! 1: 是 2 : 否" << endl;
cin>>ans;
if(ans==1)
break;}
fclose(file1);

VC++ - 檔案輸出10個字換行

#include<iostream>
using namespace std;
void main()


FILE *file1;  
int i,j,k;
char ch1;
char myst[50],st2[50];
cout<<"請輸入";
cin>>myst;


file1=fopen("mydata.txt","w"); 

for(i=0;myst[i] != '\0';i++)
putc(myst[i],file1);
cout << "存檔成功" << endl;
fclose(file1);
file1=fopen("mydata.txt","r"); 

cout << "檔案內容:";
i=0;
while ( (ch1=getc(file1)) != EOF)

st2[i]=ch1;
i++;

for (k=0;k<50;k=k+10){

for(j=0;j<10;j++){

cout << st2[j+k] ;

cout<<endl;}


fclose(file1);

excel - vba 資料分離完整版[任意欄位]

Public x, myclumn, myclumn1

Sub 執行()

x = 0
Sheets(1).Select
myclumn1 = InputBox("請輸入欄號 :A-YAC", "職訓局")
myclumn = myclumn1 & "1"

排序

While (ActiveCell.Value <> Empty)

If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then
x = x
Else

x = x + 1
End If

ActiveCell.Offset(1, 0).Select

Wend


Range(myclumn).Select



Sheets(1).Select
For i = 1 To x - 1 Step 1
Sheets.Add
Next

Sheets("資料").Move Before:=Sheets(1)
命名
搬移

End Sub
Sub 排序()

Range(myclumn).Select
Range("A1:IV65535").Sort Key1:=Range(myclumn), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
SortMethod:=xlStroke, DataOption1:=xlSortNormal
End Sub

Sub 命名()

Range(myclumn).Select
ActiveCell.Offset(1, 0).Select
i = 1
While (ActiveCell.Value <> Empty)

If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then
i = i

Else
i = i + 1
Sheets(i).Name = ActiveCell.Offset(-1, 0).Value

End If

ActiveCell.Offset(1, 0).Select

Wend

Range(myclumn).Select
End Sub

Sub 搬移()

Sheets(1).Select
Range(myclumn).Select
ActiveCell.Offset(1, 0).Select

d = 2
While (ActiveCell.Value <> Empty)


n = ActiveCell.Value
r = ActiveCell.Row
r = r & ":" & r
Rows(r).Select
Selection.Copy
Sheets(n).Select
ActiveSheet.Paste
Application.CutCopyMode = False
ActiveCell.Offset(1, 0).Select

Sheets(1).Select
d = d + 1
mydata = myclumn1 & d
Range(mydata).Select


Wend
End Sub
Sub 清除()
For i = 2 To x Step 1
Sheets(2).Select
ActiveWindow.SelectedSheets.Delete
Next
End Sub

excel - vba 分割命名

Sub 執行()

x = 0
Sheets(1).Select
排序

While (ActiveCell.Value <> Empty)

If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then
x = x
Else

x = x + 1
End If

ActiveCell.Offset(1, 0).Select

Wend

Range("F1").Select



Sheets(1).Select
For i = 1 To x - 1 Step 1
Sheets.Add
Next

Sheets("資料").Move Before:=Sheets(1)
命名

End Sub
Sub 排序()

Range("F1").Select
Range("A1:H65535").Sort Key1:=Range("F1"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
SortMethod:=xlStroke, DataOption1:=xlSortNormal
End Sub

Sub 命名()

Range("F2").Select
i = 1
While (ActiveCell.Value <> Empty)

If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then
i = i

Else
i = i + 1
Sheets(i).Name = ActiveCell.Offset(-1, 0).Value

End If

ActiveCell.Offset(1, 0).Select

Wend

Range("F1").Select
End Sub

Sub 搬移()

n = ActiveCell.Value
r = ActiveCell.Row
r = r & ":" & r
Rows(r).Select
Selection.Copy
Sheets(n).Select
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub

2008年8月19日星期二

excel - vba 合併的工作表

Sub 合併()

x = InputBox("請輸合併的工作表數量")
Sheets(1).Select
Sheets.Add

For i = 2 To x + 1 Step 1
Sheets(i).Select
If i = 2 Then

Range("A1").Select

Else
Range("A2").Select
End If

Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
Selection.Copy
Sheets(1).Select
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.End(xlDown).Select
ActiveCell.Offset(1, 0).Select

Next
End Sub

excel-vba 多列複製

Sub 多列複製()
x = InputBox("請輸入複製的列數", "職訓局")



r = ActiveCell.Row

x = r & ":" & r + x

Rows(x).Select
Selection.Copy

Sheets(2).Select
ActiveSheet.Paste
Application.CutCopyMode = False


End Sub

excel -vba 單列複製

Sub 單列複製()

r = ActiveCell.Row

x = r & ":" & r

Rows(x).Select
Selection.Copy

Sheets(2).Select
ActiveSheet.Paste
Application.CutCopyMode = False


End Sub

excel - vba 自動新增工作表

Sub 自動新增工作表()
i = 0
Range("B1").Select
Range("A1:H65535").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
SortMethod:=xlStroke, DataOption1:=xlSortNormal


While (ActiveCell.Value <> Empty)

If ActiveCell.Value = ActiveCell.Offset(1, 0).Value Then
i = i
Else
i = i + 1
End If


ActiveCell.Offset(1, 0).Select

Wend
Range("B1").Select

'增加工作表

For j = 1 To i - 1 Step 1

Sheets.Add


Next
Sheets("資料").Move Before:=Sheets(1)

End Sub

EXCEL _VBA 增加工作表

Sub 增加工作表()

x = InputBox("請輸入工作表張數", "職訓局")

For i = 1 To x Step 1

Sheets.Add '增加工作表


Next



Sheets("資料").Move Before:=Sheets(1)
End Sub

VC++ - 類別資料回傳

class subsum

 public:
    int x;
subsum(int); 
subsum sum(subsum);  
};

subsum::subsum(int s)

x=s;   

subsum subsum::sum(subsum obj) 

         x-=obj.x;  
return *this;  
 }

以下儲存成 MYADD.H

class Addsum

 public:
    int x;
Addsum(int); 
Addsum sum(Addsum);  
};

Addsum::Addsum(int s)

x=s;   


Addsum Addsum::sum(Addsum obj) 

         x+=obj.x;  
return *this;  
 }

以下是主程式


#include <iostream.h>
#include "MYADD.H"
#include "MYSUB.H"


void main()

int n1,n2,c1;
cout<<"請輸入數值一:";
cin>>n1;
cout<<"請輸入數值二:"; cin>>n2;
cout<<endl<<"請輸入計算方式 : 1 -> +,2 -> -"<<endl;
cin>>c1;
if (c1==1){

Addsum a(n1),b(n2),c(0);
c=a.sum(b);        //c=a+b
    cout <<"a + b = " <<c.x << endl;

else{

subsum d(n1),e(n2),f(0);
f=d.sum(e);        //c=a+b
    cout <<"a - b = " <<f.x << endl;


2008年8月18日星期一

vc++ - 檔案自訂寫入內容

#include<iostream>
using namespace std;
void main()

FILE *file1;  
int i;
char str1[10000];
char str2[20];
cout<<"請輸入您的文章";
cin>>str1;
file1=fopen("mydata.txt","w"); 
i=fputs(str1,file1);
if (i != EOF) 
cout << "存檔成功" << endl;
else
cout << "檔案防寫無法儲存" << endl;

fclose(file1);
file1=fopen("mydata.txt","r"); 
if (file1 == NULL)

cout << "檔案不存在,無法開啟" << endl;
exit(1);

else

while (fgets(str2,11,file1) != NULL) // 10個字元(11-1)

cout << "檔案內容:" << str2 << endl;


fclose(file1);

vc++ - 結構陣列 - 單一輸出

#include<iostream>
using namespace std;


struct score {     
int chinese;  
int math; 
int english;
};  

void main()

score myst[100];
int i,j,sum1=0,sum2=0,sum3=0;
cout << "請輸入學生人數:";
cin >> j;

for(i=0;i<j;i++)

cout << "請輸入第 " << i+1 << " 位同學的成績:" << endl;
cout << "請輸入國文成績:";
cin >> myst[i].chinese;
cout << "請輸入數學成績:";
cin >> myst[i].math;
cout << "請輸入英文成績:";
cin >> myst[i].english;
sum1+=myst[i].chinese; // 計算國文總成績
sum2+=myst[i].math; // 計算數學總成績
sum3+=myst[i].english;

cout << "==================================" << endl;
cout << "這"<<j<<"位同學的國文平均成績為:" << (int)(sum1/j)<< endl;
cout << "這"<<j<<"位同學的數學平均成績為:" << (int)(sum2/j)<< endl;
cout << "這"<<j<<"位同學的數學平均成績為:" << (int)(sum3/j)<< endl;

for(i=0;i<j;i++)

cout << "第 " << i+1 << " 位同學的成績:" << endl;
cout << "國文成績:";
cout<< myst[i].chinese;
cout << "數學成績:";
cout<< myst[i].math;
cout << "英文成績:";
cout<< myst[i].english<< endl;


VC++ - 結構回傳含計算

#include<iostream>
using namespace std;

struct score {     
int chinese; 
int math;
int english; 
int sum;
};  

struct score set_data();

void main()


score you_data;

you_data=set_data();


cout << "*********************************" << endl;
cout << "國文 : " << you_data.chinese << endl;
cout << "數學 : " << you_data.math << endl;
cout << "英文 : " << you_data.english << endl;
cout << "合計 : " << you_data.sum << endl;



struct score set_data()


score my_data;
cout << "請輸入國文成績:";
cin >> my_data.chinese;
cout << "請輸入數學成績:";
cin >>  my_data.math;
cout << "請輸入英文成績:";
cin >> my_data.english;
my_data.sum=my_data.chinese+my_data.english+my_data.math;


return my_data;

VC++ - 自訂結構含計算

#include<iostream>
using namespace std;

struct my_data {     
char my_name[5]; 
int my_wt;   
int my_old;
int my_hight;
double my_str;
char my_a[20];
};              

void main()


my_data data1;

cout<<"輸入姓名:";
cin>>data1.my_name;
cout<<"輸入身高:";
cin>>data1.my_hight;
cout<<"輸入體重:";
cin>>data1.my_wt;
cout<<"輸入年齡:";
cin>>data1.my_old;

data1.my_str=data1.my_hight*data1.my_hight*22.0/10000;

cout<<endl;


if (data1.my_wt > (data1.my_str*1.1))

strcpy(data1.my_a,"你的體重過重");


else if (data1.my_wt < (data1.my_str*0.9))

strcpy(data1.my_a,"你的體重過輕");


else

strcpy(data1.my_a,"你的體重剛好");


cout << "姓名 : " << data1.my_name << endl;
cout << "身高 : " << data1.my_hight << endl;
cout << "體重 : " << data1.my_wt << endl;
cout << "年齡 : " << data1.my_old << endl;
cout << "標準體重 :" << data1.my_str <<endl;
cout << "評語 :" << data1.my_a <<endl;

VC++ - 自訂結構

#include<iostream>
using namespace std;

struct my_data {     
char my_name[5]; 
int my_wt;   
int my_old; 
int my_ht;
double my_st;

};              

void main()


my_data data1;
cout<<"請輸入姓名: ";
cin>>data1.my_name;
cout<<"請輸入年齡: ";
cin>>data1.my_old;
cout<<"請輸入身高: ";
cin>>data1.my_ht;
cout<<"請輸入體重: ";
cin>>data1.my_wt;
    data1.my_st = data1.my_ht*data1.my_ht*22/10000;



cout <<"姓名 : " << data1.my_name << endl;
cout << "體重 : " << data1.my_wt << endl;
cout << "年齡 : " << data1.my_old << endl;
cout << "身高 : " << data1.my_ht << endl;
cout << "標準 : " << data1.my_st << endl;

excel - vba -間差值不同工作表

Sub 間差值不同工作表()

Sheets("Sheet1").Select
ActiveCell.Value = 1
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=1, Stop:=30, Trend:=False



Sheets("Sheet2").Select

ActiveCell.Value = 2
Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
Step:=2, Stop:=30, Trend:=False


Sheets("Sheet1").Select
Range("a1").Select
While (ActiveCell.Value <> Empty)
ActiveCell.Offset(1, 0).Select
Selection.ClearContents
ActiveCell.Offset(1, 0).Select
Wend


End Sub

excel-vba:資料移動

Sub 移動()

While (ActiveCell.Value <> Empty)

Sheets("Sheet2").Select
Selection.Cut
Sheets("Sheet1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Select
Sheets("Sheet2").Select

ActiveCell.Offset(1, 0).Select

Wend
End Sub

excel vba 數字轉日期

Sub 數字轉日期()

While (ActiveCell.Value <> Empty)

y = Left(ActiveCell.Value, 2) + 1911
m = Left(Right(ActiveCell.Value, 4), 2)
d = Right(ActiveCell.Value, 2)
ActiveCell.Value = y & "/" & m & "/" & d

ActiveCell.Offset(1, 0).Select

Wend
End Sub

excel-vba日期格式轉數字格式

Sub 國曆()
Dim y, m, d


While (ActiveCell.Value <> Empty)



y = Year(ActiveCell.Value) - 1911
m = Month(ActiveCell.Value)
d = Day(ActiveCell.Value)


If m < 10 Then
m = 0 & m
End If


If d < 10 Then
d = 0 & d
End If




ActiveCell.Value = y & m & d





Selection.NumberFormatLocal = "000000"

ActiveCell.Offset(1, 0).Select



Wend


End Sub

2008年8月17日星期日

EXCEL- VBA 移動資料

1 工具 / 巨集 / VB編輯
2 點選左側工作表 / 輸入下列內容
Private Sub Worksheet_Activate()

Dim r, i, sr, sc


Range("c1").Select

i = 1
While (ActiveCell.Value <> Empty)
If ActiveCell.Value = "y" Then
sr = i & ":" & i
Rows(sr).Select
Selection.Cut
Sheets("y").Select
ActiveSheet.Paste
ActiveCell.Offset(1, 0).Select

Sheets("all").Select
Rows(sr).Select
Selection.Delete Shift:=xlUp
sc = "c" & (i)

Range(sc).Select
Else
sc = "c" & (i + 1)

Range(sc).Select
i = i + 1
End If

Wend



End Sub
註 : 會自動將欄C內儲存格的值為Y移至


工作表Y

Private Sub Worksheet_Activate()

Sheets("y").Select
Range("A1").Select
Selection.End(xlDown).Select
x = ActiveCell.Row
If x = 65536 Then

Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
Else
ActiveCell.Offset(1, 0).Select
End If
End Sub

註:此為自動偵測資料內容

 
妹咕數位學園歡迎網友們來信指教 妹咕信箱