VBA Strange弹出窗口询问输入文件

时间:2018-04-22 17:22:58

标签: arrays excel vba excel-vba loops

我正在使用以下循环来运行预定义的工作表,但如果我将"ABC"作为数组的一部分包含在内,我将始终获得一个弹出窗口,就好像我必须输入一个文件一样。如果删除"ABC",则不会弹出窗口。有谁知道原因是什么?

Dim nTable As Variant
 nTable = Array("Table 1", "Table 2", "Table 3", "Table 4", "Table 8", _
"Table 11", "Table 12", "Table 13", "ABC") 

For w = LBound(nTable) To UBound(nTable)
    With ActiveWorkbook.Worksheets(nTable(w))
            'input any statement, I used autofit as an example
            .Range("A2:AR1800").Formula = "=IF(ISBLANK('Sheet 1'!A2),"""", 'Sheet 1'!A2)"
            .Range("AM2:BK1800").Formula = "=IF('" & .Name & " S'!N2 =""S"", ""S"", """")"
            .Range("AT2:AU1800").Formula = "='Sheet 1'!BA2"
            .Range("A1:BL1").Columns.AutoFit

    End With
Next w

2 个答案:

答案 0 :(得分:3)

我认为这可能是这一行:

.Range("AM2:BK1800").Formula = "=IF('" & .Name & " S'!N2 =""S"", ""S"", """")"

您的所有其他工作表都是Table 1 STable 2 S等,但ABC刚刚命名为

ABC

而不是

ABC S

答案 1 :(得分:0)

假设ActiveWorkbook有一张名为Sheet 1的工作表,这应该有用......

另外,请使用For Each循环,易于阅读并遵循...

Dim nTable As Variant, tbl
nTable = Array("Table 1", "Table 2", "Table 3", "Table 4", "Table 8", _
"Table 11", "Table 12", "Table 13", "ABC")

For Each tbl In nTable
    With ActiveWorkbook.Sheets(tbl)
        .Range("A2:AR1800").Formula = "=IF(ISBLANK('Sheet 1'!A2),"""", 'Sheet 1'!A2)"
        .Range("AM2:BK1800").Formula = "=IF('" & .Name & "'!N2 =""S"", ""S"", """")"
        .Range("AT2:AU1800").Formula = "='Sheet 1'!BA2"
        .Range("A1:BL1").Columns.AutoFit
    End With
Next tbl