首页 > 解决方案 > 多次调用函数

问题描述

我的代码有问题。

在工作表“主要”中,我有以下代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error Resume Next


Application.EnableEvents = False
Application.Calculation = xlManual
Application.ScreenUpdating = False
ThisWorksheet.EnableCalculation = False

Set Target = Tabelle1.Range("D6")

If Not Intersect(ActiveCell, Target) Is Nothing Then

    Dim OpenFiles As String
    wbcount = Application.Workbooks.count

    For i = 1 To wbcount
        If OpenFiles = "" Then
            OpenFiles = Application.Workbooks(i).Name
        Else
            OpenFiles = OpenFiles & "," & Application.Workbooks(i).Name
        End If

    Next

    With Target.Validation
        .Delete
        .Add Type:=xlValidateList, Formula1:=OpenFiles
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End If

End Sub

这使得从下拉列表中选择打开的工作簿成为可能。我使用了一些不同的函数,这些函数在“主”表中的选定单元格中调用。我的问题是,我在这些函数中有一些输入框:这些都被调用了两次,我怎样才能避免这个问题?

示例函数的代码:

功能之一的代码:

Public Function getSerialStr(hcc1 As String)
    Dim wb As Workbook

    Set wb = Workbooks(hcc1)

    Dim i As Integer, j As Integer, count As Integer
    Dim hcc As Worksheet





    For i = 1 To wb.Worksheets("Table 1").UsedRange.Rows.count
        For j = 1 To 15


        If InStr(1, wb.Worksheets("Table 1").Cells(i, j).Value2, "Serial", 1) > 0 Then

        itis = wb.Worksheets("Table 1").Cells(i + 1, j).Value



        If Len(itis) < 4 Then
            itis = InputBox("No Serial found, please type in manually!", "Serial Missing" & vbCrLf)



        GoTo ende


        End If


        GoTo ende


        End If


        Next j
    Next i

ende:
 getSerialStr = itis

End Function





标签: excelvba

解决方案


推荐阅读