Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a very simple way to enter key <> value pairs in VBA?

Tags:

excel

vba

I'm writing a VBA script where I want the following two functionalities (pseudo-code):

C5 = "Hello"
D6 = "World"
E2 = 23.45
a: Place the values in the correct cell in the worksheet
and
b: Check if the cells contain the correct values

I'll be sharing this with coworkers that have never written a script in their lives (but they are able to use Excel-formulas like vlookup etc). Therefore, I need to be able to very simply write the cell number and the corresponding value next to each other.

Sub NewbieProofSub
    Set dict = CreateObject("Scripting.Dictionary")
    dict.Add "C5", "Hello"
    dict.Add "D6", "World"
    dict.Add "E2", 23.45

    ' Inserting values:
    Dim v As Variant
    Dim s As String
    For Each v In dict.Keys
        s = v
        Range(s).Value = dict.Item(v)
    Next

    dict.Add "F3", 13

    ' Checking values
    For Each v In dict.Keys
        s = v
        If Range(s).Value = dict.Item(v) Then
        MsgBox ("The value in " & s & " is " & dict.Item(v))
        Else
        MsgBox ("The value in " & s & " is not " & dict.Item(v))
        End If
    Next

End Sub

These will be divided into two modules, but I included both here to illustrate.

I'm quite content, but I wonder if it's possible to make it even simpler, avoiding all the lines with dict.add? Something like:

' Fill this list with your desired values on the format:
' Cell, Value (Remove the existing lines)

dict.add {
"C5", "Hello"
"D6", "World"
"E2", 23.45
}

Is something like this possible?

like image 266
Stewie Griffin Avatar asked Jan 04 '23 12:01

Stewie Griffin


1 Answers

I guess this can be made simpler if cell address and corresponding values can be written somewhere in the sheet (columns that are not used). For example, if the cell address are entered in range O1:O3 and corresponding values in range P1:P3 then instead of

dict.Add "C5", "Hello"
dict.Add "D6", "World"
dict.Add "E2", 23.45

items can be added to dictionary as

Dim rng As Range, cel As Range
Set rng = Range("O1:O3")
For Each cel In rng
    dict.Add cel.Value, cel.Offset(0, 1).Value
Next cel

and if number of rows will vary then above can be written as

Dim rng As Range, cel As Range
Dim lastRow As Long
lastRow = Cells(Rows.Count, "O").End(xlUp).Row
Set rng = Range("O1:O" & lastRow)
For Each cel In rng
    dict.Add cel.Value, cel.Offset(0, 1).Value
Next cel

Another way of doing this will be to add cell address in an array and corresponding values in another array as

Dim arr1, arr2, i As Long
arr1 = Array("C5", "D6", "E2")
arr2 = Array("Hello", "World", "23.45")
For i = LBound(arr1) To UBound(arr1)
    dict.Add arr1(i), arr2(i)
Next i

or adding both cell address and corresponding values together in one array as

Dim arr, i As Long
arr = Array("C5", "Hello", "D6", "World", "E2", "23.45")
For i = LBound(arr) To UBound(arr) Step 2
    dict.Add arr(i), arr(i + 1)
Next i
like image 148
Mrig Avatar answered Mar 05 '23 23:03

Mrig