Excel VBA自动创建sheet,设置字体,单元格颜色和边框

  1 Sub link()
  2     Dim num, sheetname
  3
  4     Worksheets(1).Select
  5
  6     num = WorksheetFunction.CountA(Columns("c:c"))
  7     ‘MsgBox num
  8
  9     For i = 2 To num
 10         ‘把第一个sheet中第3列第i行单元格的值赋值给sheetname,作为后面创建sheet时的名称
 11         sheetname = VBA.UCase(Trim(Sheets(1).Cells(i, 3)))
 12
 13         ‘用单元格的值作为sheet名创建sheet
 14         On Error Resume Next
 15         Worksheets.Add(after:=Worksheets(Worksheets.Count)).Name = sheetname
 16
 17         ‘在新建的sheet中,给A1单元格输入“返回”字符串
 18         Sheets(sheetname).Cells(1, 1) = "返回"
 19
 20        ‘将新建的sheet中,返回字符串所在单元格创建链接,链接地址是第一个sheet中第3列,第i行单元格
 21         Sheets(sheetname).Hyperlinks.Add Anchor:=Sheets(sheetname).Cells(1, 1), Address:="", SubAddress:="汇总!C" & i
 22
 23         ‘在新建的sheet中添加固定格式
 24         Sheets(sheetname).Cells(2, 1) = Trim(Sheets(1).Cells(i, 1)) + ".1 表" + sheetname
 25         Sheets(sheetname).Cells(2, 1).Font.FontStyle = "加粗"
 26
 27         Sheets(sheetname).Cells(3, 1) = Trim(Sheets(1).Cells(i, 1)) + ".1.1 表" + sheetname + "的卡片"
 28         Sheets(sheetname).Cells(3, 1).Font.FontStyle = "加粗"
 29
 30         Sheets(sheetname).Cells(4, 1) = "名称"
 31         Sheets(sheetname).Cells(4, 1).Interior.Color = RGB(153, 204, 255)
 32         Sheets(sheetname).Cells(4, 1).Font.FontStyle = "加粗"
 33         Sheets(sheetname).Cells(4, 1).Borders.LineStyle = xlContinuous
 34
 35         Sheets(sheetname).Cells(4, 2) = VBA.UCase(Trim(Sheets(1).Cells(i, 2)))
 36         Sheets(sheetname).Cells(4, 2).Interior.Color = RGB(255, 255, 204)
 37         Sheets(sheetname).Cells(4, 2).Borders.LineStyle = xlContinuous
 38
 39
 40         Sheets(sheetname).Cells(5, 1) = "代码"
 41         Sheets(sheetname).Cells(5, 1).Interior.Color = RGB(153, 204, 255)
 42         Sheets(sheetname).Cells(5, 1).Font.FontStyle = "加粗"
 43         Sheets(sheetname).Cells(5, 1).Borders.LineStyle = xlContinuous
 44
 45
 46         Sheets(sheetname).Cells(5, 2) = sheetname
 47         Sheets(sheetname).Cells(5, 2).Interior.Color = RGB(255, 255, 204)
 48         Sheets(sheetname).Cells(5, 2).Borders.LineStyle = xlContinuous
 49
 50
 51         Sheets(sheetname).Cells(6, 1) = "注释"
 52         Sheets(sheetname).Cells(6, 1).Interior.Color = RGB(153, 204, 255)
 53         Sheets(sheetname).Cells(6, 1).Font.FontStyle = "加粗"
 54         Sheets(sheetname).Cells(6, 1).Borders.LineStyle = xlContinuous
 55
 56
 57         Sheets(sheetname).Cells(6, 2) = Trim(Sheets(1).Cells(i, 4))
 58         Sheets(sheetname).Cells(6, 2).Interior.Color = RGB(255, 255, 204)
 59         Sheets(sheetname).Cells(6, 2).Borders.LineStyle = xlContinuous
 60
 61
 62         Sheets(sheetname).Cells(8, 1) = Trim(Sheets(1).Cells(i, 1)) + ".1.2 表" + sheetname + "的字段清单"
 63         Sheets(sheetname).Cells(8, 1).Font.FontStyle = "加粗"
 64
 65         Sheets(sheetname).Cells(9, 1) = "名称"
 66         Sheets(sheetname).Cells(9, 1).Interior.Color = RGB(153, 204, 255)
 67         Sheets(sheetname).Cells(9, 1).Font.FontStyle = "加粗"
 68         Sheets(sheetname).Cells(9, 1).Borders.LineStyle = xlContinuous
 69
 70         Sheets(sheetname).Cells(9, 2) = "代码"
 71         Sheets(sheetname).Cells(9, 2).Interior.Color = RGB(153, 204, 255)
 72         Sheets(sheetname).Cells(9, 2).Font.FontStyle = "加粗"
 73         Sheets(sheetname).Cells(9, 2).Borders.LineStyle = xlContinuous
 74
 75         Sheets(sheetname).Cells(9, 3) = "注释"
 76         Sheets(sheetname).Cells(9, 3).Interior.Color = RGB(153, 204, 255)
 77         Sheets(sheetname).Cells(9, 3).Font.FontStyle = "加粗"
 78         Sheets(sheetname).Cells(9, 3).Borders.LineStyle = xlContinuous
 79
 80         Sheets(sheetname).Cells(9, 4) = "类型"
 81         Sheets(sheetname).Cells(9, 4).Interior.Color = RGB(153, 204, 255)
 82         Sheets(sheetname).Cells(9, 4).Font.FontStyle = "加粗"
 83         Sheets(sheetname).Cells(9, 4).Borders.LineStyle = xlContinuous
 84
 85         Sheets(sheetname).Cells(9, 5) = "能否为空"
 86         Sheets(sheetname).Cells(9, 5).Interior.Color = RGB(153, 204, 255)
 87         Sheets(sheetname).Cells(9, 5).Font.FontStyle = "加粗"
 88         Sheets(sheetname).Cells(9, 5).Borders.LineStyle = xlContinuous
 89
 90         Sheets(sheetname).Cells(9, 6) = "默认值"
 91         Sheets(sheetname).Cells(9, 6).Interior.Color = RGB(153, 204, 255)
 92         Sheets(sheetname).Cells(9, 6).Font.FontStyle = "加粗"
 93         Sheets(sheetname).Cells(9, 6).Borders.LineStyle = xlContinuous
 94
 95
 96         ‘MsgBox """" & sheetname & "!A2"""
 97         ‘MsgBox Sheets(1).Cells(i, 3)
 98
 99         ‘在第一个sheet中第3列,第i行添加链接,链接地址是第i个sheet的A1单元格
100         Sheets(1).Hyperlinks.Add Anchor:=Sheets(1).Cells(i, 3), Address:="", SubAddress:=sheetname & "!A1"
101
102     Next
103
104 End Sub
时间: 2024-10-13 17:23:18

Excel VBA自动创建sheet,设置字体,单元格颜色和边框的相关文章

Excel VBA将所有Sheet重命名为单元格a1的名称

Sub RenameSheet() Dim i As Integer For i = 1 To Sheets.Count Sheets(i).Name = Sheets(i).Range("A1").Value Next End Sub

用NPOI创建Excel、合并单元格、设置单元格样式、边框的方法

今天在做项目中,遇到使用代码生成具有一定样式的Excel,找了很多资料,最后终于解决了,Excel中格式的设置,以及单元格的合并等等.下面就介绍下,使用NPOI类库操作Excel的方法. 1.首先我们先在内存中生成一个Excel文件,代码如下:   HSSFWorkbook book = new HSSFWorkbook();        ISheet sheet = book.CreateSheet("Sheet1"); 2.然后在新创建的sheet里面,创建我们的行和列,代码如下

Excel应该这么玩——1、命名单元格

Excel应该这么玩--1.命名单元格 命名单元格:通过名称来引用单元格中的值,常用于引用固定不变的值. 下面举个栗子: 1.土掉渣的方法 平时加班工资按时薪的1倍计算,节假日加班工资按2倍计算.给出员工的时薪和各类加班时数,求加班费.一般会输入如下工资: 如果是新手看到这个公式,第一眼肯定不理解其中的1.2.3表示什么含义.怎么把其中的数字换成容易理解的概念呢? 2.创建系数表 如下,先创建一个列表,把各类加班的时薪倍率列举出来.然后选择B3,在左上角显示单元格地址的名称框输入平时加班倍率,下

【web开发】☆★之利用POI操作Excel表格系列教程【9】单元格边框处理

[web开发]☆★之利用POI操作Excel表格系列教程[9]单元格边框处理 package csg.xiaoye.poidemo; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.a

【web开发】☆★之利用POI操作Excel表格系列教程【10】单元格填充色和颜色操作

[web开发]☆★之利用POI操作Excel表格系列教程[10]单元格填充色和颜色操作 package csg.xiaoye.poidemo; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import

【web开发】☆★之利用POI操作Excel表格系列教程【11】单元格合并

[web开发]☆★之利用POI操作Excel表格系列教程[11]单元格合并 package csg.xiaoye.poidemo; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.p

133自动滚动到被选中单元格(扩展知识:滚动到头部和底部)

效果如下: ViewController.h 1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UITableViewController 4 @property (strong, nonatomic) NSMutableArray *mArrDataSource; 5 6 @end ViewController.m 1 #import "ViewController.h" 2 3 @interface Vi

Winfrom设置DataGridView单元格获得焦点(DataGridView - CurrentCell)

设置DataGridView单元格获得焦点 1 this.dgv_prescription.CurrentCell = this.dgv_prescription.Rows["行号"].Cells["列号/列Name属性"]; 2 this.dgv_prescription.BeginEdit(true);

linux BASH shell下设置字体及背景颜色

BASH shell下设置字体及背景颜色 echo -e "\e[31mtest\e[41m" \e[30m 将字符的显示颜色改为黑色 \e[31m 将字符的显示颜色改为红色 \e[32m 将字符的显示颜色改为绿色 \e[33m 将字符的显示颜色改为淡红色 \e[34m 将字符的显示颜色改为蓝色 \e[35m 将字符的显示颜色改为紫色 \e[36m 将字符的显示颜色改为淡蓝色 \e[37m 将字符的显示颜色改为灰色 \e[40m -- \e[47m 设置背景色 \e[40m 将背景色设