find month names withing for loop - vba

It returns only saturday, for the dates it was simple because I only needed to add a plus 1 but how to i get it to loop from 1 to 7? Also how about for the months names as well
Sub netprofit()
Dim dates As Date
Dim count As Integer
Dim olweekdayname As String
olweekdayname = WeekdayName(Weekday(dates))
dates = DateSerial(2014, 9, 1)
For count = 2 To 366
Select Case Weekday(dates)
Case 1, 7
Range("B" & count) = 0
Range("C" & count) = 0
Range("D" & count) = 0
Range("E" & count) = olweekdayname
Case Else
Range("C" & count) = Int((100 - 0 + 1) * Rnd + 0)
Range("D" & count) = Range("B" & count) - Range("C" & count)
Range("A" & count) = dates
Range("E" & count) = olweekdayname
End Select
dates = dates + 1
Next
End Sub

this line olweekdayname = WeekdayName(Weekday(dates)) must be inside of the loop, otherwise olweekdayname will not be changed, so your updated code is below, tested, works fine.
Sub netprofit()
Dim dates As Date
Dim count As Integer
dates = DateSerial(2014, 9, 1)
For count = 2 To 366
Select Case Weekday(dates)
Case 1, 7
Range("B" & count) = 0
Range("C" & count) = 0
Range("D" & count) = 0
Range("E" & count) = WeekdayName(Weekday(dates))
Range("F" & count) = MonthName(Month(dates))
Case Else
Range("C" & count) = Int((100 - 0 + 1) * Rnd + 0)
Range("D" & count) = Range("B" & count) - Range("C" & count)
Range("A" & count) = dates
Range("E" & count) = WeekdayName(Weekday(dates))
Range("F" & count) = MonthName(Month(dates))
End Select
dates = dates + 1
Next
End Sub

Related

how to extract data using Xpath in Vba

i want to extract table data from each rows, but it doesn't work.
it work when i change tr to tr[2], else extract only blank value
Dim driver As New WebDriver
Dim Url
Dim Rows As WebElements
Dim Value as Webelement
Set Rows = driver.FindElementsByXpath("/html/body/div[3]/div[2]/div/form/div[3]/div/div[3]/div[3]/div/table/tbody/tr")
cnt = 1
For Each Value In Rows
Range("a" & cnt) = Value.FindElementByXPath("td[8]").Text
Range("b" & cnt) = Value.FindElementByXPath("td[7]").Text
Range("c" & cnt) = Value.FindElementByXPath("td[10]").Text
Range("d" & cnt) = Value.FindElementByXPath("td[11]").Text
Range("e" & cnt) = Value.FindElementByXPath("td[18]").Text
Range("f" & cnt) = Value.FindElementByXPath("td[19]").Text
Range("g" & cnt) = Value.FindElementByXPath("td[20]").Text
cnt = cnt + 1
Next
Dim col_n as long
for i = 1 to NumCols
if Cells(1, i).Value = "column header you are looking for" Then col_n = i
next
for i = 1 to NumRows
Cells(i, col_n).Value = "PHEV"
next i

Counting the columns in sheet1 and pasting their result in a table in sheet 2

I have two sheets. sheet1 is Evaluation and sheet2 is the result sheet.
From the Evaluation sheet1, I would like to count the number of "OK" in Column S and T and count the number of "Invalid" in the column R.
These counted values are to be entered In the sheet2 result , in their corresponding columns.
I was working with this code and It worked completely fine.
But now, when I run the code, number of OK in the column S and T and number of Invalid in the column R are not counted.
Here is the Code I am trying to work with
Sub result()
Dim i As Integer
Dim j As Integer
Dim cnt As Integer
Dim cntU, CntS, cntT As Integer
Dim sht As Worksheet
Dim totalrows, n As Long
Set sht = Sheets("Result")
Sheets("Evaluation").Select
totalrows = Range("A5").End(xlDown).Row
n = Worksheets("FC_SCR Evaluation").Range("A5:A" & totalrows).Cells.SpecialCells(xlCellTypeConstants).count
For i = 2 To WorksheetFunction.count(sht.Columns(1))
cntT = 0
cntU = 0
CntS = 0
' get the current week of column A of result sheet
If sht.Range("A" & i) = Val(Format(Now, "WW")) Then Exit For
Next i
' if column A of result sheet and Column x of fc sheet are same, then count the mentioned parameters
For j = 5 To WorksheetFunction.CountA(Columns(23))
If sht.Range("A" & i) = Range("X" & j) And Range("T" & j) = "OK" Then cntT = cntT + 1
If sht.Range("A" & i) = Range("X" & j) And Range("S" & j) = "OK" Then cntU = cntU + 1
If sht.Range("A" & i) = Range("X" & j) And Range("R" & j) = "Invalid" Then CntS = CntS + 1
' print the counted value in corresponding column of result sheet
If cntT <> 0 Then sht.Range("D" & i) = cntT
If cntU <> 0 Then sht.Range("E" & i) = cntU
If CntS <> 0 Then sht.Range("C" & i) = CntS
If n <> 0 Then sht.Range("B" & i) = n
Next j
If cntT + cntU <> 0 Then
sht.Range("F" & i) = CntS / n
sht.Range("g" & i) = cntT / n
sht.Range("h" & i) = cntU / n
End If
End Sub

How to dynamically change range inside formula?

I’m applying a formula:
Textual representation of formula:
=(SUBSTITUTE((LEFT(A2;(FIND("htt";A2;1))-3));";";";"))&RIGHT(A2;(LEN(A2)-(FIND("htt";A2;1))+3))
to all cells in a range A2:A10, writing a result to range B2:B10 respectively.
To do this I use the following macro:
Sub ColumnsFormat()
Dim s As String
Dim i As Integer, j As Integer
'Set wb = Workbooks("CSV_File.xlsm")
Application.ScreenUpdating = False
j = 1
For i = 2 To 10
s = "=(SUBSTITUTE((LEFT(R[" & Trim(Str(i)) & "]C[" & Trim(Str(j - 2)) & "],(FIND(""htt"",R[" & Trim(Str(i)) & "]C[" & Trim(Str(j - 2)) & "],1))-3)),"","","";""))&RIGHT(R[" & Trim(Str(i)) & "]C[" & Trim(Str(j - 2)) & "],(LEN(R[" & Trim(Str(i)) & "]C[" & Trim(Str(j - 2)) & "])-(FIND(""htt"",R[" & Trim(Str(i)) & "]C[" & Trim(Str(j - 2)) & "],1))+3))"
Sheets("Sheet1").Cells(i, 2).Value = s
Next i
End Sub
The problem is that for some reason a row number inside the formula inside a For cycle is wrong. Instead of taking A2; A3; A4 … A10 cells, (changing row number by 1 each time), macro runs through A2; A4; A6 etc. (increasing a row number by 2 each time).
What am I doing wrong?
By changing the row in the formula to 0 the code works just fine. I guess the problem is that in your formula the row was calculated relative to the specific cell the formula was afterwards applied to. Therefore the formula in B2 looked at A(2+2), in B3 at A(3+3) and so on.
Sub ColumnsFormat()
Dim s As String
Dim i As Integer, j As Integer
'Set wb = Workbooks("CSV_File.xlsm")
Application.ScreenUpdating = False
j = 1
For i = 2 To 10
s = "=(SUBSTITUTE((LEFT(R[" & 0 & "]C[" & j - 2 & "],(FIND(""htt"",R[" & 0 & "]C[" & Trim(Str(j - 2)) & "],1))-3)),"","","";""))&RIGHT(R[" & 0 & "]C[" & Trim(Str(j - 2)) & "],(LEN(R[" & 0 & "]C[" & Trim(Str(j - 2)) & "])-(FIND(""htt"",R[" & 0 & "]C[" & Trim(Str(j - 2)) & "],1))+3))"
Sheets("Sheet1").Cells(i, 2).Value = s
Next i
End Sub

Macro to group data by month

I'm trying to create a macro that will group data by month: for example, if there are 3 entries for February for client A, then it will consolidate the three entries into one and sum the amounts.
I have:
A: Client name
B: Invoice number
C: Billing month
D: Currency
E: Charge amount
F: Invoice step
What I am trying to do, is group the following into a single row, with the invoice amounts added up, and replace the three rows with just the one row. These entries are for a single client (so the grouping depends on the value in column A).
EG. Client A has three entries for Jan, Client B has one, Client C has one. Then for February Client A has one, Client B has one and Client C has two.
Any macro suggestions I've seen on here haven't been helpful to me, I keep getting errors coming up so I don't know what the problem is. This is the one I tried:
Sub Group()
Dim e As Range, a as Range
Set e = Range("C6")
Set c = e.Offset(, 2)
Do
If Evaluate("=month(" & e.Address & ")") <> Evaluate("=month(" & e(2).Address & ")") Then
e(2).Resize(2, 3).Insert
e(2).Offset(, 2) = "=sum(" & Range(a, c.Offset(, 2)).Address & ")"
e(2).Offset(, 2).Font.Bold = 1
Set e = e.End(4)
Set c = e.Offset(, 2)
Else
Set e = e(2)
End If
Loop Until e.End(4).Row = Rows.Count
e(2).Offset(, 2) = "=sum(" & Range(c, e.Offset(, 2)).Address & ")"
e(2).Offset(, 2).Font.Bold = 1
End Sub
EDIT: Subtotals and pivot tables would not work - the raw data is pasted in (thousands of lines of entry, for 40+ clients and some have many invoices each month), the raw data is sorted using a macro, which is then pasted into other sheets. Pasting from the pivot would be more difficult.
I follow your image to write the code:
After the code:
Here is the code:
Sub TEST()
Dim lastrow As Long
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For i = 6 To lastrow
lastrow = Range("A" & Rows.Count).End(xlUp).Row
For j = i + 1 To lastrow
If Range("A" & j) = Range("A" & i) And Range("C" & j) = Range("C" & i) Then
Range("B" & i) = Range("B" & i) & "," & " " & Range("B" & j)
Range("E" & i) = Range("E" & i).Value + Range("E" & j).Value
Rows(j).EntireRow.Delete
End If
Next j
Next i
End Sub

Nested Loops and If statements

So the code I have below attempts to find WIP in column H. If we find WIP: copy 3 cells and make 10 replicas of them in the next column either in the same row or the next available row.
For some reason the code only runs the loop successfully for the first "WIP" value and then stops running. Can someone see why this keeps happening?
Thank you,
Ori
Sub Step1_update()
Dim dblSKU As Double
Dim strDesc As String
Dim strType As String
Dim BrowFin As Integer
Dim Browfin1 As Integer
Dim Counter As Integer
Dim Trowfin As Integer
Counter = 0
Worksheets("Final").Activate
Trowfin = 5
BrowFin = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
'loop 1
Do While Trowfin < BrowFin
'If 1
If Range("H" & Trowfin).Value = Range("H3").Value Then
dblSKU = Range("F" & Trowfin).Value
strDesc = Range("G" & Trowfin).Value
strType = Range("H" & Trowfin).Value
Browfin1 = (ActiveSheet.Range("J" & Rows.Count).End(xlUp).Row)
'If 2
If Browfin1 > Trowfin Then
Do While Counter < 15
Range("J" & Browfin1).Value = dblSKU
Range("K" & Browfin1).Value = strDesc
Range("L" & Browfin1).Value = strType
Counter = Counter + 1
Browfin1 = Browfin1 - 1
Trowfin = Trowfin + 1
Loop
ElseIf Browfin1 < Trowfin Then
Do While Counter < 15
Range("J" & Trowfin).Value = dblSKU
Range("K" & Trowfin).Value = strDesc
Range("L" & Trowfin).Value = strType
Counter = Counter + 1
Trowfin = Trowfin + 1
Loop
Else
Do While Counter < 15
Range("J" & Trowfin).Value = dblSKU
Range("K" & Trowfin).Value = strDesc
Range("L" & Trowfin).Value = strType
Counter = Counter + 1
Trowfin = Trowfin + 1
Loop
'If 2
End If
Else
Trowfin = Trowfin + 1
'If 1
End If
counter = 0
'loop 1
Loop
End Sub
You probably need to reset your counter back to 0 after you loop through it. Otherwise you are already at the 15 the next time you get to the loop again.