How to get current code row number in vbs? - vbscript

For example, I have a next code:
1. Dim a
2. Dim b
3. Dim c
4. Dim currentRow
5. a = 5
6. b = 9
7. c = a + b
8. currentRow = ??? 'There i need to get code row
9. MsgBox currentRow
So I need to get 8

The language does not provide this kind of introspection/reflection. So your options are:
Use a different kind of unique location identifier (Keyword, description, global counter)
Preprocess your .VBS files to insert/replace the current line number where needed
Execute your code in the Microsoft ScriptControl and make use of the .Line property of the Error object

Related

VBA - SAP GUI Scripting - Active Row number

I am struggling with the code to read out the active row-number. I searched this forum (and others) and tried several things but I am not getting it running... I am doing a kind of find in my code, but then I will need the row number (actually the value of the 1st column) of the reference number I searched for.
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").RowCount
' this returns the total number of records which is perfectly fine
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").SetFocus
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").CurrentRow
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").VerticalScrollbar.Position
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").getAbsoluteRow
test1 = session.FindById("wnd[0]/usr/tblSAPLV60PTCTRL_KOPIEREN").CurrentCellRow
getting this error message:
run-time error 438 - Reference SAP GUI scripting is enabled
Please let me know what I am missing or doing wrong.
Thanks in advance for your guidance
Flooo
As far as I am aware you cannot directly get the row number of the selected row(s).
The basic approach I use is to loop all the rows and then check if they have been selected or not, then action from there, something like:
Dim objTable as Object
Dim objRow As Object
Dim numbRows As Long
Set objTable = session.findById("wnd[0]/usr/tblSAPLCMDITCTRL_3500")
With objTable
numbRows = objTable.RowCount
For r = 0 To (numbRows - 1)
Set objRow = .GetAbsoluteRow(r)
If objRow.Selected Then
' your action here ....
End If
Next r
End With
Remember; you need to be able to handle scrolling additional pages otherwise you will get an error

Looping error, too many records added

Ive been trying to write Access VBA code to automate the addition of replicates for germination tests.
Basically I have a form where I enter the total number of Reps (NoofReps) and the number of seeds per rep (RepSize) (e.g. 50 seeds). For each record added I want it to automatically add a record for each rep and automatically calc the Rep Number (i.e if i have 4 reps then it should add 4 records, numbered 1-4 reps) as well as the RepSize (e.g 50).
I have been trying out various loops based on information from this forum and other but am still getting errors with the number of records that it generates. I have tried both the "Do while" and "Do Until" but get the same result below either way.
Could someone please let me know where I am going wrong?...If i want 2 reps then it adds 2, If i want 3 then its 246, and if i want 4 it adds >30,000!!!
For the purposes of trying to fix the code I have started to type the number of reps manually into the code in the iNoofReps so that I know the error is in the code and not from the form.
Private Sub CmdAddReps3_Click()
Dim iRepNo As Integer ' stores the current value in the series
'Open the table
Set db = CurrentDb()
Set rstGReps = db.OpenRecordset("tblGReplicates")
' Initialise the variables
iRepNo = 1
iNoofReps = 3 'iNoofReps = Me.txtNoofReps
' Add the records using a loop
rstGReps.movefirst
Do 'Until rstGReps("RepNo") = (iNoofReps + 1) ' always want to include at least 1 repNo
rstGReps.AddNew
rstGReps("GTestID") = Me.GTestID
rstGReps("RepNo") = iRepNo
rstGReps("NoofSeed") = Me.txtNoOfSeeds
' Calculate the next RepNo value in the loop
iRepNo = iRepNo + 1
rstGReps.Update
rstGReps.moveNext
Loop Until rstGReps("RepNo") = (iNoofReps) + 1 ' so that the loop includes the final repNo.
MsgBox "Finished Looping"
rstGReps.Close
Set rstGReps = Nothing
Set db = Nothing
End Sub
Any help would be appreciated!!!
Well, you're moving next here: rstGReps.moveNext, and then you're comparing rstGReps("RepNo") = (iNoofReps) + 1 after moving next, thus being on an empty record, thus always equating to false.
Loop Until iRepNo = (iNoofReps) + 1 should fix it, then you're no longer referring to the recordset, which has already been set to the next record by the time you're referring to it.
You could also fix it by just eliminating this line:
rstGReps.moveNext
Since rstGReps.AddNew already moves the recordset to a new blank record, moving it forward after adding the record doesn't make much sense. If you remove it, you might want to remove the + 1 in Loop Until rstGReps("RepNo") = (iNoofReps) + 1

Modifying an Excel VBA Script to Work in Word

I have the following VBA code:
Sub test2()
Dim w1 As Worksheet
Dim w2 As Worksheet
Dim k As Long
Dim c As Range
Dim d As Range
Dim strFA As String
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
w2.Cells.Clear
k = 1
With w1.Range("A:A")
Set c = .Cells.Find("FirstThing", After:=.Cells(.Cells.Count), lookat:=xlWhole)
strFA = ""
While Not c Is Nothing And strFA <> c.Address
If strFA = "" Then strFA = c.Address
If IsError(Application.Match(c.Offset(0, 1).value, w2.Range("A:A"), False)) Then
Set d = .Cells.Find("SecondThing", c, , xlWhole)
w2.Range("A" & k).value = c.Offset(1, 0).value
w2.Range("B" & k).value = d.Offset(0, 1).value
k = k + 1
End If
Set c = .Cells.Find("FirstThing", After:=c, lookat:=xlWhole)
Wend
End With
End Sub
The code works essentially like this:
Look through Sheet1 for a certain phrase.
Once the phrase is found, place the value from the cell one row over in Sheet2
Search for a second phrase.
Place the value from the cell one row over in the cell beside the other value in Sheet2
Repeat
Now. I have the same data that, don't ask me why, is in .doc files. I'd like to create something similar to this code that will go through and look for the first phrase, and place the next n characters in an Excel sheet, and then look for the second phrase and place the next m characters in the row beside the cell housing the previous n characters.
I'm not sure whether it's better to do this with a bash script or whether it's possible to do this with VBA, so I've attached both as tags.
Your question seems to be: "I'm not sure whether it's better to do this with a bash script or whether it's possible to do this with VBA"
The answer to that is: You'd need VBA, especially since this is a *.doc file - docx would be a different matter.
In order to figure out what that is, start by trying to do the task manually in Word. More specifically, how to use Word's "Find" functionality. When you get that figured out, record those actions in a macro to get the starting point for your syntax. The code on the Excel side for writing the data across will essentially stay the same.
You'll also need to decide where the code should reside: in Word or in Excel. That will mean researching how to run the other application from within the one you choose - lots of examples here on SO and on the Internet...

Excel VBA, multi parameter filter to return row ID

I currently have an algorithm that manipulates data on sheet 24 in my excel workbook. I have 3 integers, a, b, and c, that come from data on sheet 24, and I need to use them to return a unique row ID on sheet 14. A, B, and C, each represent their corresponding Column (range) on sheet 14 IE: Int a from sheet 24 is in column A on sheet 14. Int b in column b, and c in c. The numbers combined will always return a singular row ID from 14, in the case, integer row. I'm having a hard time writing a statement with Filter, evaluate, and a bunch of other Excel functions. Does anyone know how to launch a script from an algorithm executing on one sheet and just pull a row ID in sheet 14 using its' resulting three search numbers for a,b,c?
row = Sheets("Physical Disk Details").Columns("A").Find(what:=a, LookIn:=xlValues, lookat:=xlWhole) * Sheets("Physical Disk Details").Columns("B").Find(what:=b, LookIn:=xlValues, lookat:=xlWhole) * Sheets("Physical Disk Details").Columns("C").Find(what:=c, LookIn:=xlValues, lookat:=xlWhole)
This Function will return the first row number that is found to exactly match what is provided for a,b, and c. It demonstrates one of the simplest Range iteration and If/Then tests that forms the foundation of most data pattern searching subroutines you will find for Excel VBA. There are ways to streamline this code but it demonstrates the basic principle.
If you read every line of this code and understand what all of it does you can use the principles to accomplish most tasks even vaguely similar to it.
Function GetRowNumberByABCMatch(a As Integer, b As Integer, c As Integer) As Integer
Dim lastRowInDataSet As Integer
Dim i As Integer
'get last row containing data in column 1
lastRowInDataSet = Sheets("Physical Disk Details").Cells(Rows.count, 1).End(xlUp).Row
'i = current row number, start at row 1
For i = 1 To lastRowInDataSet
If Sheets("Physical Disk Details").Range("A" & i).Value = a _
And Sheets("Physical Disk Details").Range("B" & i).Value = b _
And Sheets("Physical Disk Details").Range("C" & i).Value = c Then
'all fields match, return the current row number
GetRowNumberByABCMatch = i
'stop processing and return because we found a single match
Exit Function
End If
Next i
'didn't find a matching row, return -1
GetRowNumberByABCMatch = -1
End Function

VB: Declaring variables in For loop

I have been trying to declare variables inside a For loop for quite some time now, and I just haven't been able to find a way.
I'm attempting to create a new image (tile) for every time a certain number is encountered in a two-dimensioned array (Measuring 32x16). I may need to add in that I am using Visual Basic 6.
Currently I'm using the following code:
Option Explicit
Dim wCount As Integer
Dim hCount As Integer
Dim arrTiles(31, 15) As Integer
Private Sub Form_Load()
For wCount = 0 To 31 Step 1
For hCount = 0 To 15 Step 1
' -Declare variables
' -I.E. Dim NAME As Image
Next
Next
End Sub
However, the above code (Using Dim tile1 As Image) gives me an error whenever trying to access one of the properties of the newly added image (Such as tile1.Width).
Is there any way to declare a variable this way at run-time?
Sincerely
- Birjolaxew
You must assign a valid Image object to the Image variable before you try to access any properties of it. For example, this works fine:
For wCount = 0 To 31 Step 1
For hCount = 0 To 15 Step 1
' -Declare variables
Dim tile1 As Image
tile1 = Image.FromFile("c:/test.png")
Dim width = tile1.Width
Next
Next

Resources