Wednesday 21 December 2022

Macro to compare two data sets

 1. Object of the macro


a)The data is populated as follows:




b) I want to compare based on the Unique identifier List A and List B

c) Highlight the common items in List A and List B

d) Get populated in Sheet 2, items present in List A, but not in List B

e)Get populated in Sheet 3, items present in List B, but not in List A



2. Code

Sub Comparedatasets()

Application.ScreenUpdating = False

lr1 = Range("A" & Rows.Count).End(xlUp).Row

lr2 = Range("D" & Rows.Count).End(xlUp).Row

Sheets(1).Range("A2", "B2").Copy

Sheets(2).Range("A1").PasteSpecial Paste:=xlPasteValues

Sheets(3).Range("A1").PasteSpecial Paste:=xlPasteValues

check = False

For r = 3 To lr1

Set compare1 = Cells(r, 1)

For q = 3 To lr2

Set Compare2 = Cells(q, 4)

If compare1 = Compare2 Then Compare2.Interior.Color = vbYellow: check = True

Next q

If check = False Then

Range(compare1, compare1.End(xlToRight)).Copy

Sheets(3).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial _Paste:=xlPasteValues

End If

check = False

Next r  

check = False

For r = 3 To lr2

Set compare1 = Cells(r, 4)

For q = 3 To lr1

Set Compare2 = Cells(q, 1)

If compare1 = Compare2 Then Compare2.Interior.Color = vbYellow: check = True

Next q 

If check = False Then

Range(compare1, compare1.End(xlToRight)).Copy

Sheets(2).Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial _Paste:=xlPasteValues

End If

check = False      

Next r

Application.ScreenUpdating = True

End Sub



3) Result  



 






Wednesday 14 December 2022

Diaries on VBA- Scripting Dictionary

 

1. Part 1- Basics of Dictionary


a) Dictionary is used to assign a value to a key/item

b)Syntax

i) If I want to create Gowtham as new dictionary

Dim Gowtham as Newdictionary

ii) Add items to the dictionary Gowtham

For Eg. I want to assign 1 to a, 2 to b


gowtham. add(a,1)

gowtham. add(a,2)


c) When a key is not already declared and is called later, it will get created in dictionary

For Eg,



Sub usedictionary()


Dim dict As New Dictionary


dict.Add "a", 1

dict.Add "b", 2

dict.Add "c", 3

dict("d") = dict("d") + 100


Debug.Print dict("d")

 

End Sub


..., the above code will print 100 

Tuesday 6 December 2022

Macro to count the number of characters in cells and populate the data set sperately

  1. Object of  the Macro



I want to:

  • count the number of HSN digits in Column A, 
  • get them populated in Column C, 
  • filter the items with HSN less than 6 digits and
  • get the dataset pasted at the cell H1.



2. The Code


Sub countcharactersincell()

Dim a As String

Dim b As Integer

Dim i As Integer

Dim j As Integer

Dim k As Integer

j = Cells.End(xlDown).Row

For i = 2 To j

a = Cells(i, 1) 

b = Len(a)

Cells(i, 3) = b

Next i

Range("A1").AutoFilter Field:=3, Criteria1:="<6"

Range("a1:c" & Range("a1").End(xlDown).Row).SpecialCells(xlCellTypeVisible).Copy

Cells(1, 8).PasteSpecial

End Sub


3. Output 




Funfact:

The same output could be achieved with a simple "Sorting" option of Excel. 

But, I'm crazy about those "Mootai poochiyai kollum navina machine"