Keep the Cell Blank if another cell is blank in the same row

mamun_ges

Board Regular
Joined
Jul 21, 2016
Messages
58
Hi, Everyone

Hope all are fine.

I am working on a worksheet with some data related to other cells. In this worksheet, if any cell in between W9:W1200 is empty then any data input in the same row of AC and AD will be cleared.

As before, if any cell between X9:X1200 is empty, then any data if input in the same row of AE and AF will be cleared.

Can it done with the VBA? If, please .....
Capture.JPG
I upload an image to better understand my thoughts.

Thanks in advance
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
It will require VBA to do this. Are you agreeable to a VBA solution?

The next question then is on the timing of things. What is the order of how data is entered into these columns (W, AC:AD, X, AE:AF)?
Depending on how/when data is entered, we have be able to create VBA code which runs automatically upon data entry.
Otherwise, we would need to create VBA code which you would have to call/run manually.
 
Upvote 0
I would love to do this with a VBA solution. Thanks, Joe4 for the response.

AC: AD & AE: AF Data are input through the input box of a command button.
 
Upvote 0
hey, I wrote a VBA code that should solve your problem:
VBA Code:
[/
Public Sub spreadsheetman():
    For i = 1 To 100
        If IsEmpty(Range("W" & i)) Then
            Range("AC" & i).ClearContents
            Range("AD" & i).ClearContents
        End If
        If IsEmpty(Range("X" & i)) Then
            Range("AE" & i).ClearContents
            Range("AF" & i).ClearContents
        End If
    Next i
]

End Sub
 
Upvote 0
Here is code that you can run manually:
VBA Code:
Sub MyClearValues()

    Dim r As Long
   
    Application.ScreenUpdating = False
   
'   Loop through rows 9-1200
    For r = 9 To 1200
'       See if column W is empty
        If Cells(r, "W") = "" Then
'           Clear columns AC:AD
            Range(Cells(r, "AC"), Cells(r, "AD")).ClearContents
        End If
'       See if column X is empty
        If Cells(r, "X") = "" Then
'           Clear columns AC:AD
            Range(Cells(r, "AE"), Cells(r, "AF")).ClearContents
        End If
    Next r
   
    Application.ScreenUpdating = True
   
    MsgBox "Macro complete!", vbOKOnly
   
End Sub
Note that since you are looping through over 1000 rows, the "Application.ScreenUpdating" lines in your code will help make your code run faster and avoid the annoying screen flickering.
 
Upvote 1
Solution
I try your code but could not make it happen.
I am using the below code for data entry in AC-AF.
VBA Code:
 Dim Popup As Integer
 Dim x(), r As Range, C As Range, dt
 Dim myCell As Range, myCell2 As Range
 
 Dim DataVal As String, DataVal2 As String
 Dim DataVal1 As Date, DataVal3 As Date
 
 'Select column I then run the code manually by command
 
 Popup = MsgBox("Press Yes for Enter VAT Data Entry", vbQuestion + vbYesNo + vbDefaultButton2, "VAT & AIT")
 If Popup = vbYes Then
         DataVal = InputBox("Please, Enter VAT Number" _
         & vbNewLine & "Press OK For Update, Cancel for Resume")
         DataVal1 = InputBox("Please, Enter VAT Date. ", "Date As DD/MM/YYYY", Default:=Date)
         dt = CDate(DataVal1)
         Range(Selection.Address).Offset(0, 20) = DataVal
         Range(Selection.Address).Offset(0, 21) = dt
        
 Else

 End If
 
 Popup = MsgBox("Press Yes for Enter AIT Data Entry", vbQuestion + vbYesNo + vbDefaultButton2, "VAT & AIT")
 If Popup = vbYes Then
         DataVal2 = InputBox("Please, Enter AIT Number" _
         & vbNewLine & "Press OK For Update, Cancel for Resume")
         DataVal3 = InputBox("Please, Enter AIT Date. ", "Date As DD/MM/YYYY", Default:=Date)
         dt = CDate(DataVal3)
         Range(Selection.Address).Offset(0, 22) = DataVal2
         Range(Selection.Address).Offset(0, 23) = dt
        
 Else
 Exit Sub
 End If
Here on selection of I column I run above code . If the "W or X" is empty then the correspondent offset value remains empty.
 
Upvote 0
I am confused.
How can you run the code "manually" in the middle of your other code? That really isn't possible.
You could call it and run it in the middle of your other code, but not run it manually.

In any event, you probably should not run the code I gave you until after you are done making edits to your data with your other code.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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