macros没有按预期方式循环使用工作簿

我正在尝试编写一个macros,将公式插入工作簿中所有工作表中的列BF中。 出于某种原因,工作簿循环无法正常工作,macros只是在第一个工作簿中持续运行。 有人有主意吗?

Option Explicit Sub Formuoli() Dim iLastRow As Integer Dim i As Integer Dim ws As Worksheet iLastRow = Range("a10000").End(xlUp).Row For Each ws In ThisWorkbook.Worksheets With ws For i = 1 To iLastRow Range("B" & i).Select Selection.Formula = 'these are formulas Range("C" & i).Select Selection.Formula = 'these are formulas Range("D" & i).Select Selection.Formula = 'these are formulas Range("E" & i).Select Selection.Formula = 'these are formulas Range("F" & i).Select Selection.Formula = 'these are formulas Next i End With Next ws End Sub 

Range函数之前,你错过了几个点:

 Option Explicit Sub Formuoli() Dim iLastRow As Integer Dim i As Integer Dim ws As Worksheet For Each ws In ThisWorkbook.Worksheets With ws iLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row For i = 1 To iLastRow .Range("B" & i).Formula = "" 'these are formulas .Range("C" & i).Formula = "" 'these are formulas .Range("D" & i).Formula = "" 'these are formulas .Range("E" & i).Formula = "" 'these are formulas .Range("F" & i).Formula = "" 'these are formulas Next i End With Next ws End Sub 

变化:

  1. 添加了所需的点。
  2. iLastRow移动到循环中以确定每个工作表的最后一行。
  3. 通过删除Select组合几行。