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

Indominus

Board Regular
Joined
Jul 11, 2020
Messages
126
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

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
1,295
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows
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
 
Solution

Indominus

Board Regular
Joined
Jul 11, 2020
Messages
126
Office Version
  1. 2016
Platform
  1. Windows
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
 

Forum statistics

Threads
1,140,925
Messages
5,703,206
Members
421,281
Latest member
mfarhankhan87

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
Top