Basic Macro

infuse

Board Regular
Joined
Dec 7, 2005
Messages
126
Hello
I am a beginner in VBA world. Well, you can tell this by looking at my code.
I highly appreciate any help regarding this. Thanks in advance for the help.
I am trying to run the code below and it gives an error (This is the error “Loop without Do”)
Sub FirstProgram()
Dim A As Date
Dim B As Integer
Dim i As Integer
Do While i = 5
A = Ai
B = Weekday(A)
If 1 < B < 7 Then
Ji = Fi
i = i + 1
Loop Until i = 428
End Sub
I wanted to check the Value of “A” column (A contains dates) and if it is a weekday I wanted to copy the contents of F column to J column. I also want one more check before copying the Value from F to J column. I think I will do this later. Can anyone tell me why the above code is not working? Please don’t write the code for me. Please let me know what I am missing. Also keep in mind that I am new to this and this is my first program. This might be simple using the Excel functions and commands, but I wanted do this by writing a code.
Thanks in Advance
Infuse
 
Yes, that is exactly what i am looking for. I dont know why this will never be true. Is it the problem with my logic, or its due to the way the software works? are there any other errors in the code? When the second "IF" Statement is true i set the value of "j"i to zero, but there will be already some value in the "j"i Cell. Will that be a problem?
Thanks
Infuse
 
Upvote 0

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
The problem is in your logic. You are testing to see if a specific time is before 8:00 AM and at the same time later than 9:00 PM. In other words it must be both early morning and late night. Try changing the AND to an OR, then it will be true if it is either before 8 or after 9.

In answer to the second question, when you set Ji to 0, it will over write whatever is in Ji. Is that what you want to have happen?
 
Upvote 0
Thanks once again. I wanted to set zero for “Ji” cell if the time is in between 9.00pm and 8.am. For all the other time I want the result of my previous “IF” statement.
The code did not work even I replaced “AND” with “OR”. I don’t know what else is wrong.
Also I am just curious, if I replace “AND” with “OR” will that set my “Ji” to zero if the time is anything less than 8.00 or anything greater than 9.00pm? I want zero for only those time in these interval 9.00pm – 8.00 am. Will replacing with “OR” help me here?
I hope I explain it clearly. Thanks for the help
Infuse
A = ActiveSheet.Range("A" & i).Value
B = Weekday(A)
If B > 1 And B < 7 Then
ActiveSheet.Range("J" & i).Value = ActiveSheet.Range("F" & i).Value
End If
If ActiveSheet.Range("C" & i) < Time(8, 0, 0) Or ActiveSheet _
.Range("C" & i) > Time(21, 0, 0) Then
ActiveSheet.Range("J" & i).Value = 0
End If
Next i

End Sub
 
Upvote 0
The prblem is that TIME is not supported in VBA. You can replace TIME(8,0,0) with 8/24 and TIME(21,0,0) with 21/24.
 
Upvote 0
Hey give the man/woman credit for not wanting the code written for him, more power to you :) but if every I have a problem, write the code out for me.
 
Upvote 0
Thanks a lot for everyone who tried to help me. That was my first program. I was trying to see how many extra minutes that I used in the past some months in my mobile phone. It turns out to be a successful code. I have couple of questions, Seti asked me to use 9/24 for time instead of using Time(, , ,). I searched in the help file to get information about time before I asked help. I could not find much information about this time function in the excel VB help file. Can I know where can I get these kinds of help? From where did you get this information? Also are there any places where I can find this kind of small projects which I can practice.
Thanks
Infuse
Sub FirstProgram()
Dim A As Date
Dim B, i, K, M As Integer
K = 0
For i = 5 To 428
A = ActiveSheet.Range("A" & i).Value
B = Weekday(A)
If B > 1 And B < 7 Then
ActiveSheet.Range("J" & i).Value = ActiveSheet.Range("F" & i).Value
Else
ActiveSheet.Range("J" & i).Value = 0
End If
If ActiveSheet.Range("C" & i).Value < 8 / 24 Or ActiveSheet _
.Range("C" & i).Value > 21 / 24 _
Then
ActiveSheet.Range("J" & i).Value = 0
End If
M = K + Range("J" & i).Value
K = M
Next i
MsgBox M - 600
End Sub
 
Upvote 0
You can simplify the new code a little

Code:
M = K + Range("J" & i).Value
K = M

can be coded as

Code:
 K = K + Range("J" & i).Value

As far as practice suggestions, I would say to read questions on this forum and see if anything interests you. Then try some on your own.

Another way to learn is to take some of the code found here and just play around with it.
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top