VBA code to paste adjacent cell only when cell pasted to is empty

Indominus

Board Regular
Joined
Jul 11, 2020
Messages
160
Office Version
  1. 2016
Platform
  1. Windows
Hello. I have this code that compares two columns between two sheets. It matches the values in column J in “Ships” with column M in “Main”. It copies the value 10 cells to the right from sheet “Ships” and pastes it in the 3rd cell to the right on “Main” sheet. However, I need it to only paste it if the 3rd cell over of column M is empty. I have tried tweaking it with no luck. Any help is appreciated. Here is my code. Thanks!

So I need it to identify if C1.Offset( ,3).Value is empty

VBA Code:
Private Sub GetCompletionPercentage()

Dim Cl As Range

Dim Dic As Object



lastRow = Cells(Rows.Count, "P").End(xlUp).Row

Set rng = Range("P2:P" & lastRow)



Set Dic = CreateObject("scripting.dictionary")

With Sheets("Ships")

For Each Cl In .Range("J2", .Range("J" & Rows.Count).End(xlUp))

Dic(Cl.Value) = Cl.Offset(, 10).Value

Next Cl

End With

With Sheets("Main")

For Each Cl In .Range("M2", .Range("M" & Rows.Count).End(xlUp))

If Dic.Exists(Cl.Value) Then Cl.Offset(, 3).Value = Dic(Cl.Value)

Next Cl

End With

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
How about replacing:

VBA Code:
If Dic.Exists(Cl.Value) Then Cl.Offset(, 3).Value = Dic(Cl.Value)

with:

VBA Code:
If Dic.Exists(Cl.Value) Then
    If Cl.Offset(, 3).Value = "" Then Cl.Offset(, 3).Value = Dic(Cl.Value)
End If
 
Upvote 0
Solution
How about replacing:

VBA Code:
If Dic.Exists(Cl.Value) Then Cl.Offset(, 3).Value = Dic(Cl.Value)

with:

VBA Code:
If Dic.Exists(Cl.Value) Then
    If Cl.Offset(, 3).Value = "" Then Cl.Offset(, 3).Value = Dic(Cl.Value)
End If
Works perfectly!!! Thank you so much
 
Upvote 0

Forum statistics

Threads
1,224,816
Messages
6,181,138
Members
453,021
Latest member
Justyna P

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