Addition in VB codes for copy paste the matched values

thespardian

Board Regular
Joined
Aug 31, 2012
Messages
119
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

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
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
 
Upvote 0
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?
 
Upvote 0
It will clear col B if col C is blank when you run the code. Are you saying that is not happening?
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
Solution
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,525
Members
448,969
Latest member
mirek8991

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