本文介绍了从 SAP 获取特定单元格数据到 Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 VBA 从 SAP 中一行中的特定单元格获取数据到 excel 表中.当我录制我点击我需要的 5-6 个项目的宏时,它只告诉我我点击了一个.此外,我看不到任何方式将数据从单元格获取到 SAP 中.SAP 代码在这里:

I am trying to get data from specific cells in a row in SAP, into an excel table using VBA. When I record a macro of me clicking the 5-6 items I need, it only gives me that I clicked one. Further more I cannot see any way to get the data from the cells, into SAP. The SAP code is here:

Dim W_Ret As Boolean

W_Ret = Attach_Session
If Not W_Ret Then
    Exit Sub
End If

startrow = 10

On Error GoTo myerr

objSess.findById("wnd[0]").maximize
objSess.findById("wnd[0]/tbar[0]/okcd").Text = "/nymm_pricelist"
objSess.findById("wnd[0]").sendVKey 0
objSess.findById("wnd[0]/usr/ctxtS_MATNR-LOW").Text = Cells(currentline, 1).Value
objSess.findById("wnd[0]/usr/ctxtS_VKORG-LOW").Text = Cells(currentline, 4).Value
objSess.findById("wnd[0]/usr/ctxtS_VKORG-LOW").SetFocus
objSess.findById("wnd[0]/usr/ctxtS_VKORG-LOW").caretPosition = 4
objSess.findById("wnd[0]").sendVKey 8
Set myGrid = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell")
Cells(currentline, 5).Value = myGrid.GetCellValue(0, "MAKTX")
Cells(currentline, 6).Value = myGrid.GetCellValue(0, "GLOBALSALES_KBETR")
Cells(currentline, 7).Value = myGrid.GetCellValue(0, "GLOBALSALES_KONWA")
Cells(currentline, 8).Value = myGrid.GetCellValue(0, "GLOBALTRANSFER_KBETR")
Cells(currentline, 9).Value = myGrid.GetCellValue(0, "GLOBALTRANSFER_KONWA")


' Setting the line status to completed
Cells(currentline, 2).Value = 1
Exit Sub

myerr:
' Some error occured
' Setting the line status to Failed
Cells(currentline, 2).Value = 2

感谢任何帮助!

推荐答案

我解决了如下问题.

  1. 首先,我记录了一个帮助脚本.在这样做时,网格的相关列被一个接一个地标记,例如按升序排列.这样处理完所有感兴趣的列后,就可以发现记录脚本中的所有列名了.

  1. First, I record a help script. In doing so, the relevant columns of the grid are marked one after the other, and e.g. sorted in ascending order. After treating all interested columns in this way, you can discover all column names in the recorded script.

帮助脚本应如下所示:

...session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").currentCellColumn = "GLOBALTRANSFER_KONWA"session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectColumn "GLOBALTRANSFER_KONWA"session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").pressToolbarButton "&SORT_ASC"

然后在正确的脚本中以下:

In the correct script is then e.g. following:

...session.findById("wnd[0]").最大化session.findById("wnd[0]/tbar[0]/okcd").text = "/nymm_pricelist"session.findById("wnd[0]").sendVKey 0session.findById("wnd[0]/usr/ctxtS_MATNR-LOW").text = Cells(currentline, 1).Valuesession.findById("wnd[0]/usr/ctxtS_VKORG-LOW").text = Cells(currentline, 4).Valuesession.findById("wnd[0]/usr/ctxtS_VKORG-LOW").setFocussession.findById("wnd[0]/usr/ctxtS_VKORG-LOW").caretPosition = 4session.findById("wnd[0]").sendVKey 8set myGrid = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell")'myPar_1 = myGrid.getcellvalue (0 , "X")'myPar_2 = myGrid.getcellvalue (0 , "Y")'myPar_3 = myGrid.getcellvalue (0 , "Z")'myPar_4 = myGrid.getcellvalue (0 , "U")'myPar_5 = myGrid.getcellvalue (0 , "V")myPar_6 = myGrid.getcellvalue (0 , "GLOBALTRANSFER_KONWA")

对于字符串 X - V,将使用有效的列名.

For character strings X - V, valid column names are to be used.

问候,脚本人

这篇关于从 SAP 获取特定单元格数据到 Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 00:38