0

我一直致力于将下面的代码从 VBA 翻译成 VB.Net。我已经走了大约 90% 的路,但我被困在 3 个问题上。

首先,在 VBA 中,我使用 Application.GetOpenFilename 从我的代码中打开文件对话框,我似乎无法在 VB 上完成。

其次,我正在尝试导出工作表并将数据复制到工作簿上的工作表上。我使用以下代码行:

     With Globals.ThisWorkbook.Application.ActiveWorkbook.Open(strImportFile)
            .Worksheets(1).Cells.Copy Workbooks(strSourceFile).Worksheets(sDestSheet).Range("A1")
            .Close(savechanges:=False)

不幸的是,中间线不起作用,工作簿给了我一个错误,它不能用作类型。

最后,我正在尝试获取应用程序 ScreenUpdating,但这也失败了。这是我的其余代码:

Imports Microsoft.Office.Interop.Excel

Module adminModule

    Function GetImportFile(Index As Long) As String

        'This function is used in the import_Module to name all of the files
        'that will be imported to the template. The function is primarily used by
        'Sub import_OC_Data

        Select Case Index

            Case 1 : GetImportFile = "byemployee.csv"
            Case 2 : GetImportFile = "byposition.csv"
            Case 3 : GetImportFile = "statusreport.xls"
            Case 4 : GetImportFile = "bydepartment.csv"

        End Select

        Return ""

    End Function

    Function GetDestSheet(Index As Long) As String

        'This function is used in the import_Module to name all of the sheets
        'where the files will be imported to in template.The function is primarily used by
        'Sub import_OC_Data

        Select Case Index

            Case 1 : GetDestSheet = "byDepartment"
            Case 2 : GetDestSheet = "byPosition"
            Case 3 : GetDestSheet = "statusReport"
            Case 4 : GetDestSheet = "byDepartment"

        End Select

        Return ""

    End Function

    Sub importRawData()

        Dim xlWBPath As String = Globals.ThisWorkbook.Application.ActiveWorkbook.Path


        Dim n As Long
        Dim strSourceFile As String
        Dim strImportFile As String
        Dim sDestSheet As String


        Dim strTitle As String = "Verify Source File"
        Dim strPrompt As String = " source file does not exist." & vbNewLine & "Press OK to browse for the file or Cancel to quit"
        Dim strAlert As String = ("You have not selected a workbook." & vbNewLine & "Press Retry to select a workbook or Cancel to exit program")
        Dim strVmbProceedResults As String = ("Procedure Canceled. Your workbook will now close")
        Dim vmbContinue As MsgBoxResult
        Dim vmbProceed As MsgBoxResult



        strSourceFile = Globals.ThisWorkbook.Application.ActiveWorkbook.Na

        For n = 1 To 4 Step 1
            strImportFile = xlWBPath & GetImportFile(n)
            sDestSheet = GetDestSheet(n)

            If Len(Dir(strImportFile)) > 0 Then
                With Globals.ThisWorkbook.Application.ActiveWorkbook.Open(strImportFile)
                    .Worksheets(1).Cells.Copy Workbooks(strSourceFile).Worksheets(sDestSheet).Range("A1")
                    .Close(savechanges:=False)
                End With

            Else

                vmbProceed = MsgBox(strImportFile & strPrompt, vbOKCancel + vbQuestion, strTitle)
                If vmbProceed = vbCancel Then
                    vmbProceed = MsgBox(strVmbProceedResults, vbOKOnly + vbCritical)
                    Globals.ThisWorkbook.Close(saveChanges:=False)
                    Exit Sub

                Else

                    strImportFile = Application.GetOpenFilename("Excel Files (*.xls;*.xlsx; *.xlsm; *.csv), *.xls; *.csv; *.xlsx; *.xlsm")
                    If strImportFile = "False" Then Application.ScreenUpdating = True
                    vmbContinue = MsgBox(strAlert, vbRetryCancel + vbCritical, "No Workbook Selected")

                    If vmbContinue = vbCancel Then
                        Globals.ThisWorkbook.Close(saveChanges:=False)
                        Exit Sub

                    Else

                        strImportFile = Application.GetOpenFilename("Excel Files (*.xls;*.xlsx; *.xlsm; *.csv), *.xls; *.csv; *.xlsx; *.xlsm")
                        Globals.ThisWorkbook.Application.ActiveWorkbook.Open(Filename:=strImportFile)

                        With Globals.ThisWorkbook.Application.ActiveWorkbook.Open(strImportFile)
                            .Worksheets(1).Cells.Copy Workbooks(strSourceFile).Worksheets(sDestSheet).Range("A1")
                            .Close(saveChanges:=False)

                        End With

                    End If

                    On Error GoTo exit_

                    Application.ScreenUpdating = False

                    Globals.ThisWorkbook.Application.ActiveWorkbook.Open(Filename:=strImportFile)
                    With Globals.ThisWorkbook.Application.ActiveWorkbook.Openn(strImportFile)
                        .Worksheets(1).Cells.Copy Workbooks(strSourceFile).Worksheets(sDestSheet).Range("A1")
                        .Close(savechanges:=False)

                    End With
exit_:
                    Application.ScreenUpdating = True
                    If Err() Then MsgBox(Err.Description, vbCritical, "Error")
                End If
            End If
        Next n
    End Sub

End Module
4

0 回答 0