每次循环后加1DimcntAsIntegercnt=1' 选中区域中已使用的部分, 防止误操作ForEachcInIntersect(ActiveSheet.UsedRange,Selection).Cells' 将要生成的报表名称放在数组里,实践中一般不会直接将数据写入程序当中,而会放在工作表当中。ForEachiInArray("rep1","rep2","rep3",
For Each 变量 In Range('a1:f33') 按照先横后直的原则,遍历'a1:f33'这个单元格区,也就是从'a1'单元格开始,'b1'、'c1'...一直到'f1',再从第二行'a2'单元格开始,'b2'、'c2'...一直到'f2',依次按行进行循环。 (2)Selection 鼠标选定区域 For Each 变量 In Selection 按照先横后直的原则,在鼠标选定...
Sub date2year() Dim tempCell As Range Selection.Value = Selection.Value For Each tempCell In Selection If IsDate(tempCell) = True Then With tempCell .Value = Year(tempCell) .NumberFormat = "0" End With End If Next tempCell End Sub 此代码将日期转换为年份。 82.从日期中删除时间 Sub...
会进入VBA编辑界面!点击菜单栏【插入】,在其下拉菜单中,选择【模块】! 会弹出模块编辑窗口! 在代码编辑窗口输入以下代码: Sub 标注考勤表() Dim i As Range Range("b2:g6").Activate For Each i In Selection If i = "" Or i = "缺勤" Then i = "x" End If Next End Sub 在VBA窗口,点击【运行...
For Each ws In ThisWorkbook.Worksheets If Not ws Is Sheet1 Then ws.Select ws.Buttons.Add(L,T,W,H).Select Selection.OnAction=MSelection.Text=CEnd If Next End Sub SubMacroToRun()MsgBox ActiveSheet.Name End Sub 运行AddButtons过程,即可在每个工作表相同位置添加相同大小的按钮并指定相同的宏。
3 会进入VBA编辑界面!点击菜单栏【插入】,在其下拉菜单中,选择【模块】!4 会弹出模块编辑窗口!5 在代码编辑窗口输入以下代码:Sub 标注考勤表()Dim i As RangeRange("b2:g6").ActivateFor Each i In Selection If i = "" Or i = "缺勤" Then i = "x" End IfNextEnd Sub 6 在VBA窗口,点击...
select是选取的意思,那么selection的意思就是正在被选取。被选取的不仅是单元格,也可以是图表,图表等。 有了Selection,我们就可以做到用户和VBA程序的互动,因为VBA可以随时处理用户选取的单元格区域或图形等。 如下句: Sub test() Selection.Copy Range("A1") '把选取的单元格内容复制到A1单元格 End Sub 再如下面...
Selection.ShapeRange.Fill.UserPicture _"\" & MR.Value & ".jpg"改成 Selection.ShapeRange.Fill.UserPicture _"\" & Left(MR.Value,8) & ".jpg"要
Selection.Item(1).AddComment xStr End Sub Private Sub CommandButton2_Click()删除 '删除批注 Selection.ClearComments End Sub Private Sub CommandButton3_Click()Dim x As Object For Each x In Me.Comments x.Delete Next x Set x = Nothing End Sub 修改 Private Sub CommandButton4_Click()On Error...
SubSlowAddPrefix()'声明变量 Dim strPrefix As String Dim rng As Range '要添加的前缀 strPrefix="完美Excel"'关闭屏幕更新 Application.ScreenUpdating = False '遍历所选区域中的每个单元格'对所选区域的单元格添加前缀 For Each rng In Selection.Cells '如果该单元格不为空则添加前缀 If rng.Value<>""The...