简体   繁体   中英

Problem with myArray in the for…next loop

Below macro doesn't work correctly in myArray_november part. This macro extracts invoice to the folder. But some documents can be doubled, so I need to check if document already was extracted to the folder, and if it was, how many times (left 9 digits of the file name) - "s" variable in the macro. Then I use "s" variable to define the row in the SAP report.

However the results for "s" variable are "strange". I think this is related with myArray_november in the loop for...next or in the scope for this loop.

Could you help, please?

code

Sub salr87013019()

Application.ScreenUpdating = False
    SheetSrc = "Input data"

    On Error Resume Next
    
    If Not IsObject(SAPApplication) Then
        Set SapGuiAuto = GetObject("SAPGUI")
        If Err.Number <> 0 Then Exit Sub
        Set SAPApplication = SapGuiAuto.GetScriptingEngine
        If Err.Number <> 0 Then Exit Sub
    End If
    
    If Not IsObject(Connection) Then
        Set Connection = SAPApplication.Children(0)
        If Err.Number <> 0 Then
            MsgBox ("Please, open SAP!")
            
            Exit Sub
        Else
            
        End If
    End If

    If Not IsObject(session) Then
        Set session = Connection.Children(0)
    End If

    On Error GoTo 0
 
    Application.Wait (Now + TimeValue("0:00:01") / 1.5)
    
    Dim i As Double
    Dim last_row As Double
    
    last_row = Cells(Rows.Count, 1).End(xlUp).Row
    
    For i = 2 To last_row
    session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/Ns_alr_87013019"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/txt$6-KOKRS").Text = "EU01"
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").Text = Cells(i, 1)
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").SetFocus
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").caretPosition = 6
session.findById("wnd[0]/tbar[1]/btn[8]").press
'On Error GoTo line1
On Error Resume Next
session.findById("wnd[0]/shellcont/shell/shellcont[2]/shell").hierarchyHeaderWidth = 453
session.findById("wnd[0]/usr/lbl[62,8]").SetFocus
session.findById("wnd[0]/usr/lbl[62,8]").caretPosition = 9
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[1]/usr/lbl[1,2]").SetFocus
session.findById("wnd[1]/usr/lbl[1,2]").caretPosition = 4
session.findById("wnd[1]").sendVKey 2
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").currentCellColumn = "BELNR"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectedRows = "0"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").contextMenu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectContextMenuItem "&XXL"
session.findById("wnd[1]/usr/cmbG_LISTBOX").SetFocus
session.findById("wnd[1]/usr/cmbG_LISTBOX").Key = "31"
session.findById("wnd[1]/tbar[0]/btn[0]").press
Dim path As String
path = Cells(2, 6)
Dim name As String
name = Cells(i, 1).Value & ".XLSX"
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = path
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = name
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 10
session.findById("wnd[1]/tbar[0]/btn[0]").press
'line1:
Application.Wait Now + TimeValue("0:00:05")

Dim wB As Workbook
Dim ws As Worksheet

  With Application
        .DisplayAlerts = False
        .EnableEvents = False
        .ScreenUpdating = False
    End With

    Set wB = Workbooks.Open(path & name)
    Set ws = wB.Sheets(1)
    
    Application.Wait Now + TimeValue("0:00:05")

Workbooks(name).Activate

'Columns("K:K").Select
'    Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
'    Range("K1").Select
'    ActiveCell.FormulaR1C1 = "action"

Dim k As Double
Dim last_row_document
last_row_document = Cells(Rows.Count, 10).End(xlUp).Row - 2

For k = 2 To last_row_document
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/Ns_alr_87013019"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/txt$6-KOKRS").Text = "EU01"
Workbooks("Saving_invoice").Activate
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").Text = Cells(i, 1)
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").SetFocus
session.findById("wnd[0]/usr/ctxt_6ORDGRP-LOW").caretPosition = 6
session.findById("wnd[0]/tbar[1]/btn[8]").press
On Error Resume Next
session.findById("wnd[0]/shellcont/shell/shellcont[2]/shell").hierarchyHeaderWidth = 453
session.findById("wnd[0]/usr/lbl[62,8]").SetFocus
session.findById("wnd[0]/usr/lbl[62,8]").caretPosition = 9
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[1]/usr/lbl[1,2]").SetFocus
session.findById("wnd[1]/usr/lbl[1,2]").caretPosition = 4
session.findById("wnd[1]").sendVKey 2
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").setCurrentCell -1, "BELNR"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectColumn "BELNR"
session.findById("wnd[0]/tbar[1]/btn[29]").press
Workbooks(name).Activate
session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").Text = Cells(k, 10)
session.findById("wnd[1]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").caretPosition = 9
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").currentCellColumn = "BELNR"

'###########################################################
'####SPRAWDZA CZY JEST JUZ PLIK O TAKIEJ NAZWIE#############
'###########################################################
Dim objFSO_november As Object
Dim objFolder_november As Object
Dim objFile_november As Object
Dim objFile1_november As Object
Dim aa_november As Integer


Dim bb_november As Integer

Set objFSO_november = CreateObject("Scripting.FileSystemObject")

Set objFolder_november = objFSO_november.GetFolder("C:\Users\plkake\Desktop\invoice\")

bb_november = 0
For Each objFile1_november In objFolder_november.Files

bb_november = bb_november + 1

Next objFile1_november

Dim myArray_november() As Variant
ReDim Preserve myArray_november(bb_november, 1)


aa_november = 0

For Each objFile_november In objFolder_november.Files

myArray_november(aa_november, 1) = objFile_november.name

aa_november = aa_november + 1
Next objFile_november

Dim z As Double
Dim how_digits As Double
how_digits = Len(Cells(k, 10))
Dim s As Double

s = 0

For z = 0 To aa_november - 1
If Left(myArray_november(z, 1), how_digits) = Cells(k, 10) Then
s = s + 1
End If
Next z

'MsgBox (s)
'MsgBox (myArray_november(6, 1))

session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").selectedRows = s  'tutaj nr linii po filtrowaniu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell/shellcont[1]/shell").doubleClickCurrentCell
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_VIEW_ATTA"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").pressToolbarContextButton "&MB_FILTER"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectContextMenuItem "&FILTER"
session.findById("wnd[2]/usr/subSUB_DYN0500:SAPLSKBH:0600/cntlCONTAINER1_FILT/shellcont/shell").currentCellRow = 2
session.findById("wnd[2]/usr/subSUB_DYN0500:SAPLSKBH:0600/cntlCONTAINER1_FILT/shellcont/shell").selectedRows = "2"
session.findById("wnd[2]/usr/subSUB_DYN0500:SAPLSKBH:0600/btnAPP_WL_SING").press
session.findById("wnd[2]/usr/subSUB_DYN0500:SAPLSKBH:0600/btn600_BUTTON").press
session.findById("wnd[3]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").Text = "Invoice"
session.findById("wnd[3]/usr/ssub%_SUBSCREEN_FREESEL:SAPLSSEL:1105/ctxt%%DYN001-LOW").caretPosition = 7
session.findById("wnd[3]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").currentCellColumn = "BITM_DESCR"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").selectedRows = "0"
session.findById("wnd[1]/usr/cntlCONTAINER_0100/shellcont/shell").pressToolbarButton "%ATTA_EXPORT"
session.findById("wnd[2]/usr/ctxtDY_PATH").Text = path
If s = 0 Then
session.findById("wnd[2]/usr/ctxtDY_FILENAME").Text = Cells(k, 10).Value & ".PDF"
Else
session.findById("wnd[2]/usr/ctxtDY_FILENAME").Text = Cells(k, 10).Value & "-" & s + 1 & ".PDF"
End If
session.findById("wnd[2]/usr/ctxtDY_FILENAME").caretPosition = 5
session.findById("wnd[2]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[12]").press
Application.Wait Now + TimeValue("0:00:05")


Next k

'Workbooks(name).Save
Workbooks(name).Close

Next i

End Sub

Might be helpful

' Add reference Tools.References Microsoft Scripting Runtime until code is debugged
' Then revert to create object
Dim myFSONovember As Scripting.FileSystemObject
Set myFSONovember = New Scripting.FileSystemObject

Dim myFolderNovember As Scripting.Folder
Set myFolderNovember = myFSONovember.GetFolder("C:\Users\plkake\Desktop\invoice\")

Dim myFileNovember As Variant
Dim mySdNovember As Scripting.Dictionary
Set mySdNovember = New Scripting.Dictionary
For Each myFileNovember In myFolderNovember

    With mySdNovember
    
        .Add .Count, myFileNovember.Name ' count is a dummy to satify the requirement for a key
        
    End With

Next

Dim z As Double
Dim how_digits As Double
how_digits = Len(Cells(k, 10).Value)
Dim s As Double

s = 0

Dim myKey As Long
For Each myKey In mySdNovember

    If VBA.Left$(mySdNovember.Item(myKey), how_digits) = Cells(k, 10).Value Then
    
        s = s + 1
        
    End If
    
Next z

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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