I was trying to rename a chart with:
With ActiveChart
For i = 1 To Count_Rows
j = i + 1
.FullSeriesCollection(i).Name = """=Sheet1!$A$" & j & Chr(34)
Next i
...
this is resolving to desired formula as "=Sheet1!$A$2" and so on...
but not naming the series with contents instead putting the name of the series as "=Sheet1!$A$2"
You are not using a range object to return the cell values.
For i = 1 To Count_Rows
j = i + 1
.FullSeriesCollection(i).Name = Range("Sheet1!$A$" & j)
Next i
Related
I am struggling with a web page on a secure website. I will put a snapshot of what I am working on.
The XPATH that has the rows of a table (equals to 13 rows) is that
//div[#id='Section3']
But the data is not inside that XPATH but after it in 9 columns.
How can I refer to those children or ancestors ( I don't know the exact term)?
Here's the HTML for that page ( I couldn't include it in the question)
https://pastebin.com/hEq8K75C
Here's the snapshot (may clarify the issue well)
How to implement the variable j in such lines?
Dim x As Long, i As Long, j As Long
x = .FindElementsByXPath("//div[#id='Section3']")
For i = 1 To x
For j = 1 To 9
Cells(i, j).Value = .FindElementByXPath("//div[#id='Section3']/following-sibling::div[following-sibling::div[#id='Section3'][count(preceding-sibling::div[#id='Section3'])=" & i & " and count(following-sibling::div[#id='Section3'])=" & x - (i + 1) & "]][" & j & "]").Text
Next j
Next i
Try to use below code:
counter = len(driver.find_elements_by_id("Section3"))
xpath = "//div[#id='Section3']/following-sibling::div[count(preceding-sibling::div[#id='Section3'])={0} and count(following-sibling::div[#id='Section3'])={1}]"
for i in range(counter):
print('\nRow #{} \n'.format(i + 1))
_xpath = xpath.format(i + 1, counter - (i + 1))
cells = driver.find_elements_by_xpath(_xpath)
for cell in cells:
value = cell.find_element_by_xpath(".//td").text
print(value)
Below is my current Excel table:
I want to automatically generate the third column in yellow as a hierarchical view of all activities. I tried to solve this challenge with formulas but I am not sure that would be the best way to do it.
Has someone already faced and solved this requirement in Excel? Any advice/suggestions to guide me?
Many thanks and best regards!
You can paste this macro in the Worksheet's code Module and run it from there:
Sub CalculateHierarchy()
Dim rLevels As Range, rLevel As Range
Dim level As Integer, maxLevels As Integer, cur As Integer, i As Integer
Dim h As String, counts() As Integer
Set rLevels = Range("A2:A" & Range("A1").End(xlDown).Row)
maxLevels = WorksheetFunction.Max(rLevels)
ReDim counts(1 To maxLevels)
cur = 1
For Each rLevel In rLevels
level = rLevel.Value
If level > cur + 1 Then
rLevel.Activate
MsgBox "error at row " & rLevel.Row & " level increase by more than 1"
Exit Sub
End If
h = ""
counts(level) = counts(level) + 1
For i = 1 To level
h = h & "." & counts(i)
Next
h = Mid(h, 2)
For i = level + 1 To UBound(counts)
counts(i) = 0
Next
rLevel.Offset(, 2).Value = h
cur = level
Next
rLevel.Offset(0, 2).Interior.ColorIndex = 6
End Sub
I'm making an heuristic analyse and i have the fallowing problem : I want to find in column D numbers that match with column J and replace them by a "0". You can see what I'm trying to do on this image :
Problem : Column D have multiples values per cell and column J have one value per cell.
some part of the code:
Dim i,j As Integer
Dim temp As String
Dim x As Integer
Dim d As String
i = Application.CountA(Range("E:E")) + 10
'number of cell with values
j = Application.CountA(Range("J:J")) + 10
For j = 11 To j
temp = Range("J" & j).Value
For i = 11 To i
d = Range("D" & i).Value
*For x = LBound(vec) To UBound(vec)
If vec(x) = temp Then
vec(x) = 0
Range("D" & i).Value = vec(x)
End If
Next
Next
Next
*-> Here it is the problem, i cant figured out how to pass over the coma "," in column D,and store the data. I want to compare the temp with value on "d", but "d" can i have multiple numbers on the same cell, like " 3, 2, 1", and if there is any match like temp = 3, then d= "0,2,1".
English is not my native language so i hope you can understand what i want.
Thanks!
I think your almost there you just need to split up each cell and then search then recreate and replace the string in the cell. Please note I've not tested this.
Dim i,j As Integer
Dim temp As String
Dim x As Integer
Dim d As String
i = Application.CountA(Range("E:E")) + 10
'number of cell with values
j = Application.CountA(Range("J:J")) + 10
For j = 11 To j
temp = Range("J" & j).Value
For i = 11 To i
d = Range("D" & i).Value
Vec = split(d, ",") 'split the cell
d = "" 'clear the string
For x = LBound(vec) To UBound(vec)
If vec(x) = temp Then
vec(x) = 0
End If
d = d & vec(x) & "," 'recreate the string
Next
Range("D" & i).Value = left(d, len(d) - 1) 'save the string without the last ,
Next
Next
you can do this with the below formula - no need for VBA
Make a new column somewhere near column D.
In your new column, use this FIND and Substitute formula:
=IF(NOT(ISERROR(FIND(J:J,D:D))),SUBSTITUTE(D:D,J:J,"0"),D:D)
This formula looks for the value in column J within the cells in column D.
If a match is found, 0 is substituted for the found number. Otherwise, column D is returned.
If you want, you can hide column D to avoid confusion.
in VBA you have a string function called Split(vString, delimiter) that will split a string into tokens using the specified delimiter. See
MSDN Library: VB Split Function
examples:
Mr Spreadsheet John Walkenbach: Split Function examples
VB-Helper
Just out of curiosity ,I am asking you a question which is as below:
Suppose i do have an array A1(6)=(45,25,,36,88),A2(6)=(14,25,11),A3(6)=(11,21,20,25,48).Now can we put those array values with the help of a single statement like single array assignment to a row,as here all the rows to a range of an Excel, Say here "C1:R3" range.
EDIT
If I need to assign them to a row like R1<- A1 + A2 +A3,R2<- A1 + A2 +A3. Can you tell me how to this?
R1<- (45,25,,36,88),14,25,11,,,,11,21,20,25,48,) same for R2.
Thanks,
Dim A(2,5)
For i = 0 to 5
A(0, i) = A1(i)
A(1, i) = A2(i)
A(2, i) = A3(i)
Next i
Range("C1:R3").Value = A
EDIT
For second part, to the best of my understanding:
Dim R(17)
For i = 0 To 2
For j = 0 To 5
R(6 * i + j) = A(i, j)
Next j
Next i
Range("C5:T5").Value = R
EDIT 2
Alternatively:
Dim R
R = Split(Join(A1, ",") & "," & Join(A2, ",") & "," & Join(A3, ","), ",")
Range("C5:T5").Value = R
You can use any delimiter you like (if it's appropriate for your data).
I have A column that has values in random order like
A column
2
3
4
2
5
6
4
3
4
I want the row index of a particular number that occurred first. say if i say the number is 4 the value returned should be 3
I also want the row index of a particular number that occured last.say if i say the number is 3 then the value returned must be 8
I was thinking Vlookup or find function must do the task but unable to put them in order.please help me with these
My examples are looking for the number 3 but it is easy to adapt.
To find the first occurence, you can use:
=MATCH(3,A:A,0)
To find the last one, you can use an array formula (validate with Ctrl+Shift+Enter)
{=MAX(IF(A1:A10=3,ROW(A1:A10),0))}
Note that you could also have used an array formula for the first one with a MIN but it would be quite complicated for what it's worth.
Hi friend you may use macros to do this
Use the following code
Sub FindNumbers()
Sheet1.Range("B:D") = ""
Application.ScreenUpdating = False
tot = Sheet1.Range("A1048575").End(xlUp).Row
i = 1
k = 1
m = 1
n = 1
o = 1
p = 1
For i = 1 To tot
c = Application.WorksheetFunction.CountIf(Sheet1.Range("B:B"), Sheet1.Range("A" & i).Value)
If c <= 0 Then
Sheet1.Range("B" & k).Value = Sheet1.Range("A" & i).Value
k = k + 1
End If
Next
tots = Sheet1.Range("B1048575").End(xlUp).Row
For m = 1 To tots
For n = 1 To tot
If Sheet1.Range("B" & m).Value = Sheet1.Range("A" & n).Value Then
Sheet1.Range("D" & m).Value = n
End If
Next
Next
For o = 1 To tots
For p = 1 To tot
If Sheet1.Range("B" & o).Value = Sheet1.Range("A" & p).Value Then
Sheet1.Range("C" & o).Value = p
p = tot
End If
Next
Next
Application.ScreenUpdating = True
End Sub
How to use the code? (In case you are new to macro)
Open a new excel file
Press Alt + F11
Insert a new module
Paste the code into the module
Go back to the excel sheet and add a button
Assign the macro ‘FindNumbers’ to the button
Save excel in .xlsm format if you are using excel 2007 or 2010
Help
Column A: Enter your data in column A and click the button or run the macro
Press the button or run the macro
Your result will be like this
Column B: Numbers that are unique in the data entered in Column A
Column C: First occurrence of data
Column D: Last occurrence of data