1-1 使用Range屬性
this.Range[“A3:F6, B1:C5”].Select();
1-2 使用Cells屬性
for(int icell=1;icell<=100;icell++)
{
this.Application.Worksheets[2].cells[icell, 1].value = http://www.siweifengbao.com/icell;
}
1-3 使用快捷記號(hào)
#N/A
1-4 使用Offset屬性
this.Range[“A1:A3”].Offset[3, 3].Select();
1-5 使用Resize屬性
this.Range[“A1”].Resize[3, 3].Select();
1-6 使用Union屬性
this.Application.Union(this.Range[“A1:D4”], this.Range[“E5:H8”]).Select();
1-7 使用UsedRange屬性
this.UsedRange.Select();
1-8 使用CurrentRegion屬性
this.Range[“A5”].CurrentRegion.Select();
2-1 使用Select方法
this.Application.Worksheets[3].Activate();
this.Application.Worksheets[3].Range[“A1:B10”].Select();
2-2 使用Activate方法
this.Application.Worksheets[3].Activate();
this.Application.Worksheets[3].Range[“A1:B10”].Activate();
注:此處的代碼,可以運(yùn)行,但是只會(huì)選中A1這一個(gè)單元格
2-3 使用Goto方法
this.Application.Goto(this.Application.Worksheets[3].Range[“A1:B10”], true);
3-1 獲得指定行,列中的最后一個(gè)非空單元格
Excel.Range rng = this.Range[“A65535”].End[Excel.XlDirection.xlUp];
MessageBox.Show(“A列中最后一個(gè)非空單元格是” + rng.Address[0, 0] + “,行號(hào)” + rng.Row.ToString() + “,數(shù)值” + rng.Text);
4-1 定位單元格
Excel.Range rng = this.UsedRange.SpecialCells(Excel.XlCellType.xlCellTypeFormulas);
rng.Select();
MessageBox.Show(“工作表中有公式的單元格為:” + rng.Address);
5-1 查找單元格
Excel.Range rng, Rng;
Rng = this.Range[“A:A”];
string strFind = textBox1.Text;
if (strFind.Trim() != string.Empty)
{
rng = Rng.Find(strFind, Rng.Cells[Rng.Cells.Count], Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false);
if (rng != null)
{
this.Application.Goto(rng, true);
}
else
{
MessageBox.Show(“沒(méi)有找到單元格!”);
}
}
注:C#中沒(méi)有InputBox,這里用文本框代替,另,C#中沒(méi)有with……End with語(yǔ)句.
5-1 查找單元格重復(fù)數(shù)據(jù)
Excel.Range rng, Rng;
string FindAddress = string.Empty;
Rng = this.Range[“A:A”];
string strFind = textBox1.Text;
if (strFind.Trim() != string.Empty)
{
rng = Rng.Find(strFind, Rng.Cells[Rng.Cells.Count], Excel.XlFindLookIn.xlValues, Excel.XlLookAt.xlWhole, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlNext, false);
if (rng != null)
{
FindAddress = rng.Address;
do
{
rng.Interior.ColorIndex=6;
rng=Rng.FindNext(rng);
}while(rng != null && rng.Address != FindAddress);
}
}
5-2 使用Like運(yùn)算符
C#中沒(méi)有Like運(yùn)算符,可以用正則表達(dá)式來(lái)處理.
6-1 替換單元格內(nèi)字符串
this.Range[“A1:A5”].Replace(“通州”, “南通”);
7-1 復(fù)制單元格區(qū)域
this.Application.DisplayAlerts = false;
this.Range[“A1”].CurrentRegion.Copy(this.Application.Worksheets[2].Range[“A1”]);
this.Application.DisplayAlerts = true;
7-2 復(fù)制單元格區(qū)域時(shí)帶列寬大小
this.Range[“A1”].CurrentRegion.Copy();
Excel.Range rng = this.Application.Worksheets[3].Range[“A1”];
rng.PasteSpecial(Excel.XlPasteType.xlPasteColumnWidths);
rng.PasteSpecial(Excel.XlPasteType.xlPasteAll);
this.Application.CutCopyMode = Excel.XlCutCopyMode.xlCut;
8-1 使用選擇性粘貼
this.Range[“A1”].CurrentRegion.Copy();
Excel.Range rng = this.Application.Worksheets[3].Range[“A1”];
rng.PasteSpecial(Excel.XlPasteType.xlPasteValues);
this.Application.CutCopyMode = Excel.XlCutCopyMode.xlCut;
8-2 直接賦值的方法
Excel.Range rng = this.Application.Worksheets[3].Range[“A1”];
Excel.Range Rng = this.Range[“A1”].CurrentRegion;
rng.Resize[Rng.Rows.Count, Rng.Columns.Count].Value = http://www.siweifengbao.com/Rng.Value;
9-1 單元格自動(dòng)進(jìn)入編輯狀態(tài)
先在”VSTO 設(shè)計(jì)器生成的代碼”內(nèi)加入
this.SelectionChange += new
【新手vba基礎(chǔ)入門(mén)教程指南 vba編程代碼大全】 Excel.DocEvents_SelectionChangeEventHandler(工作表1_SelectionChange);
然后在事件代碼中輸入
if (Target.Column == 3 && Target.Count == 1)
{
if (Target.Text == string.Empty)
{
this.Application.SendKeys(“{F2}”);
}
}
10-1 禁用單元格拖放功能
if (this.Application.Intersect(Target, this.Range[“A1:A15”]) != null)
{
this.Application.CellDragAndDrop = false;
}
else
{
this.Application.CellDragAndDrop = true;
}
11-1 單元格字體格式設(shè)置
Excel.Font rng = this.Range[“A1”].Font;
rng.Name = “宋體”;
rng.FontStyle = “Bold”;
rng.Size = 18;
rng.ColorIndex = 3;
rng.Underline = 2;
11-2 設(shè)置單元格內(nèi)部格式
Excel.Interior rng = this.Range[“A1”].Interior;
rng.ColorIndex = 3;
rng.Pattern = Excel.XlPattern.xlPatternCrissCross;
rng.PatternColorIndex = 6;
11-3 為單元格區(qū)域添加邊框
Excel.Borders rng = this.Range[“B4:G10”].Borders;
rng.LineStyle = Excel.XlLineStyle.xlContinuous;
rng.Weight = Excel.XlBorderWeight.xlThin;
rng.ColorIndex = 5;
Excel.XlColorIndex col = (Excel.XlColorIndex)5;
this.Range[“B4:G10”].BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium,col);
11-3 為單元格區(qū)域應(yīng)用多種邊框格式
Excel.XlColorIndex col = (Excel.XlColorIndex)5;
Excel.Border rng = this.Range[“B4:G10”].Borders[Excel.XlBordersIndex.xlInsideHorizontal];
Excel.Border Rng = this.Range[“B4:G10”].Borders[Excel.XlBordersIndex.xlInsideVertical];
rng.LineStyle = Excel.XlLineStyle.xlDot;
rng.Weight = Excel.XlBorderWeight.xlThin;
rng.ColorIndex = col;
Rng.LineStyle = Excel.XlLineStyle.xlContinuous;
Rng.Weight = Excel.XlBorderWeight.xlThin;
Rng.ColorIndex = col;
this.Range[“B4:G10”].BorderAround2(Excel.XlLineStyle.xlContinuous, Excel.XlBorderWeight.xlMedium, col);
推薦閱讀
- 附:所有代碼大全及語(yǔ)法 vba編程代碼大全
- 史上最全新手快速升級(jí)全面攻略 天龍八部新手怎么升級(jí)最快
- 新手lol注冊(cè)玩法詳細(xì)攻略教程 lol美服怎么玩
- 新手少走彎路技巧總匯 魔神英雄傳手游攻略大全
- 新手最全快速通關(guān)攻略 我的世界虛無(wú)世界2攻略順序
- 零基礎(chǔ)新手學(xué)電商攻略指南 電商運(yùn)營(yíng)怎么自學(xué)
- 管工下料計(jì)算公式大全 管道施工圖紙入門(mén)基礎(chǔ)知識(shí)
- h3c交換機(jī)配置教程入門(mén) 新手配置h3c交換機(jī)詳細(xì)教程
- 新手搬磚入門(mén)教程 阿拉德之怒去哪里搬磚比較好
- 新手快速升6星攻略 陰陽(yáng)師11天升6星攻略
