Darren Smith
Well-known Member
- Joined
- Nov 23, 2020
- Messages
- 631
- Office Version
- 2019
- Platform
- Windows
The Function is called ReadData it`s a Dictionary
VBA Code:
Private Sub Reset_Drawing_Numbers_Click()
Dim matchRange As Range
Dim oDict As Object
Dim cmb As ComboBox
Dim SourceLastRow As Long, DestLastRow As Long
Dim wsSource As Worksheet, wsDest As Worksheet
Set wsSource = ThisWorkbook.Worksheets("Drawing No")
Set wsDest = ThisWorkbook.Worksheets("Job Card Master")
Set cmb = Me.Reset_Drawing_Numbers
SourceLastRow = wsSource.Range("A" & Rows.Count).End(xlUp).Row
DestLastRow = wsDest.Range("A" & Rows.Count).End(xlUp).Row
Set matchRange = wsSource.Range("C1:L" & SourceLastRow)
Call ReadData
Set oDict = ReadData
Select Case cmb.Value
Dim DataRange As Range
Case ("Reset Drawing No. 1 Page")
Set DataRange = wsDest.Range("A13:Q" & DestLastRow)
Case ("Reset Drawing No. 2 Page")
Set DataRange = wsDest.Range("A13:Q61,A66:Q" & DestLastRow)
Case ("Reset Drawing No. 3 Page")
Set DataRange = wsDest.Range("A13:Q61,A66:Q122,A127:Q" & DestLastRow)
Case ("Reset Drawing No. 4 Page")
Set DataRange = wsDest.Range("A13:Q61,A66:Q122,A127:Q183,A188:Q" & DestLastRow)
Case ("Reset Drawing No. 5 Page")
Set DataRange = wsDest.Range("A13:Q61,A66:Q122,A127:Q183,A188:Q244,A249:Q" & DestLastRow)
End Select
If Not DataRange Is Nothing Then
Dim area As Range
For Each area In DataRange.Areas
GetPartInfoForRange area.Columns(5), area.Columns(2), oDict
Next area
Reset_Drawing_Numbers.Value = "Reset Drawing Numbers"
End If
End Sub
Sub GetPartInfoForRange(lookupRange As Range, outputRange As Range, DataSet As Object)
Dim cell As Range
Dim counter As Long
Dim oDict As Object
For Each cell In lookupRange.Cells
counter = counter + 1
outputRange.Cells(counter) = GetPartInfo(DataSet, cell.Value)
Next cell
End Sub
Private Function GetPartInfo(ByRef oDict As Object, sKey As String)
Dim Output As String
Output = ""
If oDict.Exists(sKey) Then
Output = oDict(sKey)
End If
GetPartInfo = Output
End Function
Private Function GetDictionary(rng As Range, keyCol As Long, valCol As Long) As Object
Dim rCell As Range
Dim oDict As Object
Dim keyCells As Range
Set keyCells = rng.Columns(keyCol).Cells
Dim valueCells As Range
Set valueCells = rng.Columns(valCol).Cells
Set oDict = CreateObject("Scripting.Dictionary")
For Each rCell In keyCells
Dim counter As Long
counter = counter + 1
If Not oDict.Exists(rCell.Value) Then
oDict.Add rCell.Value, valueCells.Cells(counter).Value
End If
Next rCell
Set GetDictionary = oDict
End Function