VBA to Check 2 Conditions and Change Cell Value

CreativeUsername

Board Regular
Joined
Mar 11, 2017
Messages
52
I'm probably making this harder than it needs to be. Basically I need to check if column K is empty (to limit the range), and the cell offset (0,1) (actually multiple in the row). If both are empty I need it to Do nothing (stop). If "K" is not empty but the offset is empty change the offset cell value to "-". "K" will be full to the end of the range. The offset will have more blanks than filled data but the blanks are important.

Code:
For Each Cel In Range("K:K")
        If Cel.Value <> "" And Cel.Offset(0, 1) <> "" Then Cel.Offset(0, 1).Value = "-"
        End If
    Next[code]

I thought I was close but I keep getting errors.  I tried using the IsEmpty also.  Apparently got that wrong.  Thought of a nested if statement but apparently had syntax issues.  I had it half working.. but it was overwriting information with "-" till "K" was blank.  
any help would be appreciated.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
If "K" is not empty but the offset is empty change the offset cell value to "-".
You have written your code to check to see if both are NOT empty.

Also, looping through an entire column is slow, inefficient, and usually unnecessary. Find the last row in the column, and only loop down that far.
Here is how I would do it:
Code:
    Dim lastRow As Long
    Dim cel As Range
    
    Application.ScreenUpdating = False
    
'   Find last row in column K
    lastRow = Cells(Rows.Count, "K").End(xlUp).Row
    
'   Loop through cells in column K (down to last entry)
    For Each cel In Range("K1:K" & lastRow)
        If cel <> "" And cel.Offset(0, 1) = "" Then cel.Offset(0, 1) = "-"
    Next cel
    
    Application.ScreenUpdating = True
 
Last edited:
Upvote 0
This works great.. but it gets stopped by a cell with an "=" prior to the contents (coming from CSV file as output from other software). How would I add an error handler that puts " ' " before the contents and tries again?
 
Upvote 0
This works great.. but it gets stopped by a cell with an "=" prior to the contents (coming from CSV file as output from other software). How would I add an error handler that puts " ' " before the contents and tries again?
Can you try explaining that again?
What exactly is in the cell that causes problems?
And which column is this value in?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
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