Changing the position of a message box

ma0ffst08

Board Regular
Joined
Apr 22, 2008
Messages
128
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone, I asked for some help the other day and I got some wonderful help from some of you, thank you.

I asked for some code to pick some names from a list, and to pick each name, but remove it from a list.

The code works well, but it brings up a messagebox in the middle of the screen. Is there any way the code can be changed so that the messagebox saying "Name:" comes up below the blue box (i.e. somewhere in cells C10:F20?

The code and a screenshot is below. Thank you

VBA Code:
Sub winners3()
  Dim a As Variant, b As Variant, arr As Variant, iRow As Variant
  Dim dic As Object
  Dim nNames&, i&, j&, k&, m&, x&, y&, z&
 
  With Range("A2", Range("A" & Rows.Count).End(3))
    .Interior.Color = xlNone
    a = .Value
    Range("H2:H" & Rows.Count).ClearContents
  End With
 
  ReDim b(1 To UBound(a), 1 To 1)
  Set dic = CreateObject("Scripting.Dictionary")
 
  For i = 1 To UBound(a)
    If Not dic.exists(a(i, 1)) Then
      k = k + 1
      b(k, 1) = a(i, 1)
    End If
    dic(a(i, 1)) = dic(a(i, 1)) & i + 1 & ","
  Next
 
  nNames = Application.InputBox("How many names should be picked", "Random name picker", Type:=1)
  If nNames = 0 Then
    MsgBox "Cancelled"
    Exit Sub
  End If
  If nNames > dic.Count Then
    MsgBox "The requested number of names is greater than the number of available names"
    Exit Sub
  End If
 
  Randomize
  j = 2
  arr = Evaluate("ROW(1:" & dic.Count & ")")      'total records
  For z = 1 To nNames                             'how many do i want
    x = Int(Rnd * k + z)
    y = arr(z, 1)
    arr(z, 1) = arr(x, 1)
    arr(x, 1) = y
    k = k - 1
    m = arr(z, 1)                         'random number
  
    Range("H" & j).Value = b(m, 1)
    For Each iRow In Split(dic(b(m, 1)), ",")
      If iRow <> "" Then Range("A" & iRow).Interior.Color = vbRed
    Next
 
    MsgBox "Name: " & b(m, 1), vbOKOnly
    j = j + 1
  Next
 
  MsgBox "Congratulations to all our winners"
End Sub
 

Attachments

  • Lotto.jpg
    Lotto.jpg
    166.2 KB · Views: 11

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
You cannot control the position of a MsgBox dialog. You have to create your own custom UserForm to give the message, and then you can set Left and Top values.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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