Messy / slow code

Billdub417

New Member
Joined
Nov 5, 2019
Messages
45
Hello,
I have used a macro to check whether numbers are included in a list, and if not, add these to the bottom of said list.

However, the code i am using is quite slow / basic and i think it could be simplified - glad of any assistance.

thanks in advance,
VBA Code:
Sub UpdateList()

    Sheets("Sheet2").Select
    Range("H1").Select
    ActiveCell.FormulaR1C1 = "=RC[-7]"
    Selection.AutoFill Destination:=Range("H1:H20"), Type:=xlFillDefault
    Sheets("Sheet1").Select
    Range("A2:A25").Select
    Selection.Copy
    Sheets("Sheet2").Select
    Range("H21").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("H1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    ActiveSheet.Range("$H$1:$H$55").RemoveDuplicates Columns:=1, Header:=xlNo
    Range("I1").Select
    ActiveCell.FormulaR1C1 = _
        "=IFNA(IF(RC[-1]<>"""",VLOOKUP(RC[-1],Sheet1!R2C1:R25C3,2,FALSE),""""),""Enter Ref"")"
    Range("I1").Select
    Selection.AutoFill Destination:=Range("I1:I55"), Type:=xlFillDefault
    Range("I1:I55").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Range("J1").Select
    ActiveCell.FormulaR1C1 = _
        "=IFNA(IF(RC[-2]<>"""",VLOOKUP(RC[-2],Sheet1!R2C1:R25C3,3,FALSE),""""),""Enter Ref"")"
    Range("J1").Select
    Selection.AutoFill Destination:=Range("J1:J55"), Type:=xlFillDefault
    Range("J1:J55").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Sheet1").Select
    Range("A2:c55").Select
    Selection.ClearContents
    Sheets("Sheet2").Select
    Range("H1:J1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("Sheet1").Select
    Range("A2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Sheets("Sheet2").Select
    Columns("H:K").Select
    Selection.ClearContents
    Sheets("Sheet1").Select
    Range("A1").Select
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).
Are you trying to add the values from col A sheet2 to col A sheet1 if they do not already exist?
 
Upvote 0
Ok, how about
VBA Code:
Sub Billdub()
   Dim Dic As Object
   Dim Cl As Range
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("sheet2")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Dic(Cl.Value) = Empty
      Next Cl
   End With
   With Sheets("sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then Dic.Remove Cl.Value
      Next Cl
   End With
   If Dic.Count > 0 Then
      Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(Dic.Count).Value = Application.Transpose(Dic.Keys)
   End If
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub Billdub()
   Dim Dic As Object
   Dim Cl As Range
  
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("sheet2")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Dic(Cl.Value) = Empty
      Next Cl
   End With
   With Sheets("sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then Dic.Remove Cl.Value
      Next Cl
   End With
   If Dic.Count > 0 Then
      Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(Dic.Count).Value = Application.Transpose(Dic.Keys)
   End If
End Sub
Genius, thanks.
Is there a way to adapt it so that any new numbers added on the list have a comment "Enter Ref" in Col B and Col C?
Doc1.xlsm
ABC
1ListRef 1Ref 2
21,034BlueApple
3679BluePear
42,868BlueOrange
56,609RedApple
68,352GreenApple
7190OrangeOrange
87,536GreenApple
91,629RedPear
1043
118,978
12613
131,805
148,037
159,276
Sheet1
 
Upvote 0
Yup like
VBA Code:
Sub Billdub()
   Dim Dic As Object
   Dim Cl As Range
   
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("sheet2")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Dic(Cl.Value) = Empty
      Next Cl
   End With
   With Sheets("sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then Dic.Remove Cl.Value
      Next Cl
   End With
   If Dic.Count > 0 Then
      With Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(Dic.Count)
         .Value = Application.Transpose(Dic.Keys)
         .Offset(, 1).Resize(, 2).Value = "Enter Ref"
      End With
   End If
End Sub
 
Upvote 0
Solution
Yup like
VBA Code:
Sub Billdub()
   Dim Dic As Object
   Dim Cl As Range
  
   Set Dic = CreateObject("scripting.dictionary")
   With Sheets("sheet2")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         Dic(Cl.Value) = Empty
      Next Cl
   End With
   With Sheets("sheet1")
      For Each Cl In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
         If Dic.Exists(Cl.Value) Then Dic.Remove Cl.Value
      Next Cl
   End With
   If Dic.Count > 0 Then
      With Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(Dic.Count)
         .Value = Application.Transpose(Dic.Keys)
         .Offset(, 1).Resize(, 2).Value = "Enter Ref"
      End With
   End If
End Sub
VMT!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,840
Members
449,096
Latest member
Erald

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