How to search row in GuiGridView - vba

I am trying to upload info into SAP GUI from Excel data. The VBA script runs, but the problems are below:
It will only work on rows 10-90 flawlessly. If I need it to be above row 90, I get an error.
See the example below - If one of the rows is missing then I am off by one row the entire time.
Any help would be great!
Code:
If Not IsObject(Application) Then
Set SapGuiAuto = GetObject("SAPGUI")
End If
If Not IsObject(session) Then
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject Application, "on"
End If
session.findById("wnd[0]").ResizeWorkingPane 150, 26, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nzsd_reqord"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtS_VBELN-LOW").Text = salesdoc1
session.findById("wnd[0]/usr/ctxtS_VBELN-LOW").caretPosition = 7
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/shellcont/shell").setCurrentCell -1, "POSNR"
session.findById("wnd[0]/shellcont/shell").SelectColumn "POSNR"
session.findById("wnd[0]/shellcont/shell").PressToolbarButton "&FIND"
session.findById("wnd[1]/usr/txtGS_SEARCH-VALUE").Text = itemnumber
session.findById("wnd[1]/usr/cmbGS_SEARCH-SEARCH_ORDER").Key = "0"
session.findById("wnd[1]/usr/txtGS_SEARCH-VALUE").caretPosition = 2
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]").Close
Dim rowNr As Long
Dim rg As Range
Set rg = Range("B2") 'the cell in question
Select Case rg.Value
Case Is <= 90: rowNr = Left(rg.Value, 1) - 1
Case 100: rowNr = 10
End Select
session.findById("wnd[0]/shellcont/shell").ModifyCell rowNr, "EINTREFF", deldate
session.findById("wnd[0]/shellcont/shell").ModifyCell rowNr, "BEGRU1", reason
session.findById("wnd[0]/shellcont/shell").ModifyCell rowNr, "BEGRU2", Bizpurpose
session.findById("wnd[0]/shellcont/shell").CurrentCellColumn = "BEGRU2"
session.findById("wnd[0]/shellcont/shell").FirstVisibleColumn = "OFMNG"
session.findById("wnd[0]/shellcont/shell").ClearSelection
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[1]/usr/btnBUTTON_1").press

As Sandra Rossi already suspected, it could also simply be the line found in the grid using the find block, which could be queried as follows.
for example:
...
Set rg = Range("B2") 'the cell in question
'Select Case rg.Value
'Case Is <= 90: rowNr = Left(rg.Value, 1) - 1
'Case 100: rowNr = 10
'End Select
rowNr = session.findById("wnd[0]/shellcont/shell").currentCellRow
session.findById("wnd[0]/shellcont/shell").ModifyCell rowNr, "EINTREFF", deldate
...
Regards, ScriptMan

Related

VBA to switch between 2 open PComm sessions

I have been writing some VBA scripts to perform actions in PComm sessions.
For my latest one the process is to have 2 sessions open, both logged in to a different/separate (sub)system.
I'm trying to find out how to make VBA:
- Connect to 1 session
- Check if that session has specific values in specific coordinates (i can use the GetText command for this)
- If it is not the session, switch to the next open session and loop
- If it is the session, continue with the rest of the process (exit sub)
EDIT:
i've got the following code which helps determine if the correct subsystem is open.
Sub FindSession(FindPlatform As Platform)
Dim SessionLetter As String
Dim strSendToPlatform As String
Dim SL As Integer
Dim Timer As Date
Const DelayInterval = "00:01:00"
On Error GoTo Inicio
Inicio:
Select Case FindPlatform
Case 1
strSendToPlatform = "CARE"
Case 2
strSendToPlatform = "GlobStar"
Case 3
strSendToPlatform = "MainFrame"
Case 4
strSendToPlatform = "Citsob"
Case 5
strSendToPlatform = "Cas"
Case 6
strSendToPlatform = "IMS"
End Select
For SL = 1 To 7
If SL = 1 Then SessionLetter = "A"
If SL = 2 Then SessionLetter = "B"
If SL = 3 Then SessionLetter = "C"
If SL = 4 Then SessionLetter = "D"
If SL = 5 Then SessionLetter = "E"
If SL = 6 Then SessionLetter = "F"
If SL = 7 Then SessionLetter = "G"
Set sConnmgr = CreateObject("PCOMM.autECLConnmgr")
Set objwcc = CreateObject("PCOMM.autECLSession")
objwcc.SetConnectionByName SessionLetter
Select Case FindPlatform
Case 1 'CARE
If objwcc.autECLPS.GetText(1, 65, 3) = "AC2" Then
SessionNotFound = False
Exit Sub
Else
Set objwcc = Nothing
End If
Case 2 'GlobStar
If objwcc.autECLPS.GetText(1, 29, 9) = "GLOBESTAR" Or objwcc.autECLPS.GetText(1, 26, 9) = "GLOBESTAR" Or objwcc.autECLPS.GetText(1, 26, 8) = "AMERICAN" Or objwcc.autECLPS.GetText(1, 25, 8) = "AMERICAN" Then
SessionNotFound = False
Exit Sub
Else
Set objwcc = Nothing
End If
Case 3 'MainFrame
If objwcc.autECLPS.GetText(2, 4, 5) <> " " And objwcc.autECLPS.GetText(2, 4, 5) = "USS10" Then
SessionNotFound = False
Exit Sub
Else
Set objwcc = Nothing
End If
Case 4 'Citsob
If objwcc.autECLPS.GetText(1, 2, 8) = "FASBRSCR" Or objwcc.autECLPS.GetText(1, 2, 7) = "FASAUTH" Then
SessionNotFound = False
Do While GetString(1, 2, 8) <> "FASBRSCR"
SendString "", 1, 9, True
Loop
Exit Sub
Else
Set objwcc = Nothing
End If
Case 5 'Cas
If objwcc.autECLPS.GetText(3, 18, 18) = "CARDMEMBER SERVICE" Or objwcc.autECLPS.GetText(3, 22, 3) = "CUR" Or objwcc.autECLPS.GetText(2, 18, 18) = "CARDMEMBER SERVICE" Then
SessionNotFound = False
Exit Sub
Else
Set objwcc = Nothing
End If
Case 6 'IMS
If objwcc.autECLPS.GetText(1, 2, 5) = "WNSPC" Or objwcc.autECLPS.GetText(7, 24, 7) = "SPECIAL" Then
SessionNotFound = False
Exit Sub
Else
Set objwcc = Nothing
End If
End Select
Next SL
It does check all my open sessions to see if the correct system is open, but it doesn't select that session to work in that.
For example, i was logged into the 'CARE' system and 'Globstar' system, and had 'CARE' as the active window. I had the VBA look for 'Globstar', which it did find, but it then continued to send commands to the other open session; in 'CARE'
Can anybody help with how to do this?
Thanks!

Arranging columns in sheets

I'm trying to check if the column headers are in their right positions ( I have declared the actual column headers as constant values) and if they are not move them around to their right positions.
I'm new to VBA and when I tried it out with the basic For loop and Select Case, I realised the code was too long. I was thinking if there is another perhaps a simpler way to do it.
Below is a sample of the code I tried:
Sheet4_Last_RowNum = WorkingSheet.Cells(Rows.Count, 1).End(xlUp).Row
Sheet4_Last_ColNum = WorkingSheet.Cells(Label_RowNum, Columns.Count).End(xlToLeft).Column
For icol = 1 To Sheet4_Last_ColNum
Select Case WorkingSheet.Cells(Label_RowNum, icol).Value
Case "WkVersion"
Sheet4_WkCol = icol
If Sheet4_WkCol <> Sheet4_ActualWk Then
Sheet4_WkValue = WorkingSheet.Range(Cells(headerRow, Sheet4_WkCol), Cells(Sheet4_Last_RowNum, Sheet4_WkCol))
End If
Case "MPA"
Sheet4_MPACol = icol
If Sheet4_MPACol <> Sheet4_ActualMPA Then
Sheet4_MPAValue = WorkingSheet.Range(Cells(headerRow, Sheet4_MPACol), Cells(Sheet4_Last_RowNum, Sheet4_MPACol))
End If
Case "Location"
Sheet4_LocCol = icol
If Sheet4_LocCol <> Sheet4_ActualLoc Then
Sheet4_LocValue = WorkingSheet.Range(Cells(headerRow, Sheet4_LocCol), Cells(Sheet4_Last_RowNum, Sheet4_LocCol))
End If
End Select
Next icol
With WorkingSheet
If IsEmpty(Sheet4_WkValue) = True Then
Else
.Range(Cells(headerRow, Sheet4_ActualWk), Cells(Sheet4_Last_RowNum, Sheet4_ActualWk)) = Sheet4_WkValue
End If
If IsEmpty(Sheet4_MPAValue) = True Then
Else
.Range(Cells(headerRow, Sheet4_ActualMPA), Cells(Sheet4_Last_RowNum, Sheet4_ActualMPA)) = Sheet4_MPAValue
End If
If IsEmpty(Sheet4_LocValue) = True Then
Else
.Range(Cells(headerRow, Sheet4_ActualLoc), Cells(Sheet4_Last_RowNum, Sheet4_ActualLoc)) = Sheet4_LocValue
End If
End With

Need to repeat code 30 times

this is my code and I want to re-execute it so that the next column has the exact same code repeated on it. That is, D:28 moves to E:28 and range E:110:I120 moves to F110:J120. I am having trouble finding a loop that does this, can anyone please help. My code is,
Sub Rebuild()
tonnes = Range("D28").Value
If tonnes > 2600000 Then
Range("E110:I120").Select
Selection.Copy
Range("E18:I28").Select
ActiveSheet.Paste
Else:
Range("E18:I28").Interior.Color = xlNone
Range("E18:I18") = ""
Range("E19:I19") = ""
Range("E20:I20") = 0
Range("E21:I21") = 2.4
Range("E22:I22") = "=E21+E20"
Range("E23:I23") = "=24 - E22"
Range("E24:I24") = "=100 * E23 / 24"
Range("E25:I25") = 3000
Range("E26:I26") = "=E25 * E23"
Range("E27:I27") = "=E26"
Range("E28:I28") = "=D28 + 27"
End If
End Sub
Option Explicit
Sub Rebuild()
Dim cumtonnes As Long
'you initially had tonnes as the variable name, but I was not sure if this was a typo or not.
cumtonnes = Range("D28").Value
If cumtonnes > 2600000 Then
Range("E110:I120").Copy Range("F110:J120")
Range("D28").Copy Range("E28")
Else:
Range("E18:I28").Interior.Color = xlNone
Range("E18:I18") = ""
Range("E19:I19") = ""
Range("E20:I20") = 0
Range("E21:I21") = 2.4
Range("E22:I22") = "=E21+E20"
Range("E23:I23") = "=24 - E22"
Range("E24:I24") = "=100 * E23 / 24"
Range("E25:I25") = 3000
Range("E26:I26") = "=E25 * E23"
Range("E27:I27") = "=E26"
Range("E28:I28") = "=D28 + 27"
End If
End Sub
So I adjusted the part that will do the copy and paste of the cells. I did not add in any loop currently as I did not know what you wanted repeated 30 times.

VBA - Check if recordset has been deleted before updating DB

I'm experimenting with VBA and i made the following thingy.
Dim rs1 As Recordset
Set rs1 = CurrentDb.OpenRecordset("TBL_Registratie")
With rs1
.AddNew
!leerling_Id = Me.leerling_Id
datum = DateValue(!datum_tijd)
tijd = TimeValue(!datum_tijd)
weekDag = Weekday(datum, vbMonday)
Select Case weekDag
Case 1, 2, 4
Select Case tijd
Case "07:00:00" To "08:00:00"
!score = !score + 1
Case "16:00" To "16:30"
!score = !score + 1
Case "16:31" To "17:00"
!score = !score + 2
Case "17:01" To "22:00"
!score = !score + 3
Case Else
Me.txt_resultaat.Caption = "Het is geen opvang"
Me.txt_resultaat.Visible = True
( rs1.close ? )
End Select
other cases
.Update
.Close
QUESTION: How can i check if my last record in there recordset has a score added or not? if not, i want to delete it and close Else Update & Close
A much easier way: Calculate the score before adding the record, save all needed data in variables instead of recordset fields.
Then only if score > 0 do the rs1.AddNew etc.

Runtime Error 13 : Type mismatch

This is my first time coding from scratch and a very dumb question indeed.
Why do i keep getting this error?
Runtime Error '13' : Type mismatch
My value in B1 is an integer
Private Sub Workbook_Open()
xdate = Worksheets("sheet1").Range("A1")
lsdate = DateAdd("d", -7, Date)
'MsgBox lsdate
If Day(lsdate) > Day(xdate) Then
Worksheets("sheet1").Range ("B1") * 1 = result
Else
Worksheets("sheet1").Range ("B1") * 1.07 = result
End If
Range("c1").Value = result
End Sub
I have further edit my code to the following but not able to get the condition to work
'To create the following condition
'If less than 7 days interest = 0%
'if 8 to 30 days interest = 7%
'if more than 31 days interest = 9%
Sub Workbook_Open()
For i = 1 To 3 'Rows.Count
xdate = Cells(i, 1)
'MsgBox Cells(i, 1)
nulldate = DateAdd("d", -7, Date)
irate7late = DateAdd("d", -8, Date)
irate7early = DateAdd("d", -30, Date)
If Day(nulldate) < Day(xdate) Then
result = Cells(i, 2) * 1
ElseIf Day(irate7early) <= Day(xdate) And Day(xdate) <= Day(irate7late) Then
'30/9/2015 20/10/2015 20/10/2015 22/10/2015
result = Cells(i, 2) * 1.07
ElseIf Day(irate7early) > Day(xdate) Then
result = Cells(i, 2) * 1.09
End If
Cells(i, 3).Value = result
Next i
End Sub
You seem pretty new at programming, so I'll explain plainly :
When you are trying to assign a value to a variable in almost every language, you use :
variable = value
So you can do a lot of operations on the value, which is on the right of the equals =.
BUT you canNOT do operations on the left of the equals = when you simply assigning a value to a variable. You can do almost any operations when you are testing 2 variables but you'll have a keyword like If or While at the start of the line of code.
So the issue with your code is that you reversed the order of the value and the variable and tried to do value * 1 = variable
Here is your corrected code (I indented it so that it can be read easily) :
Private Sub Workbook_Open()
xdate = Worksheets("sheet1").Range("A1")
lsdate = DateAdd("d", -7, Date)
'MsgBox lsdate
If Day(lsdate) > Day(xdate) Then
result = Worksheets("sheet1").Range ("B1") * 1
Else
result = Worksheets("sheet1").Range ("B1") * 1.07
End If
Sheets("Sheet_name").Range("c1").Value = result
End Sub
And you should always reference the sheet you are working with, because previously, your code didn't specify on which sheet the C1 was supposed to be.
So here, just change Sheet_name to whatever the name of your sheet is!
variables need to get the values from sheet. You are trying opposite way which is why you getting error.
Private Sub Workbook_Open()
xdate = Worksheets("sheet1").Range("A1")
lsdate = DateAdd("d", -7, Date)
'MsgBox lsdate
If Day(lsdate) > Day(xdate) Then
result = Worksheets("sheet1").Range("B1") * 1
Else
result = Worksheets("sheet1").Range("B1") * 1.07
End If
Range("c1").Value = result
End Sub
Sub test1()
Dim var1 As Variant
' If you need to get values from the range("D1") Then use the below code
var1 = Worksheets("sheet1").Range("D1").Value
End Sub