简体   繁体   English

为什么我的 VBA 代码不运行调用的宏

[英]Why my VBA code doesn't run the macros called

I have a problem with this code, doesn't run the sub I'm calling and tells me that xlApp is not defined…我对这段代码有疑问,没有运行我正在调用的子程序,并告诉我 xlApp 未定义……

The thing is that I needed to separate each “Sub” because the sendkeys didn't work as I wanted when I use 1 sub to execute it all, so I split it.问题是我需要分开每个“Sub”,因为当我使用 1 个 sub 来执行它时,sendkeys 没有按我想要的方式工作,所以我将它分开。

Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const slp As Variant = 1000

Sub Main()
    Dim mywb As Workbook: Set mywb = ThisWorkbook
    Dim wb As Workbook
    Dim xlApp As Application
    
     Set wb = Nothing
     Set xlApp = Nothing
    
Call WhichOne
Call Unprotect

End Sub
Sub WhichOne()

    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim strFolder As String: strFolder = "K:\Drive RMB\RMB Chantier\7_Sous Traitance\ISF CHRONO SOUS-TRAITANT"
    Dim strTempFile As String: strTempFile = strFolder & "aaOWB.bas"
    Dim wb1 As Workbook
    Dim xpath As String
    
    Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker)
        xFileDialog.AllowMultiSelect = False
        xFileDialog.Title = "Choisir un fichier"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    
    If xStrPath = "" Then Exit Sub
       xFile = Dir(xStrPath)
    Set wb = xlApp.Workbooks.Open(xFile)
        xlApp.Visible = True
    
End Sub
Sub Unprotect()

        With Application
        .SendKeys "%{F11}", True
        Sleep slp
        .SendKeys "^r", True
        Sleep slp
        .SendKeys "~", True
        Sleep slp
        .SendKeys "792432700", True
        Sleep slp
        .SendKeys "~", True
        Sleep slp
        End With
        
End Sub

Valid code with Ike suggestions:带有 Ike 建议的有效代码:

Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const slp As Variant = 1000

Sub Main()
    Dim mywb As Workbook: Set mywb = ThisWorkbook
    
    
     
    
Call WhichOne
Call Unprotect

End Sub
Sub WhichOne()

    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    Dim strFolder As String: strFolder = "K:\Drive RMB\RMB Chantier\7_Sous Traitance\ISF CHRONO SOUS-TRAITANT"
    Dim strTempFile As String: strTempFile = strFolder & "aaOWB.bas"
    Dim wb As Workbook
    Dim wb1 As Workbook
    Dim xpath As String
    
    Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker)
        xFileDialog.AllowMultiSelect = False
        xFileDialog.Title = "Choisir un fichier"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    
    If xStrPath = "" Then Exit Sub
       xFile = Dir(xStrPath)
    Set wb = Application.Workbooks.Open(xFile)
        Application.Visible = True
    
End Sub
Sub Unprotect()

        With Application
        .SendKeys "%{F11}", True
        Sleep slp
        .SendKeys "^r", True
        Sleep slp
        .SendKeys "~", True
        Sleep slp
        .SendKeys "792432700", True
        Sleep slp
        .SendKeys "~", True
        Sleep slp
        End With
        
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM