Wrong number of arguments or invalid property assignment in uft - vb.net

Thanks in Advance :) I can get the row count but not the column count using columncount method in uft 12.2. It simples throws an error at AccNoCol=AccNoTB.ColumnCountWrong number of arguments or invalid property assignment: 'AccNoTB.ColumnCount'. I know the column count is 8 here however they are dynamic & there is a risk to hard code the column count in script. Could you plz point out the correct? Thanks again
Set AccNoTB=browser("title:=.*").page("title:=.*").webtable("column names:=;Account No;Account Name;Billing City;Website;Phone;Assigned To;Action","cols:=8")
AccNoRow=AccNoTB.RowCount
AccNoCol=AccNoTB.ColumnCount
AccTBvalue=AccNoTB.GetCellData(AccNoRow,AccNoCol)
MsgBox AccTBvalue`

The column count of each row in the WebTable can differ therefore ColumnCount requires a parameter to specify to UFT which row's column count you're interested in.
A row can have
<table border=1>
<tr><td>One</td></tr>
<tr><td>or</td><td>two</td></tr>
<tr><td>or</td><td>even</td><td>more</td></tr>
</table>
Columns

Two simple ways to get Row and Column counts.
Note that I've removed "cols:=8" from object description, assuming the WebTablt is getting identified by column names.
Set AccNoTB=browser("title:=.*").page("title:=.*").webtable("column names:=;Account No;Account Name;Billing City;Website;Phone;Assigned To;Action")
Two ways to get Rows count
AccNoRow = AccNoTB.GetROProperty("rows") '<-- 1
AccNoRow = AccNoTB.RowCount '<-- 2
Two ways to get Column count
AccNoCol = AccNoTB.GetROProperty("cols") '<-- 1
iRow = 2
AccNoCol = AccNoTB.ColumnCount(iRow) '<-- 2 This way is useful when you have different columns in different rows.
Now lets take the example that #Motti have given. In this case we'll run a loop and get the column count.
Set AccNoTB=browser("title:=.*").page("title:=.*").webtable("column names:=;Account No;Account Name;Billing City;Website;Phone;Assigned To;Action")
AccNoRow = AccNoTB.RowCount
For i = 1 To AccNoRow
AccNoCol = AccNoTB.ColumnCount(i)
Print "Row " & i & " has " && " column/s."
Next
Output:
Row 1 has 1 column/s.
Row 2 has 2 column/s.
Row 3 has 3 column/s.

set a=Browser("OrangeHRM").Page("OrangeHRM_2").WebTable("micclass:=WebTable","html tag:=TABLE")
co=a.RowCount
MsgBox co
'set b=Browser("OrangeHRM").Page("OrangeHRM_2").WebTable("micclass:=WebTable","html tag:=TABLE")
col=b.ColumnCount
MsgBox col

Related

How to shrink the data from multiple columns into one column

hopefully someone will be able to help me. I need to write a query, which would shrink the data from multiple columns (in my case from columns A:H) into one column.
The original file looks like this:
I need to shrink the data one by one by rows. I mean, the query has to check the first row and take the data (name), and put it into "a new column" then check the second column and do the same, and continue like this one by one. The table has 170 rows.
I found a query that is shrinking the data from multiple columns into one column but in another order than I need. The query is taking as first all data from a column A and putting it into "a new column", then taking all data from a column B and putting it into "a new column" under the data from the previous column (column A).
This is the query I tried to apply:
Please could somebody help me with it? I have to admit that I have not use UBound and LBound functions and I am getting pretty lost here. :(
I will be thankful for any advise how to adjust this query.
Many thanks in advance! :)
Try this. I'm first setting your range to an array. I then loop through the array and 'slice' each row using Application.Index. It then Joins all the content in that row together before Trimming the whitespace left over from either end. This leaves me with the one value in my results array (tmp). The code then clears your source data before leaving all your data in one column.
Sub CombineColumns()
Dim rng As Range
Dim tmp As Variant, vaCells As Variant
Dim i As Long
Set rng = Sheets("DATA").Range("A2:H200")
vaCells = rng.Value2
ReDim tmp(LBound(vaCells) To UBound(vaCells))
For i = LBound(tmp) To UBound(tmp)
tmp(i) = Trim(Join(Application.Index(vaCells, i, 0)))
Next i
With rng
.ClearContents
.Cells(1).Resize(UBound(tmp)).Value2 = Application.Transpose(tmp)
End With
End Sub
LBound returns the lowest position in the array (usually 0 or 1) and UBound returns the highest
I think something like this
for i = 1 to 170
for y = 1 to 8
if worksheets("trainers").cells(i,y).value <> "" then
worksheets("output").cells(i,1).value = worksheets("trainers").cells(i,y).value
exit for
end if
next y
next i
or on same sheet
For i = 1 To 170
Z = 0
For y = 1 To 8
If Cells(i, y).Value = "" Then
Cells(i, y).Delete Shift:=xlToLeft
Z = Z + 1
If Z <= 8 Then y = y - 1
End If
Next y
Next i

How to find if the column exists in Excel through VBA

I have two headers in two rows in my excel based on which i upload values for each row. if the combination of two headers is already available, the values will be updated in that row. If that combination is not available a new column will be created.
Sample Excel
As in the image, If i find Column 2 and Column B combination again i can update value in a new row against Column 2 and Column B. If there is another combination of headers say, Column 5 and column B, then it will create a new column.
Using VBA i am able to check only one column header but not the combination of the headers. i used the below code.
Set c=ws.Range("B2",ws.Cells(2,Columns.Count)).Find:=What(d,1)
IF c is Nothing Then
My code
Else`My Code End If
Find can't be used to find values spread over several cells. I suggest this kind of code.
Private Sub FindMatchingColumn()
' 24 May 2017
Dim Caption1, Caption2
Dim Combination As String
Dim Captions As String
Dim C As Long
Caption1 = "Column A"
Caption2 = "Column 1"
Combination = CStr(Caption1) & CStr(Caption2)
With ActiveSheet
Do
C = C + 1
Captions = CStr(.Cells(1, C).value) & CStr(.Cells(2, C).value)
If StrComp(Captions, Combination, vbTextCompare) = 0 Then Exit Do
Loop While Len(Captions)
If Len(Captions) Then
MsgBox "Column " & C & " has matching captions"
Else
MsgBox "No matching captions were found" & vbCr & _
"Write new captions to Column " & C
.Cells(1, C).value = Caption1
.Cells(2, C).value = Caption2
End If
End With
End Sub
Caption1 and Caption2 are the two column headers you are looking for. The first part of the code loops through all the columns to find that combination. If it is found it passes the column where it was found to the following code. If not, it passes the number of the next blank column.
The second part takes that column number and acts upon it. If it is a used column you can add your value there. If it is a new column it adds the two captions and then you can add your values in it.
This code presumes that you know the sequence of the captions. If you need to accept either A + B or B + A both values must be checked before passing to the next column in the Do loop.

VBA(EXCEL) extract information from different rows with various criteria

I have an excel file which contain information of the composite in 1 row and in below rows information of the components of this composite. Number of component rows below a composite are varied between 2 - 20 and there can be many composites in a file.
My question is: is it possible somehow to define how many rows are in the components and to extract information from each component in to one cell(concatenate). Problem I face is that number of rows are different each time and there can be multiple composites in the file containing components. So i do not know how to stop my loop and start a new composite aggregation.
Maybe there are ways to loop from Request1(ColumnA) and assign "Request1" as a text to every empty column below until it reaches Request2, after that is finished to concatenate based on Request"n"
Example what i want the data to look like
~~~~~~~~~~~~EDIT~~~~~~~~~~~~~~~~~~~~
I might have over complicated my question
I was just looking to concatenate information from different set of rows(for simplicity just 1 consistent cell from every row) in to 1 cell(for example last cell in the first column) for each specific composite(which contains components) My problem is I do not know how to stop the concatenation and start a new one when i am working with a new composite(new set of rows).
So as an example from the first picture, I would like to have "Request 1 Green Yellow White" (cells: A1, F1, F2,F3) populated in cell J1, and "Request 2 Amber Red White Blue" (cells: A4,F4,F5,F6,F7)populated i cell J4
#######EDIT
I have established another way of doing but still struggle with concatenation formula.
In this picture example
https://i.stack.imgur.com/iQdNu.jpg
If my table stars from row 2
=IF(A2="",J1,A2) - by putting this in column J and dragging down i will get his Request 1
Request 1
Request 1
Request 2
Request 2
Request 2
Request 2
Then deleting duplicates i will be left only with
Request 1
Request 2
Then I can concatenate columns i want going by Request 1 or request 2 criteria(index match), but I cant figure out how to do it...
You can use array formula to work out the start and end rows, like =SMALL(IF($A$2:$A$20<>"",ROW($A$2:$A$20)),ROW()) to find the next populated cell in A1:A20, where this would be in the cell G1. So in G1, I have a fixed 1, then in G2 down, I have =H1+1, then in each H filled down I have =SMALL(IF($A$2:$A$20<>"",ROW($A$2:$A$20)),ROW()) this gives the following
Unfortunately we cant do the concat using what we have in Excel, so this will help with your loop start and ends. Number of products, is the difference in the 2
If I am reading your question correctly, then the following code may help. You want to be able to add element rows beneath categories rows, and when you do that it changes the row number for every row beneath the new row. This code will show you that it doesn't matter which row the category is on because you can find it's row number any time, and also the number of elements beneath it.
The trick is to add a word in col A of each category that will not be found in any element A value. For example, A1 might read "Category: Apples"," and there may be ten element rows under "Category: Apples" And then under those rows another category in col A will be "Category: Bananas." The code below looks for the value "Category:" in col A and gets the row number of each category line and how many elements are under it. With a little math you can figure out where to insert a new line for a new element row or what row to concatenate. And you won't need to hard code the rows numbers of the category. Just run this simple code and it will give you those row numbers to get and concatenate all rows beneath any category.
Sub findCategoryRows()
Dim lastRowColA As Long, myArray() As Variant
Dim rowOfCategoryNameArray, nameOfCategoryNameArray, categoryCounter As Long
lastRowColA = ActiveSheet.Range("A65536").End(xlUp).Row
myArray = Range("A1:A" & lastRowColA)
categoryCounter = 1
ReDim rowOfCategoryNameArray(1 To 1)
ReDim nameOfCategoryNameArray(1 To 1)
For i = 1 To UBound(myArray)
If InStr(1, Range("A" & i).Value, "Category: ") Then
rowOfCategoryNameArray(categoryCounter) = i
nameOfCategoryNameArray(categoryCounter) = Range("A" & i).Value
categoryCounter = categoryCounter + 1
ReDim Preserve rowOfCategoryNameArray(1 To categoryCounter)
ReDim Preserve nameOfCategoryNameArray(1 To categoryCounter)
End If
Next i
For i = 1 To UBound(rowOfCategoryNameArray) - 1
If i <> UBound(rowOfCategoryNameArray) - 1 Then
Debug.Print nameOfCategoryNameArray(i) & " has " & (rowOfCategoryNameArray(i + 1) - rowOfCategoryNameArray(i)) - 1 & " element rows under it."
Else
Debug.Print nameOfCategoryNameArray(i) & " has " & (lastRowColA - rowOfCategoryNameArray(i)) & " element rows under it."
End If
Next i
End Sub

Excel VBA: Finding the last row of a column full of duplicates, but NOT the end of the entire column

I have a large datasheet, where each row has a column that gives it a unique identifier, like so:
Row Identifier Value
1 A 3
2 A 4
3 A 7
4 B 1
5 B 3
6 B 0
7 C 9
I want to be able to find the row number of the last duplicated identifier in a range. So, for example, if the identifier "B" was some unknown number of rows long, and I wanted to find the number of the last row that had the column identifier "B," how would I do so?
So far I've come up with code to select the row of the FIRST identifier:
' Select first row where first instance of <B> exists:
With ActiveWorkbook.ActiveSheet
Set First_B_Row = .Range("A:A").Find(What:="B", LookIn:=xlValues)
End With
Dim First_B_RowNumber As Long
First_B_RowNumber = First_B_Row.Row
ActiveWorkbook.ActiveSheet.Rows(First_B_RowNumber).Select
But, I want to be able to find the row number of the LAST identifier in a column full of them. Any ideas?
You are absolutely close. Instead of searching top to bottom, then search in reverse like this:
With ActiveWorkbook.ActiveSheet
Dim Last_B_RowNumber As Long
Last_B_RowNumber = .Range("A:A").Find(What:="B", After:=[A1], _
SearchDirection:=xlPrevious).Row
End With
Above code will find the last occurrence of B in Column A and assigns it's row number to a variable.
Your just missing the SearchDirection argument.

excel macro filter, delete and sort

I want a create a macro that filters,deletes & sorts values.
Let there be 4 columns:
Serial No
Data
Amount
ID (Numeric)
The macro should
delete all rows having amount=0.
Reassign serial no because now there are fewer rows than before.
Sort the data according to ID
Unable to proceed with creation. Help will be greatly appreciated!
This seems to be a basic question, and it does not seem to need a universal solution, but rather 2 loops and then as sort call... Try looping (for or while) over the records to delete rows such as:
Sub macro1()
ColumnWithSerial = 1
ColumnWithID = 4
ColumnWithAmounts = 3
'loop over column with IDs
Dim i As Long, tempID As Long
i = 3 'variable to loop = set to 1st row with data (after header)
tempID = 1 'variable to update IDs
Do While Len(Sheets("Sheet1").Cells(i, ColumnWithID).Value) > 0
Sheets("Sheet1").Cells(i, ColumnWithSerial).Value = tempID
If Sheets("Sheet1").Cells(i, ColumnWithAmounts).Value = 0 Then
Sheets("Sheet1").Cells(i, ColumnWithAmounts).EntireRow.Delete
Else
tempID = tempID + 1
End If
i = i + 1
Loop
'now call sort method (easy to record and then adapt) (make sure to use sort and not filter)
End Sub
This should generally do the job, unless there is some parameter that needs specifying...