PDA

View Full Version : VBA code to generate letters in one cell



alihadi
08-12-2015, 04:25 AM
Hi everyone,

Does anyone know how to generate more than one letter in one cell using VBA code? For example: how to generate letters HC or GP in one cell using VBA code, every time by clicking run generates these letters in different cell.
Thanks
Ali

mikerickson
08-12-2015, 06:36 AM
Something like


Range("A1").Value = "HC"

alihadi
08-12-2015, 06:42 AM
Something like


Range("A1").Value = "HC"

alihadi
08-12-2015, 06:46 AM
Thanks,
I want the letters be generated randomly in VBA. For example: by clicking run to be generated HC and in another click to be generated either HC again or GP.

Thanks
Ali

p45cal
08-12-2015, 10:06 AM
Range("A1").Value = Choose(Application.RandBetween(1, 2), "HC", "GP")

alihadi
08-13-2015, 04:19 AM
Thanks,
Do you know how to generate the numbers (20, 40, 45) randomly in VBA code. For example: by clicking run to be generated 20 and in another click to be generated either 40 again or 45.

Thanks
Ali

mikerickson
08-13-2015, 06:45 AM
You might use this volatile UDF

Function RandFromList(ParamArray ListItems() As Variant)
RandFromList = ListItems(WorksheetFunction.RandBetween(0, UBound(ListItems)))
End Function

put =RandFromList(20,40,45) in a cell.

alihadi
08-13-2015, 08:13 AM
Thanks for the code, but it does not work. Can you make it more clear?

Thanks
Ali

mikerickson
08-13-2015, 05:29 PM
I'm not sure what you mean "it doesn't work". Putting that formula in a cell returned a random selection of 20,40,45 every time that any cell in the workbook was changed. It worked for me.

Actually, I thought of a more native expression

=CHOOSE(RANDBETWEEN(1, 3), 20, 40, 45)

What is the purpose of this project? You have spread several slight variations of the same topic (generating random selections) across two threads.
It would be more fruitful if you were to explain the whole project.

alihadi
08-14-2015, 08:44 AM
Thank you for the code.

Do you know how to generate random numbers between (1 & 4) with no repetition and sort them ascending using VBA excel without removing blank cells? I want to generate the numbers randomly in random cells an sort them without removing the blank cells.

Regards
Ali

mikerickson
08-15-2015, 10:57 AM
Do you know how to generate random numbers between (1 & 4) with no repetition and sort them ascending
That would be the constant sequence 1,2,3,4.

p45cal
08-15-2015, 12:46 PM
That would be the constant sequence 1,2,3,4.Priceless! This really had me LOL - OK… a few drinks helped.

snb
08-15-2015, 02:05 PM
You should sort them randomly :crying:

mikerickson
08-15-2015, 04:45 PM
You should sort them randomly :crying:

OK

If Rnd() < 3 Then
myRange.Sort
End If

alihadi
08-16-2015, 01:01 AM
Thanks for the code.
I want to generate random numbers between (1 & 4) with no repetition and sort them ascending using VBA excel without removing blank cells? I want to generate the numbers randomly in random cells and sort them without removing the blank cells as shown in the attached excel file.
I will be very grateful if you assist me in this matter.

Thanks
Ali