+ Reply to Thread
Results 1 to 3 of 3

macro for pulling data from SAP works but doesn't as loop

  1. #1
    Registered User
    Join Date
    11-16-2021
    Location
    Munich, Germany
    MS-Off Ver
    Office 2013
    Posts
    21

    macro for pulling data from SAP works but doesn't as loop

    I have the following macro to pull data from SAP and want to loop it
    But as soon as i loop it even the first row doesnt work anymore

    I hope someone can tell me where im wrong

    Dim CellValue As String
    Dim SAPBestand As String
    Dim Counter As Integer


    CellValue = Range("A20").Value

    oSession.StartTransaction ("MD04")

    oSession.FindById("wnd[0]").ResizeWorkingPane 150, 17, False
    oSession.FindById("wnd[0]/tbar[0]/okcd").Text = "md04"
    oSession.FindById("wnd[0]").SendVKey 0
    oSession.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-MATNR").Text = CellValue
    oSession.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-BERID").Text = "1301"
    oSession.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-BERID").SetFocus
    oSession.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-BERID").CaretPosition = 4
    oSession.FindById("wnd[0]/tbar[0]/btn[0]").Press
    oSession.FindById("wnd[0]/usr/subINCLUDE1XX:SAPMM61R:0750/tblSAPMM61RTC_EZ/txtMDEZ-MNG02[9,0]").CaretPosition = 13


    SAPBestand = oSession.FindById("wnd[0]/usr/subINCLUDE1XX:SAPMM61R:0750/tblSAPMM61RTC_EZ/txtMDEZ-MNG02[9,0]").Text
    ThisWorkbook.Worksheets("Versorgung Montage").Activate

    Range("I20").Select
    ActiveCell.FormulaR1C1 = SAPBestand






    But as soon as i try this it doesnt work:



    Dim CellValue As String
    Dim SAPBestand As String
    Dim Counter As Integer


    'Dim C As Range
    For Each C In Range("A20:A74")

    C.Select
    CellValue = ActiveCell.Value

    oSession.StartTransaction ("MD04")

    oSession.FindById("wnd[0]").ResizeWorkingPane 150, 17, False
    oSession.FindById("wnd[0]/tbar[0]/okcd").Text = "md04"
    oSession.FindById("wnd[0]").SendVKey 0
    oSession.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-MATNR").Text = CellValue
    oSession.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-BERID").Text = "1301"
    oSession.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-BERID").SetFocus
    oSession.FindById("wnd[0]/usr/tabsTAB300/tabpF01/ssubINCLUDE300:SAPMM61R:0301/ctxtRM61R-BERID").CaretPosition = 4
    oSession.FindById("wnd[0]/tbar[0]/btn[0]").Press
    oSession.FindById("wnd[0]/usr/subINCLUDE1XX:SAPMM61R:0750/tblSAPMM61RTC_EZ/txtMDEZ-MNG02[9,0]").CaretPosition = 13


    SAPBestand = oSession.FindById("wnd[0]/usr/subINCLUDE1XX:SAPMM61R:0750/tblSAPMM61RTC_EZ/txtMDEZ-MNG02[9,0]").Text
    ThisWorkbook.Worksheets("Versorgung Montage").Activate

    C.Offset(0, 7).Select
    ActiveCell.FormulaR1C1 = SAPBestand
    Next C
    Last edited by Erazo; 11-17-2021 at 06:00 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: macro for pulling data from SAP works but doesn't as loop

    What happens when you step through it using F8? If that works OK then it could be the processing of the loop in real time is too fast for the SAP SendVKey commands and you might need to put in a wait function. I would also advise against selecting cells ... slight tweaks below.
    Please Login or Register  to view this content.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  3. #3
    Registered User
    Join Date
    11-16-2021
    Location
    Munich, Germany
    MS-Off Ver
    Office 2013
    Posts
    21

    Re: macro for pulling data from SAP works but doesn't as loop

    It works now, many thanks.
    Also thanks for the tips about making the code more readable(i will think more about my succesors :D) and the tip about cell selecting with it staying selected after the loop!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Recorded macro TexttoColumn doesn't works
    By danusko in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-13-2020, 04:32 AM
  2. please help me identify why this macro works one second and then doesn't.
    By taylorsm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-24-2017, 08:18 PM
  3. My VBA Macro Sometimes Works and Sometimes Doesn't
    By chsaav in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-22-2013, 09:10 PM
  4. Macro that works on PC doesn't work on Mac.
    By elrader in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-09-2012, 03:52 PM
  5. Macro works on some computers but on others it doesn't
    By SEBS_PARADISE in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-16-2011, 04:35 PM
  6. Macro Loop Broken. Detects Max but doesn't continue loop
    By herchenbach in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2011, 12:17 PM
  7. [SOLVED] macro works for some users and doesn't for others
    By Pooja in forum Excel General
    Replies: 2
    Last Post: 10-27-2005, 04:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1