How to use exported data as input in SAP with VBA - excel

I am writing a VBA script to check all measuring point, who are part of the selection have inspection orders. For this I need to extract a large amount of measering point (ca. 2721) and use them as an input in another transaction.
The problem I have is: What is the most efficient way to extract / export a large amount of data from SAP in a way that I can paste them as an input in SAP?
I can make an export, but I cannot access the exported Excel-file through VBA. I can loop through the datarows and copy every single cell, but this is very slow, as shown below.
row = 1
For i = 1 To session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").RowCount - 2
session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").CurrentCellRow = i
ThisWorkbook.Sheets("Output2").Cells(row, 1) = session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").GetCellValue(i - 1, "TPLNR")
ThisWorkbook.Sheets("Output2").Cells(row, 2) = session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell").GetCellValue(i - 1, "POINT")
row = row + 1
Next

You should
use an array - instead of writing directly to the sheet
use with to not call session.FindByID... multiple times
Dim arrData As Variant, i As Long
With session.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell")
ReDim arrData(1 To .RowCount - 2, 1 To 2)
For i = 1 To .RowCount - 2
.CurrentCellRow = i
arrData(i, 1) = .GetCellValue(i - 1, "TPLNR")
arrData(i, 2) = .GetCellValue(i - 1, "POINT")
Next
End With
With ThisWorkbook.Sheets("Output2")
.Resize(UBound(arrData, 1), 2).Value = arrData
End With
But maybe it's worth to look into Powerquery to achieve what you want.

Related

Data Record Automatically Every Time the Value Changing

Previously, I was using this VBA to collect the data Automatically every time the data change in the Row2. However, currently this script is can't be use again.
When I start, it always shows the pop-up in "Method '_Default' of object 'Range'failed.
And Excel just record/copy the Column A even if there is no change on that row.
Private Sub Worksheet_Calculate()
capturerow = 2
currow = Range("A65536").End(xlUp).Row
Cells(currow + 1, 1) = Cells(capturerow, 1)
Cells(currow + 1, 2) = Cells(capturerow, 2)
Cells(currow + 1, 3) = Cells(capturerow, 3)
Cells(currow + 1, 4) = Cells(capturerow, 4)
End Sub
Right now, I am using Microsoft Excel 2010.
This method function for record data tick based on my MT4 using DDE. I want to analyze the data.
Would you mind helping me about this?
Here I attach the screenshot of the Pop up and Excel
Thank you.
Result in Excel, Pop up in VBA, Pop up in VBA Highlighted

Import data from one sheet to another with multiple function

I need help. Is it possible to import a data from one sheet to another but the data I'm trying to import should be substracted from a tab of the original sheet?
The formula should look like this:
=(importrange("https://LINKHERE","JAN!j18")).
Now the data on J18 is what I'm trying to import but it is being added from Jan!j18. I'm trying to put this to Feb!j18 (but wants to exclude the added data from Jan!18. It was added intentionally to feb but the other sheet needs a monthly numbers only. Any help please?
Sorry, I don't quite follow your problem.
But if you import a range into an array, you can then loop through it and manipulate it
Sub test1()
arr1 = Range(Worksheets(1).Cells(1, 1), Worksheets(1).Cells(4, 3))
'Note, that the array will start at 1, rather than 0.
For iRow = 1 To UBound(arr1, 1)
For iCol = 1 To UBound(arr1, 2)
'Which is convenient as we're pasting back to a workbook
Worksheets(2).Cells(iRow, iCol) = arr1(iRow, iCol) + 10
Next iCol
Next iRow
End Sub
Do pay attention to the array indices - as going to and from ranges & workbooks is abnormal relative to usual array use

Dynamic excel sum function

I am trying to make a sum formula on a dynamic range. Much like in a pivot table.
Taking a look on the picture I want L15 to be the sum of the range from L16 to the blank row. As the range is dynamic I am not sure how to write it on my code. So far what I have is this:
If out.Range("A15").Cells(i, 1) = "Aktier" Then
out.Range("L15").Cells(i, 1) = Application.WorksheetFunction.Sum(Range("L15").Cells(i + 1, 1), Range("L15").Cells(i + 1, 1).End(xlDown))
End If
So my question is basically how do I write something like Sum(A1:End(xlDown))? :)
Hope you can help me out guys! :D
Thanks in advance!
First I would recommend that every time that's possible, you get a structure more Data Base-like, so I would have a column repeating the concept and then you could use the Excel SUMIF function easily.
Probably that's not your case (that seems the output of an accounting program). Taking advantage of using VBA macros, you can use a loop to generate the column I mentioned before (you could do the sum as well using the concept, but I believe is cleaner generating a better data format). Please see the image below:
Sub Add_Concepts()
i = 1
Concept = Cells(i, 2)
i = 2
Do While (Cells(i, 2) <> "")
Cells(i, 1) = Concept
If Cells(i + 1, 2) = "" Then 'Change in concept
Concept = Cells(i + 2, 2) 'New concept
i = i + 2 'add 2 to skip the New concept line and the white space
End If
i = i + 1
Loop
End Sub
Now you can use the regular Excel functions.
Hope this helps!
Skipping Steps
Snippet:
If out.Range("A15").Cells(i, 1) = "Aktier" Then
With out.Range("L15")
.Cells(i, 1) = Application.WorksheetFunction _
.Sum(.Parent.Range(.Offset(1), .Offset(1).End(xlDown)))
End With
End If
Working example:
Option Explicit
Sub SumToBlank()
Dim out As Worksheet: Set out = ThisWorkbook.Worksheets("Sheet1")
Dim i As Long: i = 1
If out.Range("A15").Cells(i, 1) = "Aktier" Then
With out.Range("L15")
.Cells(i, 1) = Application.WorksheetFunction _
.Sum(.Parent.Range(.Offset(1), .Offset.End(xlDown)))
End With
End If
End Sub

Excel, importing data from different file

I want to rotate values from a separate Excel file like this:
To a vertical list:
I mean, after every ninth value, I want the program to start a new column (as in the picture).
I manage to do this manually (ofc B) ) when the different "sheets" are on the same document (Sheet1, Sheet2 etc).
Is this even possible what I am trying, without too much further programming? Should I be using Excel macros? I appreciate all the help I get..!
Store the data matrix in an array and slice off transposed 'rows' of values.
Dim x As Long, vVALs As Variant
With Worksheets("Sheet1")
vVALs = .Range("A2:I5").Value2
For x = LBound(vVALs, 1) To UBound(vVALs, 1)
.Cells(7, 1).Offset((x - 1) * UBound(vVALs, 2)).Resize(UBound(vVALs, 2), 1) = _
Application.Transpose(Application.Index(vVALs, x, 0))
Next x
End With

Excel to Dynamically Insert Fields

I have an Excel spreadsheet devoted to monitoring flags and other variables on a device and they are displayed as follows and are dynamically updated:
Flag name TimeStamp Value
SomeFlag 05:45:12 0
SomeOther 08:22:23 1
Another 08:22:23 0
I have another spreadsheet where I provide an overview of all the devices being monitored. On this spreadsheet, I would like to only display flags that have the value of 1 on the devoted spreadsheets. So for the example above, it would look as follows:
Voltage 09:22:45 230V
Current 09:22:45 15A
Flags: SomeOther 1
If more flags were to become 1, they must be added dynamically. For example, if the Another flag becomes 1, the overview would look as follows:
Voltage 09:22:46 230V
Current 09:22:46 15A
Flags: SomeOther 1
Another 1
Can someone assist me? I know how to use VBA, but I need some guidance in getting started with this.
The below will read the data into an array
Sub t()
Dim arr()
ReDim arr(1)
i = 0
With ActiveSheet
lastrow = .Range("A2").End(xlDown).Row
For Each cell In Range("A2:A" & lastrow)
If cell.Offset(0, 2).Value = 1 Then
arr(i) = cell.Value
arr(i + 1) = cell.Offset(0, 2).Value
ReDim Preserve arr(UBound(arr) + 2)
i = i + 2
End If
Next
End With
For i = 0 To UBound(arr()) - 2 Step 2
Debug.Print arr(i) & ":" & arr(i + 1)
'change debug.print to the range/sheet etc of where you would like the output to start
'may need to clean output range first
Next
End Sub
the output is currently only outputting to the immediate window so that will need to be changed but should be enough to get you going. Remember to step 2 when outputting array as you are putting 2 values per iteration

Resources