【问题标题】:Receiving "Compile Error: Next without For" when attempting to insert new formatting instructions to existing code尝试将新的格式化指令插入现有代码时收到“编译错误:Next without For”
【发布时间】:2020-10-22 17:06:36
【问题描述】:

我已对现有代码进行了修改(我在下面插入双 星号)。现有代码无需我添加就可以正常工作,我不确定为什么一旦包含其他单元格边框格式,我就会收到上述提示“编译错误:下一步没有 For”。我收到此错误消息的代码行也是双 星号。我查看了现有的线程,但似乎没有任何建议适用。请问有人可以帮忙吗?谢谢!

     `For Each WS In Worksheets
    'This code will delete the debt financing in the Property Summary tab
    With Worksheets("Property Summary").Activate

      'Range might need to be updated if it has moved around due to AE 
        version updates.
        Range("E27:H36").Delete
        Range("E27:H36").Select
        Selection.ClearContents
        Selection.Borders(xlDiagonalDown).LineStyle = xlNone
        Selection.Borders(xlDiagonalUp).LineStyle = xlNone
        Selection.Borders(xlEdgeLeft).LineStyle = xlNone
        Selection.Borders(xlEdgeTop).LineStyle = xlNone
        Selection.Borders(xlEdgeBottom).LineStyle = xlNone
        Selection.Borders(xlEdgeRight).LineStyle = xlNone
        Selection.Borders(xlInsideVertical).LineStyle = xlNone
        Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    
        **Range("E26:H26").Select
        Selection.Border(xlEdgeLeft).LineStyle = xlNone
        Selection.Border(xlEdgeRight).LineStyle = xlNone
        With Selection.Border(xlEdgeTop)
        .Pattern = xlContinuous
        .TintAndShade = 5
        .Weight = xlThin
        End With**
    

    'This code will clean up the Cash Flow Report tab
    With Worksheets("Cash Flow").Activate
        
        'Range might need to be updated if it has moved around due to AE version updates
        Range("A2").Select
    
        Cells.Replace What:=" (Amounts in EUR)", Replacement:="", LookAt:= _
            xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        
        Cells.Replace What:=" (Amounts in PLN)", Replacement:="", LookAt:= _
            xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
       
        Cells.Replace What:=" (Amounts in USD)", Replacement:="", LookAt:= _
            xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        
        NomProperty = Range("A2").Value

        'Blue
        Range("B8:L8").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .ThemeColor = xlThemeColorAccent1
            .TintAndShade = 0.599993896298105
            .PatternTintAndShade = 0
        End With
        
        'Clean Up
                    
        'This will unmerge the first 4 rows (A1:M4) and delete columns M:Z
        Range("A1:M4").Select
        Selection.UnMerge
        Columns("L:Z").Select
        Selection.Delete Shift:=xlToLeft
                  
        'This will delete unwanted rows and minimize rows
        For i = 9 To 100
            
            If Cells(i, 1).Value = "" Or Cells(i, 1).Value = " " Or Cells(i, 1).Value = "  " Or 
            Cells(i, 1).Value = "   " Or Cells(i, 1).Value = "    " Then
                Rows(i).RowHeight = 3
            GoTo Following
            End If
            
            If Cells(i, 1).Value = "  Scheduled Base Rent" Then GoTo Following
            If Cells(i, 1).Value = "  CPI Increases" Then GoTo Following
            If Cells(i, 1).Value = "  Free CPI Increases" Then GoTo Following
            If Cells(i, 1).Value = "Total Rental Revenue" Then GoTo Following
            If Cells(i, 1).Value = "Total Other Revenue" Then GoTo Following
            If Cells(i, 1).Value = "Potential Gross Revenue" Then GoTo Following
            If Cells(i, 1).Value = "Total Vacancy & Credit Loss" Then GoTo Following
            If Cells(i, 1).Value = "Effective Gross Revenue" Then GoTo Following
            If Cells(i, 1).Value = "Net Operating Income" Then GoTo Following
            If Cells(i, 1).Value = "  Total Capital Expenditures" Then GoTo Following
            If Cells(i, 1).Value = "Total Leasing & Capital Costs" Then GoTo Following
            If Cells(i, 1).Value = "Cash Flow Before Debt Service" Then GoTo Following
            If Cells(i, 1).Value = "Cash Flow Available for Distribution" Then GoTo Following
            If Cells(i, 1).Value = "Total Other Tenant Revenue" Then GoTo Following
            If Cells(i, 1).Value = "Total Tenant Revenue" Then GoTo Following
            If Cells(i, 1).Value = "Total Operating Expenses" Then GoTo Following
            If Cells(i, 1).Value = "Property Resale" Then GoTo Following
            If Cells(i, 1).Value = "Total Cash Flow" Then GoTo Following
                         
            Rows(i).Delete Shift:=xlUp
            i = i - 1

            Rows(9).RowHeight = 12.75
            
                            
            
               
    Following:
    Next

    DATABASE_LOC = "C:\Users\32948\Desktop\Cockpit\Cockpit"

    If IsFileOpen(DATABASE_LOC & "\Cashflow Appendix.xlsx") Then
    Windows("Cashflow Appendix.xlsx").Activate
    Else
    Set WbDatabase = Workbooks.Open(DATABASE_LOC & "\Cashflow Appendix.xlsx")
    End If

    WB_Appendix_Name = "Cashflow Appendix.xlsx"

    Windows(WB_Appendix_Name).Activate

    Columns("A:Z").Select
    Selection.Delete Shift:=xlUp


    Cells.Select
    Selection.Copy

    Windows(WB_Appendix_Name).Activate
    Cells.Select
    ActiveSheet.Paste


    ActiveWorkbook.Save
    ActiveWorkbook.Close

    Set WbDatabase = Nothing

    End With
     


    **Next WS**


    ActiveWorkbook.Save
    ActiveWorkbook.Close True
    Application.StatusBar = MyFile & " closed."
 
    'read next filename
    MyFile = Dir()
    Loop` 

【问题讨论】:

  • 您的缩进不一致......修复它有助于发现错误。
  • 看看这个链接,看看你是否可以将那里的 guid3lines 应用到你的代码中:stackoverflow.com/questions/10714251/…
  • 非常感谢。

标签: excel vba for-loop


【解决方案1】:

With Worksheets("Property Summary").Activate

Activate 方法不会返回您可以在 With 块中使用的任何内容

? typename(activesheet.activate) '>>Boolean

而且您没有使用外部循环中的WS

【讨论】:

  • 谢谢,蒂姆,有趣的是,这出现在原始代码中,似乎可以工作
  • 它可能不会出错,而仅仅是因为你从未真正使用那个外部With
【解决方案2】:

您是否获得了您插入的代码?我无法仅在 sa 示例宏中运行插入,但它运行了

Sub topborder()
Range("E26:H26").Select
    Selection.Borders(xlEdgeLeft).LineStyle = xlNone
    Selection.Borders(xlEdgeRight).LineStyle = xlNone
    With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .TintAndShade = 0
    .Weight = xlThin
    End With
End Sub

【讨论】:

    猜你喜欢
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多