I have the If statement as below. If given values A and B as below, I wonder why the condition will be true in the end as the DateDiff will always return 0 and 0 is not larger than 100. I would appreciate for any comment.
A = ""
B = 100
If DateDiff("n", Me.A, Now()) > Val(Me.B) Then
End If
The datediff function returns the number of minutes since the start of the year 1900 which is a lot more than 100.
I couldn't get it to work with an empty string for A though
Related
I have a lua function to attempt to convert the time duration of the currently playing song e.g. hh:mm:ss to seconds.
function toSeconds (inputstr)
local mytable = string.gmatch(inputstr, "([^"..":".."]+)");
local conversion = { 60, 60, 24}
local seconds = 0;
--iterate backwards
local count = 0;
for i=1, v in mytable do
count = i+1
end
for i=1, v in mytable do
mytable[count-i]
seconds = seconds + v*conversion[i]
end
return seconds
end
in order to add it to os.time to get the estimated end time of a song.
but the hours may be missing, or the minutes may be missing on a short track.
When running against https://www.lua.org/cgi-bin/demo All I get is input:10: 'do' expected near 'in'
for the test script
function toSeconds (inputstr)
local mytable = string.gmatch(inputstr, "([^"..":".."]+)");
local conversion = { 60, 60, 24}
local seconds = 0;
--iterate backwards
local count = 0;
for i=1, v in mytable do
count = i+1
end
for i=1, v in mytable do
mytable[count-i]
seconds = seconds + v*conversion[i]
end
return seconds
end
print(toSeconds("1:1:1")
You're mixing up the two possible ways of writing a for loop:
a)
for i=1,10 do
print(i, "This loop is for counting up (or down) a number")
end
b)
for key, value in ipairs({"hello", "world"}) do
print(key, value, "This loop is for using an iterator function")
end
The first one, as you can see, simply counts up a number, i in this case. The second one is very generic and can be used to iterate over almost anything (for example using io.lines), but is most often used with pairs and ipairs to iterate over tables.
You also don't write for ... in tab, where tab is a table; you have to use ipairs for that, which then returns an iterator for the table (which is a function)
You're also using string.gmatch incorrectly; it doesn't return a table, but an iterator function over the matches of the pattern in the string, so you can use it like this:
local matches = {}
for word in some_string:gmatch("[^ ]") do
table.insert(matches, word)
end
which gives you an actual table containing the matches, but if you're only going to iterate over that table, you might as well use the gmatch loop directly.
for i=1, v in mytable do
count = i+1
end
I think you're just trying to count the elements in the table here? You can easily get the length of a table with the # operator, so #mytable
If you have a string like hh:mm:ss, but the hours and the minutes can be missing, the easiest thing might be to just fill them with 0. A somewhat hacky but short way to achieve this is to just append "00:00:" to your string, and look for the last 3 numbers in it:
local hours, minutes, seconds = ("00:00:"..inputstr):match("(%d%d):(%d%d):(%d%d)$")
If nothing is missing, you'll end up with something like 00:00:hh:mm:ss, which you only take the last 3 values of to end up with the correct time.
I'm trying to determine how many mondays there is between two dates.
These dates will be inserted by a datetimepicker
I know there are similar threads to this but they're all in PHP or VBScript.
Any ideas?
This post tells you how to do it in C#. Count number of Mondays in a given date range
I have run the code through a converter below. Have not tested it.
Private Shared Function CountDays(day As DayOfWeek, start As DateTime, [end] As DateTime) As Integer
Dim ts As TimeSpan = [end] - start
' Total duration
Dim count As Integer = CInt(Math.Floor(ts.TotalDays / 7))
' Number of whole weeks
Dim remainder As Integer = CInt(ts.TotalDays Mod 7)
' Number of remaining days
Dim sinceLastDay As Integer = CInt([end].DayOfWeek - day)
' Number of days since last [day]
If sinceLastDay < 0 Then
sinceLastDay += 7
End If
' Adjust for negative days since last [day]
' If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.
If remainder >= sinceLastDay Then
count += 1
End If
Return count
End Function
I have a big block of data >50 across and >1500 down and some of the entries are very large negative numbers like -1000000 or -9820000 and I want all of those to be turned into -100's.
I also want any non-zero numbers that are above -100 to show 2 decimals.
I thought this vba macro would work, but its causing excel to freeze and the excel screen turns all grey and idk whats happening.
I think it might be because there are so many cells, so it takes a long time and over loads something, is there any way to make this code more efficient??
Sub Blah()
For ColNum = 2 To WorksheetFunction.CountA(Range("1:1"))
For RowNum = 2 To WorksheetFunction.CountA(Range("A:A"))
If Cells(RowNum, ColNum) < -101 Then Cells(RowNum, ColNum) = -100
If Cells(RowNum, ColNum) <> 0 And Cells(RowNum, ColNum).Value > -100 Then Cells(RowNum, ColNum).NumberFormat = "0.00"
Next RowNum
Next ColNum
End Sub
Although Tmdean pointed out the solution, I'll post a sample code which might be of help.
Edit1: It seems assigning negative value in cell took a while as well. So apply the same principle. Get the relevant cells first and assign value in one go.
Sub marine()
Dim r As Range, c As Range, nonzero As Range, s As Range
Set r = ActiveSheet.UsedRange
For Each c In r
Select Case True
Case c.Value <= -101
'~~> Identify the cells first and combine all of them, don't assign value
If s Is Nothing Then Set s = c _
Else Set s = Union(s, c)
Case c.Value <> 0 And c.Value > -100
'~~> Identify the cells first and combine all of them, do not format
If nonzero Is Nothing Then Set nonzero = c _
Else Set nonzero = Union(nonzero, c)
End Select
Next
'~~> Once you got all the cells, assign value in one go
If Not s Is Nothing Then s.Value = -100
'~~> Once you got all the cells, format in one go
If Not nonzero Is Nothing Then nonzero.NumberFormat = "0.00"
End Sub
You can replace this with your For Loop, whatever is easier.
You can also be more explicit on setting the Range Object instead or using UsedRange. HTH.
There's nothing in that code that's particularly slow. How many cells are you changing? The only suggestion I have is to set the NumberFormat for the entire range beforehand (to 0.00), then turn set it back to General only for the cells that are 0 or -100 in your loop. Changing the NumberFormat is probably the most expensive operation, so you want to minimize the times you set it for individual cells.
Someone will be here shortly to recommend you turn off Application.ScreenUpdating.
Ok, so I was going to make a neat program that made your Number Lock, Caps Lock, and Scroll Lock keys flash. My problem is when I run it, it only produces one number, instead of multiple ones that are different, any ideas as to what the problem might be?
Set Keys = WScript.CreateObject("WScript.Shell") 'So The Script Can Press keys
Dim MAX, MIN 'Declaration
MAX = 2 'Sets MAX Equal To 2
MIN = 0 'Sets MIN Equal To 0
Randomize 'So The Numbers Are Different All The Time
Number = (Int((MAX-MIN+1)*Rnd+MIN)) 'Assigns The Random number To A Variable
Do Until X = 10 'Does The Loop 10 Times
Select Case Number
Case 0 'If Number = 0 Then The Following happens
WScript.Sleep 500 'Stops The Script For 500 Milliseconds
Keys.SendKeys("{NUMLOCK}") 'Pushes The Number Lock
Case 1 'If Number = 1Then The Following happens
WScript.Sleep 500 'Stops The Script For 500 Milliseconds
Keys.SendKeys("{CAPSLOCK}") 'Pushes The Caps Lock
Case 2 'If Number = 2 Then The Following happens
WScript.Sleep 500 'Stops The Script For 500 Milliseconds
Keys.SendKeys("{SCROLLLOCK}") 'Pushes The Scroll Lock
End Select
X = X + 1 'Increment
Loop
Posting comment as answer...
Well, for starters, you might want to put your equation within the loop so it regenerates the random number after every loop.
You need to create the random number in the loop:
Do Until X = 10 'Does The Loop 10 Times
Number = (Int((MAX-MIN+1)*Rnd+MIN)) 'Assigns The Random number To A Variable
Select Case Number
I feel like this is a really obvious bug but I really can't figure it out. It seems as if the comparison operators in VBScript aren't working properly. I'm writing a program in which the user enters an order amount, and this amount is compared to 1000. If I enter 200 for the order amt, the program outputs that 200 > 1000 = true.
Here is the code for my program:
largeOrderAmt = 1000
orderAmt = Inputbox("What is the order amount?")
Document.write(orderAmt & largeOrderAmt & (orderAmt > largeOrderAmt))
'Calculations
If orderAmt <= largeOrderAmt then
Document.write ("if statement called <br>")
...
Else
Document.write ("else statement called <br>")
EndIf
and here is the output:
200 1000True else statement called
I really don't get it. This is extremely frustrating. Any help would be appreciated, thank you!
That's because the values are compared as strings:
"200" > "1000" = True
Convert your input into a number using CInt or CDbl, e.g.:
orderAmt = CDbl(Inputbox("What is the order amount?"))