操作单元格
约 266 字小于 1 分钟
2024-07-07
选中单元格
Sub 选中单元格()
'不支持变量
[a1].Select
Cells(10, 1).Select
Range ("a1")
Range ("a1:a10")
'value默认可省略
Range("c1").Value = 1
End Sub
偏移
Sub 偏移()
'单元格向下2右边偏移3
Range("a1").Offset(2, 3).Select
Dim rng As Range
'选中最后一行
For Each rng In Range("a1:c" & Range("a65536").End(xlUp).Row)
If rng.Offset(0, -1) = "男" Then
rng = "先生"
Else
rng = "女士"
End If
Next
End Sub
除此之外,还有一些比如entirerow
、resize
、call
需要在实际操作中使用
筛选
Sub 筛选()
Dim i As Integer
For i = 2 To Sheets.Count
Sheet1.Range("a1:f108").AutoFilter field = 4, critieral = Sheet(i).Name
Sheet1.Range("a1:f108").Copy Sheet(i).Range("a1")
Next
Sheet1.Range("a1:f108").AutoFilter
End Sub
新建表
Sub 新建表()
Dim shit As Worksheet
Dim label, i As Integer
'新建几张表
For i = 1 To 3
'设置初始值
k = 0
'判断是否有相同名的表
For Each shit In Sheets
If shit.Name = Sheet1.Range("a" & i) Then
k = 1
End If
Next
If k = 0 Then
Sheets.Add after:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = Sheet1.Range("a1")
End If
Next
End Sub