【发布时间】:2016-11-14 00:50:27
【问题描述】:
我目前正在用 VBA 编写一个循环,由于某种原因,它只能在第一个“If”语句为真之前工作。之后,它还将宏应用于错误的单元格。我只是找不到原因。在我的示例中,操作 1.-3。应该只对 x = 12 和 x = 25 执行但代码对所有 x >= 12 和 x 执行宏
我已经尝试了三个小时来修复此代码并在 Internet 上的某个地方找到答案... :-( 如果您能提供帮助将非常高兴!提前非常感谢!
Sub CreateReport()
Dim lastrow As Long
Dim x As Long
lastrow = Sheets("Overview").Cells(Rows.Count, 1).End(xlUp).Row
For x = 10 To lastrow
If ActiveSheet.Range("A" & x).EntireRow.Hidden = False Then
'1. Copy sheet once per visible row
Sheets("Master").Select
Sheets("Master").Copy After:=Sheets(Sheets.Count)
'2. Paste company name
ActiveSheet.Cells(4, 1).Value = Sheets("Overview").Cells(x, 1).Value
'3. Name worksheet after company name
ActiveSheet.Name = Cells(4, 1).Value
End If
Next x
End Sub
【问题讨论】:
标签: excel vba loops if-statement