Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

excel - VBA auto executes full code even in Step Into (Debug ode) after FileDialog folder selection step

I am creating a macro to create invoices from a template and filling the details with data table.

UPDATE: I have found the issue. The code is not the problem. I was trying the code in step into mode every time and excel was executing the whole code after the FileCopy step automatically. I added a msgbox step and a application wait step to slow down the process. That's when I realized that the whole code is getting executed. Can someone help me figure out why that is happening even in debug mode?

Below is the code I have until now. It worked fine a couple of times. But now it suddenly ends at line where the copy of the template is saved in the selected folder from the FileDialog picker. Can't figure out what is going wrong. No errors show up while executing.

Option Explicit

Sub MMRetCI()

'select destination folder
Dim Dfolder As FileDialog
Dim DFolderPath As String

Set Dfolder = Application.FileDialog(msoFileDialogFolderPicker)

With Dfolder
    .Title = "Select Destination Folder"
    .AllowMultiSelect = False
    .Show
    DFolderPath = .SelectedItems(1) & ""
    Debug.Print DFolderPath
End With

'Create copy of template
Dim strtemplatename As String
Dim UserName As String
strtemplatename = "MM_Returns CI_Template.xlsm"

    FileCopy "Location of Template" & strtemplatename, DFolderPath & strtemplatename 'Location of template is dummy. I have an actual path there. The code suddenly ends after this line.

'Turn Filter on
With Workbooks("Returns for pre-Brexit orders_Template.xlsm")
If Not Sheets("RawData").AutoFilterMode Then
    Sheets("RawData").Range("A1").AutoFilter
  End If
End With

'Get all order IDs in array
Dim WSRaw As Worksheet
Dim OrderCount As Double
Set WSRaw = Workbooks("Returns for pre-Brexit orders_Template.xlsm").Sheets("RawData")

    With WSRaw
        OrderCount = Range("Z2", .Range("Z2").End(xlDown)).Rows.Count + 1
    End With

Dim OrderArrayAddress As Range
Dim ArrayOfOrders() As Variant
Dim i As Double
    
    ArrayOfOrders = Sheets("RawData").Range("Z2").CurrentRegion.Value
    
 'Write order number to filter cell
    With Sheets("CI Details")
    For i = 2 To OrderCount
    Range("B1").Formula = ArrayOfOrders(i, 1)
    
    'More operations here
    
    Next
    End With
End Sub
question from:https://stackoverflow.com/questions/65907487/vba-auto-executes-full-code-even-in-step-into-debug-ode-after-filedialog-folde

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...