【问题标题】:How to make a loop to delete a sheet if and only if the values are the same?当且仅当值相同时,如何创建循环以删除工作表?
【发布时间】:2019-06-18 08:56:20
【问题描述】:

我从 SAP 提取数据到 excel,我这样做:

  • 事务在 SAP GUI 中自动运行,然后将数据保存在 txt 文件中
  • VBA 读取我的 txt 文件
  • 将其粘贴到 Excel 临时表中,然后粘贴到我的表格中
  • 问题是有时我没有任何数据,因为 SAP 没有找到任何数据,除了粘贴上一个事务的数据(因为我有几个事务要执行)我想要解决的问题问题是我想创建一个循环“如果我在剪贴板中的数据与临时表相同,请选择 timesheet2(它将一直没有数据)”并将其复制到我的表中(这将是空的)

我不确定这是不是正确的方法,但我认为它是可行的,它会解决问题

代码:

   Sub StartExtract()

  ' Set the sid and client to connect to
    W_System = "P10320"
  ' Run the GUI script
    RunGUIScript
  ' End the GUI session
   objSess.EndTransaction
  'effacer contenu feuille temp
   Sheets("temp").Select
   Cells.Select
   Selection.Delete Shift:=xlUp
 ' Switch to the worksheet where the data is loaded to
  Sheets("temp").Select
   
  ' Load the CSV file
   OpenCSVFile



    Sheets("BGSOCIAL").Select
    Columns("B:G").Select
   Selection.ClearContents
   Sheets("temp").Range("B:G").Copy
   Sheets("BGSOCIAL").Range("B:G").PasteSpecial Paste:=xlPasteValues
   Sheets("BGSOCIAL").Select

【问题讨论】:

  • 如果你澄清你的问题,或者可能包括一些屏幕截图,我会尽力帮助你,但有点不清楚。我是一名 SAP 顾问,并且非常擅长使用 Excel,如果我不明白你在问什么,我猜很少有人能做到。祝你好运。如果您改进了问题,请回复。
  • @PGCodeRider 我只想处理错误,如果我的提取没有返回任何“无选定数据”,我希望世界代码继续运行,但是当出现此信息错误时VBA 代码停止
  • 哪里出错了?什么线路?
  • 对不起,没有错误,这只是我运行脚本时的信息。 “没有选定的数据”信息
  • 什么时候,当你尝试 xlPasteValues ?

标签: excel vba sap-gui


【解决方案1】:

您无法评估要复制的区域是否具有值。但是,我认为如果 Temp 范围为空,我们可以跳过下面的所有内容。我还建议使用值集而不是 xlPasteValues

希望有效。

Sub StartExtract()

  ' Set the sid and client to connect to
    W_System = "P10320"
  ' Run the GUI script
    RunGUIScript
  ' End the GUI session
   objSess.EndTransaction
  'effacer contenu feuille temp
   Sheets("temp").Select
   Cells.Select
   Selection.Delete Shift:=xlUp
 ' Switch to the worksheet where the data is loaded to
     Sheets("temp").Select

  ' Load the CSV file
   OpenCSVFile

'Modified code below...


If Application.WorksheetFunction.CountA(Range("B:G")) = 0 Then
    'skips as no values in the range.

Else
'   Dont need to copy and paste, just set the values to being the same.
    Sheets("BGSOCIAL").Range("B:G").Value = Sheets("temp").Range("B:G").Value


End If

  Sheets("BGSOCIAL").Select

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多