设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 3920|回复: 0

Excel VBA常用代码VSTO版(C#)-Winner

[复制链接]

点击这里给我发消息

发表于 2015-8-17 15:44:40 | 显示全部楼层 |阅读模式
Excel VBA常用代码VSTO版(C#)-Winner
Excel VBA常用代码VSTO版(C#)


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 = icell;
     }

1-3 使用快捷记号
#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["A14"], 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();
    注:此处的代码,可以运行,但是只会选中A1这一个单元格

2-3 使用Goto方法
      this.Application.Goto(this.Application.Worksheets[3].Range["A1:B10"], true);


3-1 获得指定行,列中的最后一个非空单元格
      Excel.Range rng = this.Range["A65535"].End[Excel.XlDirection.xlUp];
    MessageBox.Show("A列中最后一个非空单元格是" + rng.Address[0, 0] + ",行号" + rng.Row.ToString() + ",数值" + 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("没有找到单元格!");
  }
      }   
      注:C#中没有InputBox,这里用文本框代替,另,C#中没有with……End with语句.



5-1 查找单元格重复数据
     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运算符
    C#中没有Like运算符,可以用正则表达式来处理.

6-1 替换单元格内字符串
      this.Range["A1:A5"].Replace("通州", "南通");

7-1 复制单元格区域
      this.Application.DisplayAlerts = false;
     this.Range["A1"].CurrentRegion.Copy(this.Application.Worksheets[2].Range["A1"]);
     this.Application.DisplayAlerts = true;

7-2 复制单元格区域时带列宽大小
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 = Rng.Value;


9-1 单元格自动进入编辑状态
            先在”VSTO 设计器生成的代码”内加入
            this.SelectionChange += new 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;
            }


您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-3-29 16:25 , Processed in 0.089034 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表