Anyone know what is wrong with following code?:
I have extra parenthesis to compare the difference in value!
(Sheet1.Cells(i - 1, "H").Value - Sheet1.Cells(i, "H").Value) < 60 / 86400
Related
I have a table which looks like this:
What I am trying to do is to sum up a few values within a given datetime range. Let's say for example between 01.01.2016 00:30 and 01.01.2016 02:00.
My if clause looks like this.
If .Cells(i, 3).Value = "Samariter" And .Cells(i, 1).Value >= "01.01.2016 00:30:00" And .Cells(i, 1).Value <= "01.01.2016 02:00:00" Then
And if I run the algorithm, it runs correctly, BUT:
If I try, for example, the range from 25.1.2016 20:00 until 26.1.2016 02:00:
This is my if clause again:
If .Cells(i, 3).Value = "Samariter" And .Cells(i, 1).Value >= "25.01.2016 20:00:00" And .Cells(i, 1).Value <= "26.01.2016 02:00:00" Then
If I run this, it will start at the correct line, but will exceed the upper bound of the datetime interval.
What is the problem here?
Xabier's answer solved the problem, the if-clause should look like this in order for the algorithm to run properly:
If .Cells(i, 3).Value = "Samariter" And DateDiff("s", .Cells(i, 1).Value, "25.01.2016 00:00:00") < 0 And DateDiff("s", .Cells(i, 1).Value, "31.01.2016 23:59:59") >= 0 Then
Recently started working with VBA. Youtube and this forum have been excellent help so far. However, my problem is this:
Private Sub CommandButton1_Click()
Dim i As Integer
i = 1
Do While Sheet2.Cells(i, 1).Value <> 0
Sheet3.Cells(i, 1).Value = Sheet2.Cells(i, 1)
i = i + 1
Loop
Do While Sheet2.Cells(i, 10).Value <> 0
Sheet3.Cells(i, 4).Value = Sheet2.Cells(i, 10)
i = i - 1
Loop
Do While Sheet2.Cells(i, 6).Value <> 2
Sheet3.Cells(i, 3).Value = Sheet2.Cells(i, 6)
i = i + 1
Loop
End Sub
This script populates information in the correct sheet as well as the correct columns, at least until it encounters an empty cell from Sheet2. At this point is moves on to the next "Do While" instead of referring to the next non blank cell.
I've also encountered a Runtime Error 6- Overflow with this specific line:
Do While Sheet2.Cells(i, 6).Value <> 2
Sheet3.Cells(i, 3).Value = Sheet2.Cells(i, 6)
i = i + 1 <<<<----- ERROR??
I'm fairly certain that the Overflow Error is a result of Dim i as Integer vs. Dim i as String, but then again I've pretty much but working by trial and error, moving portions of script around and playing with expressions/functions.
As mentioned before I'm new to VBA. I'm also the kind of guy that learns by doing as well. I've looked all over different forums, youtube videos, etc. trying to create a script that works the way I want it to.
UPDATE
Thanks for the quick help and recommendations. I realized I didn't give near enough information in my first post.
1.) I don't need/want to leave an empty cell on sheet3. The script is now looping but not skipping over blanks if that makes sense?
For example:
doing need to do
101 101
102 102
104
104 105
105 106
106
I'm stil running off the end of the world as well so to speak. For some reason the Debug Function is bringing up this line
i = i + 1
2.) the line:
Do While Sheet2.Cells(i, 6).Value <> 2
Sheet3.Cells(i, 3).Value = Sheet2.Cells(i, 6)
is supposed to recognize a column of text not numbers. Not exactly sure if this is having an effect on anything
Thanks again everyone.
UPDATE 3:
I've got rid of the Error Messages and corrected all the expressions in the script. Also, I found that instead of using Do Until....... And........ had zero results. I had to go back to Do While and substitute an Or statement vs. the former.
So far the script is working better than I had expected. Thank You all for the help and insight.
Good news aside, I still need to figure out how to compose an "IF" statement so that the script will skip over blank cells in sheet2.cells(i, 1) and not import present values for that row in the 2 adjacent columns
Here is the current and running script:
Private Sub CommandButton1_Click()
Dim i As Long
i = 3
Do While (Not IsEmpty(Sheet2.Cells(i, 10))) Or (Sheet3.Cells(i, 4).Value <> 0)
Sheet3.Cells(i, 4).Value = Sheet2.Cells(i, 10).Value
i = i + 1
Loop
i = 3
Do While (Not IsEmpty(Sheet2.Cells(i, 1))) Or (Sheet3.Cells(i, 1).Value <> 0)
Sheet3.Cells(i, 1).Value = Sheet2.Cells(i, 1).Value
i = i + 1
Loop
i = 3
Do While (Not IsEmpty(Sheet2.Cells(i, 6))) Or (Sheet3.Cells(i, 3).Value <> 0)
Sheet3.Cells(i, 3).Value = Sheet2.Cells(i, 6).Value
i = i + 1
Loop
End Sub
Thank you again everyone. Hope I can return the favor in the near future.
A few thoughts:
As #dgorti said, make sure to set i before each Do loop to be the index of the row you want to start with.
Dim any integer value as Long, not Integer. Long is 32 bits; Integer is 16.
To skip over empty cells, use IsEmpty. For example, in your first loop: Edited
Do Until (Not IsEmpty(Sheet2.Cells(i, 1))) and (Sheet2.Cells(i, 1).Value = 0)
Edit And, as #A.S.H. points out, you should also range-check i. Since VBA doesn't have short-circuit operators, I would do that at the end of your loop:
Do ...
... 'vvvvv representable in a Long
If i = 65536 Then Exit Do ' Or If i = 1... for loops that count down
i = i + 1
Loop
That way you never run off the end.
Edit I fixed the test above — you want to run until you hit a non-empty cell with a value of 0, right? So Do Until (which is a real thing :) ) loops until exactly that condition holds. The Not IsEmpty() prevents the test against 0 from giving the wrong result on blank cells.
I'm a business student who just started to learn VBA. I am trying to write a macro for a project but only have minimal experience actually stepping into the code. What I am trying to do is delete all row entries which have future month's dates in them, and I'd like for this to update based on the current month. I hope I am using the correct functions, but maybe just in the wrong order.
I'm trying to compare the data for each row (in this case I'm looking at cell 16 in each) and I was thinking that if the month digits for the date in that column are greater than the value of the month digits for the current month, then it should delete, but I'm receiving the error [Run-time error '5' Invalid procedure call or argument].
So here is the part of the code I am having trouble with:
If DatePart(mm, Cells(iCntr, 16)).Value > DatePart(mm, Date).Value Then
Rows(iCntr).Delete
In the code, I was only focusing on the month portion because the file I'm using only contains current year information, so I wouldn't have to worry about accidentally failing to delete something for March of next year (03/2017) due to it being June of this year (for example, technically 03/13/2017 would not be deleted since 03 < 06).
(Second question for my own learning experience--someone suggested I use iCntr in this, but what does this actually do for the formula?)
Update: Went to Code Review and they updated my code and now the problem I am running into is that the friend who sent me the file left some rows of blanks that contain a single space, which is causing an error when I run the macro. Could someone suggest how to use the trim() function to eliminate those?
Sub Remove_excess_entries()
Application.ScreenUpdating = False
Dim lRow As Long
Dim iCntr As Long
lRow = 10000
For iCntr = lRow To 1 Step -1
If Cells(iCntr, 12).Value = "Mule" Or Cells(iCntr, 11).Value = "*R1*" Or Cells(iCntr, 11).Value = "*R2*" Or Cells(iCntr, 7).Value = "*Mule*" Or Cells(iCntr, 6).Value = "*Unassigned*" Or Cells(iCntr, 12).Value = "PS" Or Cells(iCntr, 7).Value = "Marketing" Or Cells(iCntr, 12).Value = "V1" Or DatePart("m", Cells(iCntr, 16).Value) > DatePart("m", Date) Then
Rows(iCntr).Delete
End If
Next
Application.ScreenUpdating = True
End Sub
iCntr isn't a VBA command, it's likely just a declared variable (possibly a counter value for iterating over the range of rows you want to consider, based on the context of the code shown). Presumably you have it declared somewhere and defined in a For type loop?
Secondly, according to MSDN the correct parameter for "month" in DatePart is "m", not mm. That means your code should read as:
If DatePart("m", Cells(iCntr, 16).Value) > DatePart("m", Date) Then Rows(iCntr).Delete
You also have the .Value outside the closing brackets, which means you are trying to assign it to the DatePart object rather than the Cells one. Feel free to ask anything further if I've not explained it well enough
as for the correct use of DatePart() function, type:
If DatePart("m", Cells(iCntr, 16)) > DatePart("m", Now) Then Rows(iCntr).Delete
I am having these codes with
cells(4,9)=0, cells(4,10)=1, cells(4,11)=0.01
in the sheet:
AF_from = Cells(4, 9).Value
AF_to = Cells(4, 10).Value
af_jump = Cells(4, 11).Value
For x = AF_from To AF_to Step af_jump
Cells(19, 8).Value = x
then the result is 0, 0.01,0.02,0.03...as expected until 0.83, then unexpected results 0.839999, 0.849999, 0.859999...1.999998
What should I do to fix this? Thanks.
First of all I wouldn't use a For loop using Double types. Explaining a little bit better: counters are ok when you use an IntegerorLong but you may encounter problems if you use fractions.
You could multiply your af_jump and AF_to (in this case by 100) and use those. Then write Cells(19, 8).Value = x/100.
You need also to be sure that all those variables have been set to their correct type. Especially the x which if not, will reproduce numbers as 0.84 like 0.8399999999. That is the "normal" way vba calculates. This is due to a finite bit precision.
https://stackoverflow.com/tags/floating-point/info
Assuming this is Excel, set the number format to two decimals.
I am trying to clean my data set in excel. Currently, my code deletes data before 7 am and after 10 pm. how do I make it 7:30 am?
current code:
If Hour(.Cells(Cell, 2)) < 7 Or Hour(.Cells(Cell, 2)) > 22
I want to make the 7 to 7:30. How would I do this?
Depending on what is the format of the data in your Cells(Cell, 2), if it is as in the Nowfunction you could do this:
'Example with `Now`
If CInt(Hour(.Cells(Cell, 2)) & Minute(.Cells(Cell, 2))) < 730 Or _
Hour(.Cells(Cell, 2)) > 22 Then
Hour() will always return and integer. Try converting the time to a string: Format(Range("H1"), "hhnn") for the comparison.
One way:
if TimeValue(.Cells(Cell, 2)) > #7:30:00 AM# Or TimeVa...