首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将相同的代码应用于同一工作簿中的多个工作表

将相同的代码应用于同一工作簿中的多个工作表
EN

Stack Overflow用户
提问于 2015-06-24 21:28:50
回答 1查看 244关注 0票数 0

我是VBA的新手,我需要一段代码来以同样的方式应用于我的工作簿中的一些工作表。

我需要应用代码的工作表的名称如下:

分析流程排架百分比再填充

分析流机架1选件

分析行柜重新装满百分比

按挑库分析生产线橱柜

分析PFB

分析柜重新装满百分比

精选的分析橱柜

分析流机架2选件

代码如下:如果您能提供任何帮助,我们将不胜感激。非常感谢

代码语言:javascript
运行
复制
 Sub AddCheckBox()

 Application.ScreenUpdating = False

 Dim cell As Range

 DelCheckBox  'Do the delete macro
'or delete all checkboxes in the worksheet
' ActiveSheet.CheckBoxes.Delete
ActiveWindow.View = xlNormalView
lastRow = ActiveSheet.Range("B" & ActiveSheet.Rows.Count).End(xlUp).Row

For Each cell In Range("A5:A" & lastRow)
With ActiveSheet.CheckBoxes.Add(cell.Left, _
cell.Top, cell.Width, cell.Height)
.LinkedCell = cell.Offset(, 8).Address(External:=True)
'.Interior.ColorIndex = 37   'or  xlNone or xlAutomatic
.Caption = ""
'.Border.Weight = xlThin
End With
Next

 With Range("A5:A" & lastRow)
.Rows.RowHeight = 15
Worksheets("Analysis Flow Racking % Refill ").CheckBoxes.Select
Selection.ShapeRange.Align msoAlignCenters, msoFalse
Selection.ShapeRange.IncrementLeft 50
Range("A10000").Select


End With
ActiveWindow.View = xlPageLayoutView
Application.ScreenUpdating = True
End Sub
EN

回答 1

Stack Overflow用户

发布于 2015-06-24 21:47:38

试一试:

代码语言:javascript
运行
复制
Sub Driver()
  Dim ws as Worksheet
  'Since your worksheet names are somewhat variable, 
  'I'd suggest passing an array of known names from a driver routine to your worker routine.
  For each ws in Worksheets(Array("Analysis Flow Racking % Refill", _
                                  "Analysis Flow Racking 1 Picks", _
                                  "Analysis Line Cupboards %Refill"))
                                  'continue with the rest of your worksheets here...
    AddCheckBox ws
  Next
  'If however, you're processing all the worksheets in the workbook, then this will be easier
  For each ws in ActiveWorkbook.Sheets
    AddCheckBox ws
  Next
End Sub

您现在需要修改AddCheckBox()例程以接受工作表作为参数:

代码语言:javascript
运行
复制
Sub AddCheckBox(ByVal TheSheet as Worksheet)

  Application.ScreenUpdating = False
  DelCheckBox  'Do the delete macro
  'or delete all checkboxes in the worksheet
  ' ActiveSheet.CheckBoxes.Delete
  ActiveWindow.View = xlNormalView
  Dim LastRow as integer   'always declare your variables!
  lastRow = ActiveSheet.Range("B" & ActiveSheet.Rows.Count).End(xlUp).Row

  Dim cell As Range
  For Each cell In TheSheet.Range("A5:A" & lastRow)
    With TheSheet.CheckBoxes.Add(cell.Left, _
                                 cell.Top, cell.Width, cell.Height)
      .LinkedCell = cell.Offset(, 8).Address(External:=True)
      '.Interior.ColorIndex = 37   'or  xlNone or xlAutomatic
      .Caption = ""
      '.Border.Weight = xlThin
    End With
  Next

  'Note: removed WITH from here - it only effected 1 row and was confusing    
  TheSheet.Range("A5:A" & lastRow).Rows.RowHeight = 15
  ''''''''''''''''''''''''''''''
  TheSheet.CheckBoxes.Select
  Selection.ShapeRange.Align msoAlignCenters, msoFalse
  Selection.ShapeRange.IncrementLeft 50
  Range("A10000").Select
  '
  'I believe that this code can be replaced with this:
  TheSheet.Checkboxes.ShapeRange.Align msoAlignCenters msoFalse
  TheSheet.Checkboxes.ShapeRange.IncrementLeft 50
  ''''''''''''''''''''''''''''''
  ActiveWindow.View = xlPageLayoutView
  Application.ScreenUpdating = True
End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31027966

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档