I have a set of data and i have a macro running adding a line each 5 seconds
This is linked to a pivot-table and a pivot-chart in a Dashboard
Looks like this (picture is my 2nd version with incremental instead of decremented)
I have a field in a hidden page where i put how many of the last logged line i want to see
For example i have 1000 lines and i want the dashboard graph, to the press of a button, toggle between "see only last X" or "see all"
I want to find a lighter way to do what i have done cause i see a big latency
I have achieved this by adding two columns in my data with a macro writing formula in them in a way that
Col D : give a value to the line
Col E : if the value of D > 0 write "last X logs" and the rest "before X logs"
I put this code for Col D
Settings!I4 = Field where i but the amount of logs i want Ex; 15
Settings!I5 = Is where i store the last row value to be used in the formula thus updating all the line at each added lines
setsheet.Range("I5").Value = lastrow
logsheet.Range("D" & lastfreerow).Value = ("=Settings!I4-(Settings!I5)") & "+" & setsheet.Range("I5").Text
This write the formula in the cell and look like this :
=Settings!I4-(Settings!I5)+7
the result is a count down where the most resent value is "15"
and the oldest goes in the negative
And put this code for Col E
logsheet.Range("E" & lastfreerow).Value = "=IF(RC[-1]>0,""Last"" & "" "" & Settings!R4C[4] & "" logs"",""Before the last"" & "" "" & Settings!R4C[4] & "" logs"")"
This write the formula in the cell and look like this :
=IF(D8>0;"Last" & " " & Settings!I$4 & " logs";"Before the last" & " " & Settings!I$4 & " logs")
And i use those those "values" in the slicer that sort my pivot-table
thank you
This is my version a bit simplified since there is only one column with a updating formula at each cycle. But i didnt see much improvement on the load of the macro
Before
'Create the IF formula that will give a value to the row, the older the row the smaller the value
setsheet.Range("I5").Value = lastrow
logsheet.Range("D" & lastfreerow).Value = ("=Settings!I4-(Settings!I5)") & "+" & setsheet.Range("I5").Text
'Create the IF formula that will say if the row is part of the desired range for the "last logs" option
logsheet.Range("E" & lastfreerow).Value = "=IF(RC[-1]>0,""Last"" & "" "" & Settings!R4C[4] & "" logs"",""Before the last"" & "" "" & Settings!R4C[4] & "" logs"")"
After
'Log#; simply give an incremental value to each row starting from 1
setsheet.Range("I6").Value = lastrow - 6 'Row value of the last log, put into the hidden setting page to be used in a formula
logsheet.Range("D" & lastfreerow).Value = (lastrow - 6) 'Row value of the last log,
'Create the IF formula that will say if the row is part of the desired range for the "last logs" option
logsheet.Range("E" & lastfreerow).Value = "=IF((Settings!R6C9 - Settings!R4C9) < (Log!RC[-1]),""Last"" & "" "" & Settings!R4C9 & "" logs"",""Before the last"" & "" "" & Settings!R4C9 & "" logs"")"
If you have any idea of a way to sort a Table in a way that the Chart show only the last X values im interested
Thank you for your time!
Related
I have two formulas that I need to transfer to VBA.
On Excel, my formula would be =countif(A$2:A2,A2) so I transferred that using this formula but everything is returning to 1. The rows didn't become dynamic and I want only the values to be displayed.
For a = 2 To lrow
ws.Range("T" & a).Formula = "=CountIf(A$2&"":""&A2)"",""&A2)"
Next a
Next formula that I use in Excel is
=IF(COUNTIF(A:A,A2)>Q2,"Check","Ok")
I tried this formula in VBA:
For i = 2 to lrow
If Countif(ws.Range("A2:A" & lrow), "A2") > ws.Range("Q2:Q", & lrow) Then
ws.Range("T" & i).Value = "Check"
Else
ws.Range("T" & i).Value = "Ok"
End If
Next i
You could populate column T with your first formula with this line of code:
ws.Range("T2:T" & lrow).FormulaR1C1 = "=COUNTIF(R2C[-19]:RC[-19],RC[-19])"
I can't advise on your second formula unless you clarify where you want to write it...
I have written a bit of code that copies non contiguous cells numbers from one sheet and pastes them in another sheet if their values do not exist in the new list. I have insert a row at the end of the list and then paste the missing value and repeat until all missing values have been added. When done I sort the column and apply formula in the adjacent columns to match the length of the newly expanded list.
I read in other questions that I can simply do
Range("A1:A13").Formula = "A1+B2"
and it will produce a column like
A1+B2
A2+B3
A3+B4
...
A13+B14
And that is great so that is what I did.
Then I discovered that I had issues with formulas like:
=IF(B2="","",A1+B2)
Which was fixed after some searching to produce
=IF(B2="""","""",A1+B2)
and I thought I hast that solved until I tried something like
=IF(B2="DOG","cat",A1+B2)
Which I initially tried
=IF(B2="""DOG""","""cat""",A1+B2)
which did not work and then figured out I had too many " and after some more research I adjusted it to
=IF(B2=""DOG"",""cat"",A1+B2)
So I thought I had this quote thing figured out, but it turns out I am getting lost in the quotes somewhere.
So my working formula in excel is:
=IF(C14="","",HYPERLINK(MID(CELL("filename",$A$1),FIND("[",CELL("filename",$A$1)),FIND("]", CELL("filename",$A$1))-FIND("[",CELL("filename",$A$1))+1)&"'"&C14&"'!$A$1",VLOOKUP(C14,ItemList,2,0)))
Basically I am generating a hyperlink to another worksheet. I have a variable that tracks the first row 14 in this case, so I tried code this into the formula so I could change the first row variable easily if it ever changed rather than going through all my hard coded formulas and updating them. As a result I wound up with the following line of code which keeps causing an error when the line is executed:
Dim Item_List_Sheet As Worksheet
Dim Item_List_First_Row As Long
Dim Item_List_Max_Row As Long
Item_List_First_Row = 14
Item_List_Max_Row = Item_List_First_Row + Application.WorksheetFunction.Max(Item_List_Sheet.Range("B:B")) - 1
Item_List_Sheet.Range("B" & Item_List_First_Row & ":" & "B" & Item_List_Max_Row).Formula = "=MAX($B$" & Item_List_First_Row - 1 & ":B" & Item_List_First_Row - 1 & ")+1"
'the above formula works as intended
'the formula below causes problems
Item_List_Sheet.Range("D" & Item_List_First_Row & ":" & "D" & Item_List_Max_Row).Formula = "=IF(C" & Item_List_First_Row & "="""","""",HYPERLINK(MID(CELL(""filename"",$A$1),FIND(""["",CELL(""filename"",$A$1)),FIND(""]"", CELL(""filename"",$A$1))-FIND(""["",CELL(""filename"",$A$1))+1)&""'""C" & Item_List_First_Row & "'!$A$1"",VLOOKUP(C" & Item_List_First_Row & ",ItemList,2,0)))"
Can someone see where I am going wrong?
UPDATE
I tried adjusting the formula as per GSerg comment that two & were missing and I still get the error message:
Item_List_Sheet.Range("D" & Item_List_First_Row & ":" & "D" & Item_List_Max_Row).Formula = "=IF(C" & Item_List_First_Row & "="""","""",HYPERLINK(MID(CELL(""filename"",$A$1),FIND(""["",CELL(""filename"",$A$1)),FIND(""]"", CELL(""filename"",$A$1))-FIND(""["",CELL(""filename"",$A$1))+1)&""'""&C" & Item_List_First_Row & ""&'!$A$1"",VLOOKUP(C" & Item_List_First_Row & ",ItemList,2,0)))"
I have some specific (different) numbers which will be the lead-time of the order to arrive to e.g. a warehouse, meaning that the lead-time is stochastic. I want the "ordered quantity" to be inserted "x" rows down at the "order arrival" column. For example, as seen in the photo, I want the "100" ordered quality to be inserted two rows down at "order arrival", and afterwards "120" to be inserted 5 rows down at "order arrival".
Is there a way to do that with a function in excel?Any ideas? Thanks 1
example
Hopefully the screenshot and code sample below will help you get started. I cannot think of a way to do what you want without using macros though I am sure there are some experts on here who may be able to come up with an amazing formula!
Please see the screenshot attached below. I have reformatted your table slightly for the purposes of demonstration, so now it looks like this:
I then wrote the following macro. Its a very rough-and-ready macro as Im at work and about to go home, but it should get you started:
Sub runorders()
Worksheets("Sheet1").Range("D2:D1000").ClearContents
readrow = 2
currentday = Worksheets("Sheet1").Range("A" & readrow).Value
currentleadtime = Worksheets("Sheet1").Range("B" & readrow).Value
currentorderquantity = Worksheets("Sheet1").Range("C" & readrow).Value
Do Until currentday = ""
If currentorderquantity <> "" Then
Worksheets("Sheet1").Range("D" & readrow + currentleadtime).Value = Worksheets("Sheet1").Range("D" & readrow + currentleadtime).Value + currentorderquantity
End If
readrow = readrow + 1
currentday = Worksheets("Sheet1").Range("A" & readrow).Value
currentleadtime = Worksheets("Sheet1").Range("B" & readrow).Value
currentorderquantity = Worksheets("Sheet1").Range("C" & readrow).Value
Loop
End Sub
Obviously for the code to work you will need the workbook set up exactly as I have done so in the screenshot. The cells highlighted in orange will need your normaldistribution formula. Don't forget that each time the macro writes the "order arrival" value into a cell in column d, the random distribution values will change (e.g. re-sample) unless you do something about it.
I hope this helps!
Paul
Can somebody help me to write this formula in excel VBA?
=IF(ISERROR(VLOOKUP(A3,Temp!$A$3:$A$595,1,FALSE)),A3,"0")
My code is getting stuck with :"syntax error"
Sub checkDuplitems()
Application.ScreenUpdating = False
Const top As Integer = 3
Dim bottom As Long
bottom = Sheets("Temp").Cells(Rows.Count, top).End(xlUp).row
With ThisWorkbook.Sheets("trash").Range("A" & top & ":A" & bottom)
.Formula = "=IF(ISERROR(VLOOKUP(A" & top & ",Temp!$B$" & top & ":$B$" & bottom & _
",1,FALSE)),A" & top & ", & '" 0" & ," '")"
.Value = .Value
.SortSpecial
End With
'Call something...
End Sub
You have a concatenation problem in the second line of the .Formula line.
To emulate the formula you have at the top of your question (which is wrong incidentally because you should be pointing to $B$3:$B$595 or something like that because your look up cell A3 should not be inside the VLOOKUP range).
Try this new .Formula line:-
.Formula = "=IF(ISERROR(VLOOKUP(A" & top & ",Temp!$B$" & top & ":$B$" & bottom & _
",1,FALSE)),A" & top & ", " & "0)"
Are you sure you want to use top as both the starting row in column A and the column to get the bottom row from the Temp worksheet? The important column on the Temp worksheet is column B (i.e. 2) not C (i.e. 3).
If you are putting formula(s) into Trash!A3:A595 that reference Trash!A3:A595 then these are circular references and cannot be resolved under normal conditions. I'll put the formulas into column Z.
If you are operating with Excel 2007 or newer then I would humbly propose this alternate that uses the worksheet's IFERROR function and does not attempt to make text out of the 0 returned value.
Const top As Integer = 3
Dim bottom As Long
bottom = Sheets("Temp").Cells(Rows.Count, "B").End(xlUp).Row '<~~change here
With ThisWorkbook.Sheets("trash")
With .Range("Z" & top, .Cells(Rows.Count, "A").End(xlUp).Offset(0, 25))
.Formula = "=IFERROR(VLOOKUP(A" & top & ", Temp!$B$" & top & ":$B$" & bottom & _
", 1, FALSE), 0)" '<~~ big change here
.Value = .Value
End With
End With
It is also curious as to why the number of rows of formulas in the Trash worksheet must be governed by the number of rows of data in the Temp worksheet. I would have thought that the number of values in column A of the Trash sheet should govern how many formulas go into the Trash worksheet.
I want to paste a list in the "InsertList" sheet. This list will only contain the word "Correct" or "False". From then on i need a way to search for the word "Correct" or "False" in the columnS P,Q,R,S,T,U,V.
e.g. If in the column "P" on the "InsertList" sheet the word "Correct" is found, i need that entire row from A to V to be copied onto it's destination, in this case "sheet1".
If the word "Correct" is found on the column "Q" on the "InsertList" sheet, the rows from A to V need to be copied in the Sheet2. And so on..
For i = 2 to Thisworkbook.Sheets(“ÏnsertList”).Range(“A64000”).End(xlup).row
If Thisworkbook.Sheets(“ÏnsertList”).Range(“P” & i).Value = “Correct” Then
Thisworkbook.Sheets(ÏnsertList”).Range(“A” & i & ":V" & i).Copy Thisworkbook.Sheets(“Sheet1”).Range(“A” & i)
ElseIf Thisworkbook.Sheets(“ÏnsertList”).Range(“Q” & i).Value = “Correct” Then
Thisworkbook.Sheets(ÏnsertList”).Range(“A” & i & ":V" & i).Copy
Thisworkbook.Sheets(“Sheet2”).Range(“A” & i)
End If
Next
Try using the above code in one of the modules