下标超出范围 - 大多数计算机工作正常(包括我的)

时间:2017-02-16 10:23:07

标签: excel vba excel-vba subscript

我是vba的新用户。

最近有一个vba问题让我感到无能为力和无助 - 下标超出范围 - 在特定用户的计算机上,而其他每个用户似乎都没有使用宏的问题(我自己)包括)因此,我不能简单地试验和错误来排除故障。

因此,真的需要各位的专家帮助!非常感谢!!

我使用了一系列vba,它们会一个接一个地运行,并按时间顺序粘贴它们,如下所示。

VBA 1

Sub VBA_1()
Dim ws As Worksheet

        For Each ws In ActiveWorkbook.Worksheets

        ws.Outline.ShowLevels 1, 1

        Next ws
    End Sub

VBA 2

Sub VBA_2()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets

ws.Protect ("Password")

Next ws

End Sub

VBA 3

Sub VBA_3()
Dim iRet As Integer
Dim strPrompt As String



'Prompt
strPrompt = "This will take about 2 minutes. Click 'OK' to proceed."

'Show msgbox
iRet = MsgBox(strPrompt, vbOKCancel)

'When user clicked 'OK'..
If iRet = vbOK Then

'SaveAs Function
Dim fName As String

fName = Application.GetSaveAsFilename(, "Excel Binary Workbook (*.xlsb), *.xlsb")
If fName = "False" Then
MsgBox "File not saved.", vbOKOnly
Cancel = True
End If

Application.EnableEvents = False
ThisWorkbook.SaveAs Filename:=fName, FileFormat:=xlExcel12
Application.EnableEvents = True

' Calculate
   Application.Calculate
   Application.ScreenUpdating = True


' Outlet
    Worksheets("Total Outlets").Activate
'Copy and Paste this portion to each worksheet
        For Each cell In Range("A1")
            If cell.Value = "Not Applicable" Then
            ActiveSheet.Visible = xlSheetHidden
        Else
            Call HypMenuVRefresh
        End If
    Next
'End Outlet & Copy and Paste

    Worksheets("D11101").Activate
        For Each cell In Range("A1")
            If cell.Value = "Not Applicable" Then
            ActiveSheet.Visible = xlSheetHidden
        Else
            Call HypMenuVRefresh
        End If
    Next

    Worksheets("D11102").Activate
        For Each cell In Range("A1")
            If cell.Value = "Not Applicable" Then
            ActiveSheet.Visible = xlSheetHidden
        Else
            Call HypMenuVRefresh
        End If
    Next

'Hide sheets accordingly
Worksheets("Restaurant List").Visible = xlSheetVeryHidden
Worksheets("Hotel List").Visible = xlSheetVeryHidden

'Recalculate
Application.Calculate
Application.ScreenUpdating = True


'Renaming of tabs
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    If ws.Range("A2").Value = 1 Then
        If ws.Visible = xlSheetVisible Then
            On Error Resume Next
                ws.Name = ws.Range("A10").Value
        End If
    End If
Next ws


'Save Workbook
ActiveWorkbook.Save

'Enable finishing screen to be 'Input'
Sheets("Input").Select

'Show msgbox
MsgBox ("Retrieval Completed!")


Else

MsgBox ("Retrieval of Data Cancelled")

End If

End Sub

1 个答案:

答案 0 :(得分:0)

我可以想到以下可能的原因,但不要说其中任何一个是真正的原因:

  

" ...在特定用户的计算机上......"

然后:

  • Excel / VBA的版本不同;

  • 某些全局Option Base已设置(但我认为这不能设置为全局,即适用于所有已加载的工作簿);

  • 某个分隔符是"硬编码"不符合Windows全局设置(控制面板 - >区域和语言 - >格式 - >其他设置);

  • 语言与VBA中的反射不同(例如,母语中的关键字/函数名称或具有非US ASCII 7位字符的标识符名称)。

要找到程序遇到错误(并停止)的位置,请创建一个在每个主要步骤之后将状态消息写入文件的函数。确保在每条消息之后关闭文件,以便实际写入消息。