Vba bug fix

adpatel

New Member
Joined
May 30, 2019
Messages
42
Office Version
  1. 2016
Platform
  1. Windows
Hello everyone, I have a VBA code which is very simple, but still it's not working as it should be, so i think there is little bug in code, pls help to find it out

Requirement:-

If cell value is 0 or time is less than 15:29:00

than do nothing

else, 'Main code ll be run in this case'

My code:-

VBA Code:
Dim LTime As Date

   Sheets("AD").Select
    
    If Range("J2").Value = 0 Or LTime < "15:29:00" Then
    Range("K3").Select
    
    Else


Sheets("AD").Select

 Range("J2:AF3").Select
    Selection.Copy
 
    Range("J6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("J6").Select
   
       
      End If


Any help ll be appriciated
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Time is this line is just viewed as text, not the time itself

If Range("J2").Value = 0 Or LTime < "15:29:00" Then

In Excel time is stored as as fractional number. Try

If Range("J2").Value = 0 Or LTime < TimeValue("15:29:00") Then
 
Upvote 0
Solution
Time is this line is just viewed as text, not the time itself

If Range("J2").Value = 0 Or LTime < "15:29:00" Then

In Excel time is stored as as fractional number. Try

If Range("J2").Value = 0 Or LTime < TimeValue("15:29:00") Then
I have tried this solution but didn't worked my full code mentioning here belw

VBA Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)


Application.ScreenUpdating = False

Dim LTime As Date

   Sheets("AD").Select
   
    If Range("J2").Value = 0 Or LTime < TimeValue("11:29:00") Then
    Range("K3").Select
   
    Else


Sheets("AD").Select

Range("J2:AF3").Select
    Selection.Copy

    Range("J6").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("J6").Select
  
      
      End If
       
    Sheets("calenders").Select
   
    Application.ScreenUpdating = True
   
    If Me.Saved = False Then Me.Save
   
End Sub
 
Upvote 0
How the worksheet looks like? Where LTime comes from?
 
Upvote 0
How the worksheet looks like? Where LTime comes from?
Ltime comes from nowhere, it's just mentioned in VBA code, want to check current time so I guess I am missing on that
 
Upvote 0
ok, got it now it looks like
I think no need to use TimeValue(Now) since now is in Excel time format already. TimeValue is to convert time is from string representation
 
Upvote 0
I think no need to use TimeValue(Now) since now is in Excel time format already. TimeValue is to convert time is from string representation
As currently it's working fine, not making any changes into it, if required in future ll make changes according to you suggestion, Thx a tone @Zot for your prompt replies
 
Upvote 0
@Zot you do need to use the Timevalue part as Now includes the date, so it includes the Integer part of the number.

For instance here it is 06:57 which is 0.289583333333333 without the date part.

Now would give 44210.289583333333333 which would be greater than TimeValue("03:29:00 PM") which is 0.645138888888889.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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