用vba给Excel单元格赋值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用vba给Excel单元格赋值相关的知识,希望对你有一定的参考价值。

参考技术A 1.打开Excel
2010。
2.点击开发工具-宏。
3.命名-然后点击创建。
4.复制以下语句。
Sheet1.Name
=
"对sheet1表名赋值"。
Sheet1.Range("a1")
=
"对sheet1的a1单元格赋值"。
粘贴到下图光标处。
5.了解语句含义,见图。
6.了解语句含义,见图。
7.改成自己需要赋的值,点击运行,然后返回excel表查看是否有所改变。
7.查看结果,可发现已赋值。

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

 

以上是关于用vba给Excel单元格赋值的主要内容,如果未能解决你的问题,请参考以下文章

excel vba 数组中第1位字符为0,赋给单元格时如何将0保留?

excel vba如何将选中的不连续单元格赋值到数组

vba编程 变量赋值单元格

求VBA高手 ComboBox 自动出现在单元格,选择值后自动赋值给当前单元格的问题

java的swing编程,Jtable的单元格如何赋值

vba日历控件只能在一个单元格用怎么办