help me with coding this statement

mahex

New Member
Joined
Apr 28, 2018
Messages
4
hello everyone,

i have a table like below picture:
2bgj_22333.jpg


i want to give specific amount of number from user ( the amount of number is based on unique values in state field- for example in above picture we have 5 unique state then we should give 5 number from user) with inputbox and paste the number for each state.
the result should be the below picture
dmh_4321.jpg


can anyone help me with this??
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try this
Assumes data is in column B, row 2 onwards

Place in standard module
Run from the sheet containing table values
Amend if necessary
Code:
Sub AddNumber()
    Dim dict As Object, states As Collection
    Dim ws As Worksheet, rng As Range, state As Variant, msg As String
    Set ws = ActiveSheet
    Set rng = [COLOR=#ff0000]ws.Range("B2", ws.Range("B" & Rows.Count).End(xlUp)[/COLOR])
UniqueStates:
    Set states = New Collection
    On Error Resume Next
    For Each state In rng
        states.Add state, state
    Next
    On Error GoTo 0
CreateDictionary:
    Set dict = CreateObject("Scripting.Dictionary")
    For Each state In states
        dict.Add state, InputBox(state, "ENTER A VALUE")
     Next
IsUserHappy:
    msg = ""
    For Each key In dict.Keys:      msg = msg & vbCr & dict(key) & vbTab & key: Next
    If MsgBox(msg, vbYesNo, "Are you happy?") = vbNo then  GoTo CreateDictionary
AddToWorksheet:
    For Each state In rng:
        For Each key In dict.Keys
            If key = state.Value Then state.Offset(, -1) = dict(key)
        Next key
    Next state
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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