I've been up all night trying to figure this one out. I have a value derived from a formula in cell I6. This value represents the number of buy orders for a particular stock. I designed the spreadsheet so it only display 50 lines for buys and 50 lines for sells. When the value I6 exceeds 50, I want to display a message saying that "The number of buys )or sells) cannot exceed 50." I would like for this macro to run automatically anytime the value exceeds 50.
I should point out that I've never written a VB macro before, but I'm essentially fearless (as well as clueless).
Two ways you can do this depending on how exactly you want to "Display a Message".
You could put a formulae in cell that checks the value of cell I6 something like:
=IF(I6 > 50, "The number of buys / sells cannot exceed 50", "")
for this you would need an area where the message can display - you can even put some conditional formatting on the cell to display red if it contains any text.
Similar to the above, but doesn't rely on an area on your spread sheet and uses a VBA Macro.
a) Put the below forumlae in a cell (anywhere will do) not visible
=IF(I6 > 50, ToManyBuySells(),"")
b) add a module to the VBA project and add the following code.
Function ToManyBuySells()
MsgBox "The number of (buys) or (sells) cannot exceed 50", vbExclamation + vbOKOnly, "Buy/Sell Exceeded"
ToManyBuySells = ""
End Function
This function will run when I6 goes over 50 and display the message.
Hope this helps
Related
I have been leveraging for the past several months a couple of lines of code in VBA which with the help of the stackoverflow community I was able to adjust as needed (link Looping and scanning a list of e.g., 200 strings against a column with string data (Excel, VBA, Macros)). Essentially, the code scans row by row through a list of pre-defined keywords against a range of data highlighting possible hits in an adjacent empty column/cell. For example, if my range/column contained "DOG ABC LLC" and my keyword list/array contained "ABC" the macro helped easily highlight the hit in another column by displaying it.
I have noticed one issue with this method that often more than one keyword hit could occur. For example, I can have an array containing both "ABC" and "DOG" as separate keywords. The current loop in place sadly only factors in the first hit apparently and then moves on. I was wondering whether there is an easy way of adjusting the code so that one could add all possible hits into a cell after a comma or space. Therefore instead of seeing just "DOG" or "ABC", one could clearly see that there were 2 hits "ABC , DOG". Here is the code I have been using thus far:
Dim wordsArray() As Variant
wordsArray = Worksheets("Keywords").Range("B2:B439").Value
Dim word As Variant
Dim cell As Range
For Each cell In Worksheets("Normalized").Range("J2:J49010")
For Each word In wordsArray
If InStr(cell.Value, word) > 0 Then
cell.Offset(0, -1).Value = word
End If
Next word
Next cell
Thank you in advance for advice!
I've created a Time Sheet for my dog walkers to fill in. They enter TIME STARTED and TIME ENDED for each of their dog walks and then a TOTAL TIME is generated using the =X-Y formula. This TOTAL TIME is formatted to be displayed in hh:mm but its true VALUE is a long integer.
Using the MATCH and INDEX functions, I've set up a formula to match the TOTAL TIME value generated to an index on the "Payment Schedule" sheet and locate the respective payment.
I keep getting errors that state the total time VALUES can't be matched but I know that formatting isn't the issue and the values are clearly on the "Payment Schedule" sheet. And when the MATCH function does return a row it returns the incorrect row, which locates the incorrect payment/rate.
Here is the Google Sheets I'm having trouble with.
Found the answer on another website:
it's floating point rounding error on the time values. I don't fully
understand what's happening, but I was able to reproduce it on my
system, and I found a work-around: Change your formula to
=INDEX(B:B, MATCH($I$4+TIME(0,0,1), A:A)) This adds one second (TIME(0,0,1); the arguments are TIME(hours,minutes,seconds)) to the I4
value; that seems to be enough to get it "over the hump", so that it
tests as being ≥ the value in A4 (or A7 or A10). BTW, I tried
TIME(0,0,0.9), but apparently TIME() won't honor fractional seconds,
and so it just treats that as TIME(0,0,0); i.e., just plain zero. If
you want to get a millisecond, you can use TIME(0,0,1)*0.001.
link
In reference to the picture below, I would like to loop through a certain column (Column D in this case) until I hit a specific cell (Yellow cells in this case). In my final spreadsheet I have multiple yellow cells that I would like to target. Once I hit a yellow cell, I would like to start a simple summation of the values one cell to the left of the yellow (Column C). I would like to keep summing the values until I hit a blank cell, which would indicate the end of the set.
Please let me know if you need any more clarification!
Here's some code that should get the job done. However you are going to have to adapt it to however you want to use it.
Dim Summation as Double
For Each Target in Range("D:D")
If Target.Interior.ColorValue = 6 Then
Summation = Summation + Target.Offset(0, -1).Value
End If
Next Target
I hope this helps. However, don't forget about FreeMan's suggestions about good question asking and using the macro recorder!
I am trying to write a simple macro to display a pop-up (vbOKOnly) if the value in a cell exceeds a certain value.
I basically have a worksheet with products and discounts. I have a formula in one cell, say A1, that shows the discount as a percent (50% or .5) effective discount of all the entries.
What I'm looking for is code to display a message box if the value of cell A1 exceeds say 50%, because the input of another cell pushed the discount over 50%.
Thanks!
You could add the following VBA code to your sheet:
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A1") > 0.5 Then
MsgBox "Discount too high"
End If
End Sub
Every time a cell is changed on the sheet, it will check the value of cell A1.
Notes:
if A1 also depends on data located in other spreadsheets, the macro will not be called if you change that data.
the macro will be called will be called every time something changes on your sheet. If it has lots of formula (as in 1000s) it could be slow.
Widor uses a different approach (Worksheet_Calculate instead of Worksheet_Change):
Pros: his method will work if A1's value is linked to cells located in other sheets.
Cons: if you have many links on your sheet that reference other sheets, his method will run a bit slower.
Conclusion: use Worksheet_Change if A1 only depends on data located on the same sheet, use Worksheet_Calculate if not.
Essentially you want to add code to the Calculate event of the relevant Worksheet.
In the Project window of the VBA editor, double-click the sheet you want to add code to and from the drop-downs at the top of the editor window, choose 'Worksheet' and 'Calculate' on the left and right respectively.
Alternatively, copy the code below into the editor of the sheet you want to use:
Private Sub Worksheet_Calculate()
If Sheets("MySheet").Range("A1").Value > 0.5 Then
MsgBox "Over 50%!", vbOKOnly
End If
End Sub
This way, every time the worksheet recalculates it will check to see if the value is > 0.5 or 50%.
I don't think a message box is the best way to go with this as you would need the VB code running in a loop to check the cell contents, or unless you plan to run the macro manually. In this case I think it would be better to add conditional formatting to the cell to change the background to red (for example) if the value exceeds the upper limit.
I have one sheet which is comprised of data that was imported from a web page. In a second sheet I have a cell asking for min price and max price which comes from the imported data. The issue is that the data can move to different cells when it's refreshed so my code needs to look for some specific wording to find the prices that I need rather than just directing it to the same cell each time
The info in the imported data sheet which I need to extract from will always be in this string of text which will always be found somewhere between row 15-35 but will move when the website is refreshed. The 1st number (71.00) needs to be extracted to another sheet in a cell asking for min price while the second number (75.00) is the max price. These prices can change so I can just look for those numbers and extract them.
SLAUGHTER BULLS: Yield grade 1-2 1000-1500 lbs
1500-2000 lbs 71.00-75.00.
Could someone please help me with the coding for this macro?
Use the InStrRev function (similar to the LastIndexOf function in .NET) to find the last index of "lbs" in the test string.
lastIndex = InStrRev(testString, "lbs", 1, 1)
Then search from that last index to the end of the string spliting on the -
splitArray = Split(Mid(testString, lastIndex, Len(testString) - lastIndex), "-", 1)
The lowest value will be in splitArray(0) and the upper value will be in splitArray(1) assuming a zero indexed array.