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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
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,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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