VBS Excel cell to SAP - excel

I am struggeling to get the right informations from an Excel sheet via vbs script into SAP.
A single cell works fine and will be added into SAP but the original Excel sheet contains a cell where the last 4 digits needs to be deleted and afterwards combined with another cell and then added to SAP.
This works for me:
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP06/ssubTABFRA1:SAPLMGMM:2000/subSUB5:SAPLZMM_GINCOFIELDS:5802/ctxtMARC-PRCTR").text = (objSheet.Cells(6,"P") & (objSheet.Cells(38,"P")).Value)
But this dont:
session.findById("wnd[0]/usr/subSUBSCR_BEWERT:SAPLCTMS:5000/tabsTABSTRIP_CHAR/tabpTAB1/ssubTABSTRIP_CHAR_GR:SAPLCTMS:5100/tblSAPLCTMSCHARS_S/ctxtRCTMS-MWERT[1,1]").text = (objSheet.Cells(40,"F") & (Left((40,"F"), Len(40,"F") - 4)) & (objSheet.Cells(39,"L") & (objSheet.Cells(39,"O").Value)
The error message while starting the script means translated: ...at 207 ')' excpected...
Thanks in advance for having a look at this and providing help or a hint.

You are using Left(40,"F"): I suspect that what you actually mean is objSheet.Cells(40, "F"). Same with your Len call, I suspect what you actually want is: Len(objSheet.Cells(40, "F")). So putting that together:
Left(objSheet.Cells(40, "F"), Len(objSheet.Cells(40, "F")) - 4) & objSheet.Cells(39, "L") & objSheet.Cells(39, "O")

Related

Problem with using VBA to insert formula in Excel sheet

I recently ran into an issue when using VBA to insert a formula into an Excel sheet. Below the relevant piece of code;
Calculatie_cell.Offset(n, 5) = "=SOM(H" & (n + 3 - 1) & ":H" & (n + 3 - Onderdelen_aantal) & ")"
Calculatie_cell.Offset(n, 8) = "=SOM(K" & (n + 3 - 1) & ":K" & (n + 3 - Onderdelen_aantal) & ")"
For the first iteration where it triggers, the expected output is;
=SOM(H4:H7)
=SOM(K4:K7)
The output I recieve is;
=#SOM(H4:H7)
=#SOM(K4:K7)
This output yields a #NAME? error. Pressing F2, enter on the cell (like inputting it by hand), gives an error stating that this formula is not supported by older versions of Excel with the option to correct it to the expected output. If I accept this it works.
I can't find why Excel thinks it needs the # and the code below does not filter the # from the formule (but it does filter it out of random text with an #)
Calculatie_cell.Offset(n, 5) = Replace(Calculatie_cell.Offset(n, 5), "#", "")
Calculatie_cell.Offset(n, 8) = Replace(Calculatie_cell.Offset(n, 8), "#", "")
Does anyone have a suggestion how I can get rid of the #, or another way to make the formula work? Thanks in advance!
Relevant note; I use Excel 2016 in Dutch, meaning "SOM" is the correct way to spell "SUM"
First of all, regardless of your Office installation language, VBA asks for engligh input.
Therefore, replace SOM by SUM in your code.
It should appear as SOM in the workbook, but not in the code.
Second of all, using oCell = "=SOME_FORMULA" usually does not yield the intended result.
Instead, one should use oCell.Formula2 = "=SOME_FORMULA".
This is why an # shows up in the formula.
See this article for information.
Finally, your code should be
Calculatie_cell.Offset(n, 5).Formula2 = "=SUM(H" & (n + 3 - 1) & ":H" & (n + 3 - Onderdelen_aantal) & ")"
Calculatie_cell.Offset(n, 8).Formula2 = "=SUM(K" & (n + 3 - 1) & ":K" & (n + 3 - Onderdelen_aantal) & ")"
User #Solar Mike was right.
Even though Excel wants the code to be "SOM" (Dutch spelling), in VBA it needs to ben SUM (English spelling). It auto-translates and works right of the bat. Thank you for your help!
If you post it as an answer, I will accept it as such.

Opening two separate Excel workbooks from a third

I'm attempting to automate something I do each day and I am not getting very far.
We run a report from an HR system each day and then compare the Id's to
1) a list of Id's we are working on this fortnight
2) a list of Id's we worked on last fortnight
3) the report that came out yesterday - so that any that weren't on the first two lists but were picked up by someone yesterday get marked.
I am writing this code in a separate spreadsheet so that this remains each day and the downloaded spreadsheet gets updated.
I thought this would be easy, but it is a couple of years since I've written much vba. But no matter what I try I can't seem to get it to read the third file.
All files are open already.
So the first file is the macro file, which just allows the other files to be selected.
The second file is the Excel document which comes from the HR system. This has "Empl ID" as the first column from A2 up to around to A500 at times.
The third file is the Excel document which details the ID's we are working on this fortnight and has "Emplid" as the first column.
With the code below I can't get the third Excel doc to work, I just get Run time error 9 : subscript out of range.
If I add a watch for Workbooks(txtMSR) I can see the sheet "MSR" as Item1 in Sheets and the name for that is MSR.
Any assistance would be most welcomed.
Dim txtWorkbook As String
Dim vLoop, vLoop2 As Integer
Dim vEID As String
Dim txtMSR As String
Dim txtWork2 As String
'Get workbook name less the .xls bit
'C13 has ps.xls in it
txtWorkbook = Left(Range("C13"), InStr(Range("C13"), ".xls") - 1)
'Next check who has the employees on this MSR
'Set counters to "zero"
vLoop = 2
vLoop2 = 2
'C7 has MSR Fortnight 20190419-20190502.xlsm in it
txtMSR = Left(Range("C7"), InStr(Range("C7"), ".xlsm") - 1)
Do Until Workbooks(txtWorkbook).Sheets("ps").Range("A" & vLoop).Text = ""
vPMKeyS = Workbooks(txtWorkbook).Sheets("ps").Range("A" & vLoop).Value
```
'The line below produces the error.
```
Do Until Workbooks(txtMSR).Sheets("MSR").Range("A" & vLoop2).Text = ""
If (vPMKeyS = Workbooks(txtMSR).Sheets("ps").Range("A" & vLoop2).Value) Then
Workbooks(txtWorkbook).Sheets("ps").Range("N1").Text = "Y"
End If
vLoop2 = vLoop2 + 1
Loop
vLoop = vLoop + 1
Loop

Vlookup using VBA

I have a woorkbook with informations that I am adding into word.
Now I need to add a txt that I have to find by use of vlookup (or index match)
My code will not run, and after 6 hours I am near to give up, but hope, that one of you can help me.
The code is:
wrdApp.Selection.TypeText Text:=(Application.Function.VLookup(Cells((RowNo + i), ColumnForLevel2Text).Value, sectionT.Range("ab1:ac5"), 2, 0))
My Vba code is placed in anoter workbook. But that has not been a problem so far.
I want to lookup for the text in the cell (Range((RowNo + i), ColumnForLevel2Text)
I want to lookup in a range("ab1:ac5") at another sheet in the same workbook, called sectionT.
My text could be 1.1 and I would like to have the text "Result" typed into word.
Regards
Peter
I'm not sure I have everything to solve your problem, but I can help you split your problem into more manageable pieces. If you write your code like this:
Dim LookupValue, TableArray, OutputText
LookupValue = Cells((RowNo + i), ColumnForLevel2Text).Value
Set TableArray = sectionT.Range("ab1:ac5")
OutputText = Application.WorksheetFunction.VLookup(LookupValue, TableArray, 2, 0)
wrdApp.Selection.TypeText Text:=OutputText
you can follow it step by step and see where it went wrong. The big issue here is that you wrote Function instead of WorksheetFunction.

Alphanumerical sorting - paste error - large amount of data

I've devellopped a quick little code to help me sort a large amount of data on an excel tool, it works perfectly except for one tiny detail ruining all of my work so far.
My table is 500+ columns wide and my algorithm would like to copy and paste two rows at a time (I use fused cells and do not wish to use an alternative to that).
My algorithm then tries to do :
Rows(i & ":" & i + 1).Cut
Rows(j & ":" & j + 1).Select
Selection.Insert shift:=xlDown
i and j being the row index (I try to put row j and j+1 just below row i and i+1
Which tells me that excel cannot do with the current ressources.
I tried to reduce the amount of cells with something like
range(cells(i,bc),cells(i+1,ec)).cut
range(cells(j,bc),cells(j+1,ec)).select
Selection.Insert shift:=xlDown
bc being the first column of my selection and ec the last
But still I have the same problem (selection is between 500 and 510 colums so around 1k cells)
So here is the question :
Is there a way to bypass that error and force the cut/paste? (I would like to avoid changing x columns at a time, slowing a process already quite slow)
Or a way to change the index of the row or something alike that I do not know about ?
Thank you for your time and future answers.
PEagle
This is not the answer, just too long to put as a comment.
I just ran the code below (with 700 columns of data per row), and it Cut >> Paste just fine without errors.
Option Explicit
Sub CopyFullTwoRows()
Dim i As Long, j As Long
' just for simulation
i = 2
j = 10
Rows(i & ":" & i + 1).Cut
Rows(j & ":" & j + 1).Insert Shift:=xlDown
End Sub
Sorry i am not yet at comment privilegies, so here is the answer to your comments/answers:
My file is about 6300KB.
For other programs, generaly just the outlook and firefox but computers at work are not very powerful.
Regarding the flatening of my merged cells and then sorting, the problem is then that I need to do sorting but only on even lines.
As it is a file from work I cannot send it on the internet, but to give you an idea, I have 2 lines working together, columns B to F are merged (B8+B9, B10+B11 etc, then C8+C9 etc.) and then I have two rows of data for each of these lines.
Unmerging would cause blank cells to pop and then sorting would mess the whole table.
I am sorry, my question is now obsolete.
I reorked the file, noticing that the file, for a reason I do not know, included all lines until the end of the sheet (1+ million). I have removed the unnecessary lines (recreated the file) and it now works fine.
sorry for your time loss, thank you for helping me.
See you soon.

.Paste Method where `,` is the decimal mark

I'm grabbing data from SAP to paste into my Excel sheet via VBA. (There are other methods for exporting data, but this is the one I am limited to.)
The data is put into the Windows-Clipboard via SAP. Afterwards it is put into the newest Excel-Worksheet (see Sub importStuff and then formatted see Sub divData) The data is delimited with pipes | and after pasting it looks like this (including the blanks):
| FOO: BAR | 360.000 |
After I Call divData it is split into two separate columns. (this how I want it to be formatted)
[SOME_ID: SOME_NAME][360.000]
Sometimes Excel formats 360.000 to 360,000 = 360. This only happens for values that end in a 0. So 360.000 is formatted to 360, 312.312.001.800 would be formatted to 312.312.001,80.
I'm using a German version of Excel 14.0.7166.5000 (32-Bit). So a . is for digit grouping, a , is the decimal mark.
This my code
Sub importStuff()
dBegin = wsUeb.Range("BeginPlan")
dEnd = wsUeb.Range("EndPlan")
'lots
'of
'other
'SAP-Stuff
SAP_Session.findById("wnd[0]/usr/tabsTABSTRIP_MYTAB/tabpPUSH4/ssub%_SUBSCREEN_MYTAB:ZCO_SUSAETZE_NEW:0400/ctxtP_LAYOUT").Text = "/ZL_UMSPIEXP"
SAP_Session.findById("wnd[0]/tbar[1]/btn[8]").press
SAP_Session.findById("wnd[0]/tbar[1]/btn[45]").press
SAP_Session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[4,0]").Select
SAP_Session.findById("wnd[1]/tbar[0]/btn[0]").press
ActiveWorkbook.Sheets.Add After:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Paste
Worksheets(Worksheets.Count).Name = "Plan-Umsaetze " & dBegin & " - " & dEnd
Call divData
End Sub
Sub divData()
ActiveSheet.Columns("A:A").TextToColumns _
DataType:=xlDelimited, _
TextQualifier:=xlTextQualifierNone, _
ConsecutiveDelimiter:=False, _
Other:=True, _
OtherChar:="|"
End Sub
Here is what happens.
I open the workbook
I call importStuff
I call divData afterwards, doesn't matter if it is called within importStuff or not
everything is fine
I call importStuff again
I don't call divData at all (doesn't matter if I do)
I get erroneous values
But if I just press Ctrl+v the values get inserted just fine (see 4.)).
I can't call Ctrl+v via Windows Shell, because the new worksheet is invisible for the user.
This is just Jochen's and my comment posted as an answer. I had the same issue today, that's why I necro'd this post.
I formatted the column to text, imported the data, changed the format back to numbers and then multiplied each value with 1. That did the trick.
you could set Range.numberFormat
or use Range.PasteSpecial( [xlPasteTypes], [xlPasteOperation]), doc here, particularly xlPasteValuesAndNumberFormats or xlPasteSpecialOperationMultiply
I have the same issue but I can solve it by:
range("A1").PasteSpecial(-4142)
it seems like excel sees "---------" as calculation.

Resources