Addition in VB codes for copy paste the matched values

thespardian

Board Regular
Joined
Aug 31, 2012
Messages
100
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi there!
Back in Oct, 2019, CharlieRog posted a question and the @Fluff suggested the code. I just changed the sheet name and cell references in the suggested code and it works for me. But I want to add the following task.

If I delete any value in column C on sheet3 the value in Column B on sheet 3 should automatically be deleted. Any help will be highly appreciated.

The codes are as under:
VBA Code:
Sub Sheet3ColB()
Dim Cl As Range
    Dim Dic As Object
    
    Set Dic = CreateObject("scripting.dictionary")
    With Sheet2
        For Each Cl In .Range("A3", .Range("A" & Rows.Count).End(xlUp))
            Dic(Cl.Value) = Cl.Offset(, 5).Value
        Next Cl
    End With
    With Sheet3
        For Each Cl In .Range("C3", .Range("C" & Rows.Count).End(xlUp))
            If Dic.exists(Cl.Value) Then Cl.Offset(, -1).Value = Dic(Cl.Value)
            
        Next Cl
    End With
End Sub
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,239
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
    With Sheet3
        For Each Cl In .Range("C3", .Range("C" & Rows.Count).End(xlUp))
            If Cl.Value = "" Then Cl.Offset(, -1).Value = ""
            If Dic.exists(Cl.Value) Then Cl.Offset(, -1).Value = Dic(Cl.Value)
        Next Cl
    End With
 

thespardian

Board Regular
Joined
Aug 31, 2012
Messages
100
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
How about
VBA Code:
    With Sheet3
        For Each Cl In .Range("C3", .Range("C" & Rows.Count).End(xlUp))
            If Cl.Value = "" Then Cl.Offset(, -1).Value = ""
            If Dic.exists(Cl.Value) Then Cl.Offset(, -1).Value = Dic(Cl.Value)
        Next Cl
    End With
Thanks a lot @Fluff for the input. I apologize for late reply. Actually i was not in good health.
But the above code doesn't do any changes when i delete the value in column C on sheet3. Can you please look it again?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,239
Office Version
  1. 365
Platform
  1. Windows
It will clear col B if col C is blank when you run the code. Are you saying that is not happening?
 

thespardian

Board Regular
Joined
Aug 31, 2012
Messages
100
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows

ADVERTISEMENT

It will clear col B if col C is blank when you run the code. Are you saying that is not happening?
Yes, Here is the file link. There are two macro named Sheet3ColA and Sheet3ColB. They are serving the purpose very well. But On SHEET 3, I want to clear the Col B when Col C is cleared. Can you please check it for me.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,239
Office Version
  1. 365
Platform
  1. Windows
Ok, how about
VBA Code:
    With Sheet3
        For Each Cl In .Range("C3:C" & .Range("A" & Rows.Count).End(xlUp))
            If Cl.Value = "" Then Cl.Offset(, -1).Value = ""
            If Dic.exists(Cl.Value) Then Cl.Offset(, -1).Value = Dic(Cl.Value)
        Next Cl
    End With
 
Solution

thespardian

Board Regular
Joined
Aug 31, 2012
Messages
100
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows

ADVERTISEMENT

Ok, how about
VBA Code:
    With Sheet3
        For Each Cl In .Range("C3:C" & .Range("A" & Rows.Count).End(xlUp))
            If Cl.Value = "" Then Cl.Offset(, -1).Value = ""
            If Dic.exists(Cl.Value) Then Cl.Offset(, -1).Value = Dic(Cl.Value)
        Next Cl
    End With
Hi there! Thanks a lot for your guidance. The macro is working now but there is another problem. I have tried a lot to solve this issue myself but couldn't succeed.
I am calling this macro in Private Sub. (When i click any cell in the column C, it call this macro). The problem is---- it takes more than 30 seconds when i click in any cell in column C. Can you please help me to get rid of this issue please.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,239
Office Version
  1. 365
Platform
  1. Windows
That code was not designed to be run from selection change event, which is why it's slow.
If you want to run it that way, I suggest that you start a new thread & explain exactly what you are trying to do.
 

thespardian

Board Regular
Joined
Aug 31, 2012
Messages
100
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Ok thanks a lot for your time and guidance.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,239
Office Version
  1. 365
Platform
  1. Windows
You're welcome.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,127
Messages
5,835,553
Members
430,367
Latest member
glastonbury

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