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

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
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,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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