Macro不会像预期的那样通过工作簿循环

时间:2016-06-17 15:16:26

标签: excel vba excel-vba

我试图编写一个宏,将公式插入工作簿中所有工作表中的B-F列。由于某种原因,工作簿循环不起作用,宏只是在第一个工作簿中连续运行。有人有什么想法吗?

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

1 个答案:

答案 0 :(得分:4)

您在DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Date sdate= null; String d = null; for(int i =0;i<pd.size();i++){ d = pd.get(i).getDate(); try{ sdate = (Date)formatter.parse(d); if(events.contains(sdate)){ } else{ events.add(sdate); System.out.println(sdate); } }catch(ParseException r){ System.out.println("error"); } } //arraylist of events for(int i = 0; i < events.size(); i++) { Calendar cal1 = Calendar.getInstance(); cal1.setTime(events.get(i)); int day1 = cal1.get(Calendar.DAY_OF_MONTH); int month1 = cal1.get(Calendar.MONTH); int year1 = cal1.get(Calendar.YEAR); //selected month and year on JCalendar if(month == month1 && year == year1) { // Calculate the offset of the first day of the month cal.set(Calendar.DAY_OF_MONTH,1); int offset = cal.get(Calendar.DAY_OF_WEEK) -1; component[day1 + offset ].setBackground(Color.blue); } } 功能之前错过了几个点:

Range

的变化:

  1. 添加了所需的点。
  2. 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 移至循环中以确定每张表的最后一行。
  3. 删除iLastRow
  4. 组合了多行