Home > Blockchain >  Getting a random value from a column in VBA Excel
Getting a random value from a column in VBA Excel

Time:11-17

I'm writing War the Card Game in VBA Excel. I would like to get a random value from column A and column B. I researched it and it seems that this is able to generate a random number.

Dim ws As Sheet1

P1LR = Cells(Rows.Count, 1).End(xlUp).Row
P2LR = Cells(Rows.Count, 2).End(xlUp).Row

P1Cards = WorksheetFunction.RandBetween(A1, P1LR)
P2Cards = WorksheetFunction.RandBetween(A1, P2LR)

However it is giving me the value of the row and not the value that's actually in the cell. How would I get that value using this function?

CodePudding user response:

you can address the cell by the variable, try like this:

Dim ws As Sheet1

P1LR = Cells(Rows.Count, 1).End(xlUp).Row
P2LR = Cells(Rows.Count, 2).End(xlUp).Row

P1Cards = WorksheetFunction.RandBetween(A1, P1LR),
P2Cards = WorksheetFunction.RandBetween(A1, P2LR)

Cells(P1Cards, 1).Select    
Cells(P2Cards, 2).Select

Or

Cells(WorksheetFunction.RandBetween(A1, P1LR), 1).Select    
Cells(WorksheetFunction.RandBetween(A1, P2LR), 2).Select
  • Related