OK thanks. Let me try it!
Posts by jerryexcel2
Please note that on 14th December 2023 users will experience an expected outage whilst we make upgrades to our network. We anticipate this process may take a couple of hours and so we apologise in advance for any inconvenience.
-
-
Thanks for the suggestion.
How do I adapt the array formula you posted to VBA?
Cell based formula wouldn't work for me since I have to call that function from another Sub.Many thanks!
-
Here my actually code, with a loop.
I just need the same thing without the loop since it really drags on speed.[VBA]Function rowTill(col As Integer, startBar As Long, target As Variant) As Integer
x = 0
j = startBar
Do Until dataSheet.Cells(j - x, col).Value = target
x = x + 1
Loop
rowTill = x
End Function[/VBA]
-
That works but I need a VBA version, without the excel formulas.
Searching the entire column maybe too slow since I have 500k+ rows!
Is there any way to search from the current cell up to the closest matched cell using VBA macro?Thanks!
Jerry -
Hi,
Suppose the current Cell is C1000, how do I find the closest cell (C600) that has a value of 20, without looping?
The direction would always be up, in the same column.
The return value would be the row number.Thanks!
Jerry
-
Re: VBA function to get MAX/MIN of a range offset
Hi Pike
Thanks for the tip and the fix!
Jerry
-
I want to use a User-defined function to find the max or min of a range that is also dynamic depending on the input variable.
Anyone know how this works?
CodeFunction X(Rng As Range, Period As Integer) X = Application.Max("Rng.Offset(-period, 0):Rng.Offset(-1, 0)") End Function
for instance,
for function X(A10, 5), should return MAX value of range("A5:A9")Many thanks!
-
Re: VBA function to find last non-zero value in a column
Batman,
The macro is periodically getting data and update them in the range that the formula uses.
I tested both with and without Application.Volatile, and it always gets #VALUE error.
I tested manually changing the values in the range and it didn't get the error.
the function is to be used from within the sheet, the macro has nothing to do with it, it simply gets data and update the range.Baraakhalil,
Yes, it works great now. I'll do some more test to see if there will be any problems.
And thank you both! You are both being very helpful!!
Best,
Jerry -
Re: VBA function to find last non-zero value in a column
Quote from BaraaKhalil;754427Try this
Code
Display MoreFunction LastNonZero(Rng As Range) Dim LastRow As Long, lCol As Long, X As Long, I As Long, Cell As Range For Each Cell In Rng.Columns If Cells(Rows.Count, Cell.Column).End(xlUp).Row > X Then X = Cells(Rows.Count, Cell.Column).End(xlUp).Row: lCol = Cell.Column Next Cell LastRow = Rng.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row For I = LastRow To 1 Step -1 If Cells(I, lCol) > 0 Then Exit For Next I LastNonZero = Cells(I, lCol).Address End Function
Hi,
This works as well.
However when I did some test, when I use a macro to get data from external sources, it returns the last non-zero value of the range of the ACTIVE sheet, which is not the sheet that has the data and the function.
Could you change the code a bit so that it specify which sheet is used for calculation.
Thanks a lot!! -
Re: VBA function to find last non-zero value in a column
Quote from Batman;754459Hi,
It might not make any difference to your application, but this is Microsoft's explanation:
If any of the values in your formula's range change, then the formula will recalculate without being volatile, so you might not need that. I would suggest you put the custom function alongside the standard LOOKUP (without the volatile statement) and test to make sure that they both update simultaneously.
There are a number of Excel's standard functions that are volatile, probably the main two that people might use and be unaware that they are volatile are OFFSET and INDIRECT. If you use lots of these formulas in your spreadsheets, be aware that they may update unexpectedly, and slow down your spreadsheet.
Hi,
Thanks for the explanation.
I did some test, it appears that whenever I use a macro to get data from external sources.
The custom function will go to #VALUE error, while the regular LOOKUP is fine.
To get out of that #VALUE error I have to change the value of any cell.Is there any way around this so I would not have to correct the error every time?
-
Re: VBA function to find last non-zero value in a column
Thanks, I never knew you could use Evaluate.
Could you also explain what Application.Volatile does?
thanks again! -
-
-
Re: Application.OnTime to repeat at fixed time
Quote from cytop;753565Unless I'm missing something, why not just add TIMEVALUE(“00:01:10") to Now()...?
Also, struggling to understand how 1m10s added to
9:12:30 is 9:13:10Hi,
Sorry I didn't explain it clearly.
I meant to say it should run at every minute + 10 more seconds,
so if now is 9:12:30, it should run 9:13:10
if now is 9:12:50, it should run 9:13:10
if now is 9:13:15, it should run 9:14:10 -
-
Re: Macro to copy two cells into one
OK, that works great, thanks a lot!
-
Re: Macro to copy two cells into one
Thanks, however that shows 78 instead of 7&8, I do need them to be separate.
-
Let's say A1 has value of 7, and B1 and value of 8,
I need a macro to copy both of them into a new cell of another sheet, let's say Sheet2!C1, and display it as 7&8.Is it possible?
Thanks a lot!
-
Re: Translate a complicated formula to VBA function
Now it works, I didn't put quotation marks around the cell.
MyUDF("C3") works but MyUDF(C3) doesn't.anyway, Thanks for your effort!
Jerry
-
Re: Translate a complicated formula to VBA function
Well, I actually tried it with an empty sheet, copy the VBA code to the developer, and randomly set 4 numbers in C3, C4, D3, D4, and result is error
I have Office 2007, not sure what could be the problem