Adding an if formula to existing VBA code

Olympus64

New Member
Joined
Oct 13, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi!!! I am lost on trying to add an "if" statement to an existing VBA code. I would =IF(OR(G3="",H3=""),"-",IF(G3=H3,"Ship","Do Not Ship")) to
the VBA code below
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Integer
For x = 2 To 100
If Cells(x, 1).Value <> "" And Cells(x, 2).Value = "" Then
Cells(x, 2).Value = Date & " " & Time

Cells(x, 2).NumberFormat = "m/d/yyyy h:mm AM/PM"
End If
Next
Range("B:B").EntireColumn.AutoFit

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I have no idea where you want to write Ship, Do Not Ship, or - on the worksheet. You need to replace xx with cell address.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim x As Integer
For x = 2 To 100
    If Cells(x, 1).Value <> "" And Cells(x, 2).Value = "" Then
        Cells(x, 2).Value = Date & " " & Time
        Cells(x, 2).NumberFormat = "m/d/yyyy h:mm AM/PM"
    End If
Next

If Range("G3") = "" Or Range("H3") = "" Then
    Range("xx") = "-"
ElseIf Range("G3") = Range("H3") Then
    Range("xx") = "Ship"
Else
    Range("xx") = "Do Not Ship"
End If

Range("B:B").EntireColumn.AutoFit

End Sub
 
Upvote 0
I have no idea where you want to write Ship, Do Not Ship, or - on the worksheet. You need to replace xx with cell address.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim x As Integer
For x = 2 To 100
    If Cells(x, 1).Value <> "" And Cells(x, 2).Value = "" Then
        Cells(x, 2).Value = Date & " " & Time
        Cells(x, 2).NumberFormat = "m/d/yyyy h:mm AM/PM"
    End If
Next

If Range("G3") = "" Or Range("H3") = "" Then
    Range("xx") = "-"
ElseIf Range("G3") = Range("H3") Then
    Range("xx") = "Ship"
Else
    Range("xx") = "Do Not Ship"
End If

Range("B:B").EntireColumn.AutoFit

End Sub
Thank you Zot for your help. This was really helpful.
 
Upvote 0
When writing to the worksheet using the Worksheet change event, you will get into a unending loop because the writing to the worksheet triggers the worksheet change event again, which writes to the worksheet which trigger the event again, forever or until you abort. To avoid this you MUST turn off events before you write to the worksheet, and then turn them on again otherwise EXCEL won't work properly like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Integer
Application.EnableEvents = False  ' YOU MUST ADD THIS LINE Turn off events

For x = 2 To 100
If Cells(x, 1).Value <> "" And Cells(x, 2).Value = "" Then
Cells(x, 2).Value = Date & " " & Time

Cells(x, 2).NumberFormat = "m/d/yyyy h:mm AM/PM"
End If
Next
Range("B:B").EntireColumn.AutoFit
Application.EnableEvents = True    ' YOU MUST ADD THIS LINE Turn events back on again
 
End Sub
Note Zot as well
 
Upvote 0
When writing to the worksheet using the Worksheet change event, you will get into a unending loop because the writing to the worksheet triggers the worksheet change event again, which writes to the worksheet which trigger the event again, forever or until you abort. To avoid this you MUST turn off events before you write to the worksheet, and then turn them on again otherwise EXCEL won't work properly like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Integer
Application.EnableEvents = False  ' YOU MUST ADD THIS LINE Turn off events

For x = 2 To 100
If Cells(x, 1).Value <> "" And Cells(x, 2).Value = "" Then
Cells(x, 2).Value = Date & " " & Time

Cells(x, 2).NumberFormat = "m/d/yyyy h:mm AM/PM"
End If
Next
Range("B:B").EntireColumn.AutoFit
Application.EnableEvents = True    ' YOU MUST ADD THIS LINE Turn events back on again
 
End Sub
Note Zot as well
That is how you learn VBA. There are many situation that event trigger can be more mind boggling especially when you have many other events in your program. ;)
 
Upvote 0
Change:

Code:
Cells(x, 2).Value = Date & " " & Time

to:

Code:
Cells(x, 2).Value = Now
 
Upvote 0
Change:

Code:
Cells(x, 2).Value = Date & " " & Time

to:

Code:
Cells(x, 2).Value = Now
Hi Zot and Phuoc I added a snapshot of what I am trying to achieve with your help.

I want to be able to scan two barcodes (Col A and Col B) capturing the instructions (Col C) to "ship" if the barcodes match, and "Do not ship" if the barcodes do not match. I need the date and time stamp (Col D) after the condition has been met in Col C.

1634579251955.png


VBA Code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x As Integer
For x = 2 To 100
Dim xx As Integer
For xx = Range("c:c").Select
If Cells(x, 1).Value = Cells(x, 2).Value = "" Then
Cells(x, 4).Value = Date & " " & Time
Cells(x, 4).NumberFormat = "m/d/yyyy h:mm AM/PM"
End If
Next

If Range("A2") = "" Or Range("B2") = "" Then
Range("xx") = "-"
ElseIf Range("A2") = Range("B2") Then
Range("xx") = "Ship"
Else
Range("xx") = "Do Not Ship"
End If

Range("D:D").EntireColumn.AutoFit

End Sub
 
Upvote 0
Any reason why you need to put your macro under Worksheet_Change event?
 
Upvote 0
I was watching a video and thats the way the instructor explained how to do it.
It all started with scanning a barcode and getting the date and time. I then miserably failed at trying to build on it.
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,303
Members
449,218
Latest member
Excel Master

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