run additional code in vb & yes/no

Trevor3007

Well-known Member
Joined
Jan 26, 2017
Messages
589
Hi,

I have this code that creates a random a password into a range:-
Sub autocode()
Static IsRandomized As Boolean
Dim i As Integer, PW1 As String
Dim cell As Range, PW As String

If Not IsRandomized Then Randomize: IsRandomized = True

For Each cell In Range("A2:A3007")
PW = vbNullString
For i = 1 To 9
Do
DoEvents
PW1 = Chr(Int((96 - 123 + 1) * Rnd + 123)) ' Lower case alpha
Loop Until InStr(1, PW, PW1, 1) = 0
PW = PW & PW1
Next i
PW = Replace(PW, Mid(PW, Int(9 * Rnd + 1), 1), Int(9 * Rnd + 1))
cell.Value = PW
Next cell

End Sub
I need to add this into the above (either before of after):-


Dim myValue As Variant
myValue = InputBox("Give me some input")
Range("b2").Value = myValue

The random code works OK, but the 'red' code above does not ( I have tried this on its own & its Ok)


Oh.... I also need YES/NO msgbox before the 'red code' is ran to ask if they want to enter txt into cell b2 or not?

Could you solve my issue please?


Thank you for your help .

KR
Trevor3007
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,878
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
This works fine for me....
On the Yes/No part of your query...do you want to exit the sub if the answer is no and / or continue if yes ??

Code:
Option Explicit

Sub autocode()
Static IsRandomized As Boolean
Dim i As Integer, PW1 As String
Dim cell As Range, PW As String
Dim myValue As Variant
myValue = InputBox("Give me some input")
Range("b2").Value = myValue
If Not IsRandomized Then Randomize: IsRandomized = True
    For Each cell In Range("A2:A3007")
    PW = vbNullString
        For i = 1 To 9
            Do
                DoEvents
                PW1 = Chr(Int((96 - 123 + 1) * Rnd + 123)) ' Lower case alpha
            Loop Until InStr(1, PW, PW1, 1) = 0
            PW = PW & PW1
        Next i
    PW = Replace(PW, Mid(PW, Int(9 * Rnd + 1), 1), Int(9 * Rnd + 1))
    cell.Value = PW
    Next cell

End Sub
 

Trevor3007

Well-known Member
Joined
Jan 26, 2017
Messages
589
This works fine for me....
On the Yes/No part of your query...do you want to exit the sub if the answer is no and / or continue if yes ??

Code:
Option Explicit

Sub autocode()
Static IsRandomized As Boolean
Dim i As Integer, PW1 As String
Dim cell As Range, PW As String
Dim myValue As Variant
myValue = InputBox("Give me some input")
Range("b2").Value = myValue
If Not IsRandomized Then Randomize: IsRandomized = True
    For Each cell In Range("A2:A3007")
    PW = vbNullString
        For i = 1 To 9
            Do
                DoEvents
                PW1 = Chr(Int((96 - 123 + 1) * Rnd + 123)) ' Lower case alpha
            Loop Until InStr(1, PW, PW1, 1) = 0
            PW = PW & PW1
        Next i
    PW = Replace(PW, Mid(PW, Int(9 * Rnd + 1), 1), Int(9 * Rnd + 1))
    cell.Value = PW
    Next cell

End Sub

hi Michael M & thank you for getting back to me.

In answer to your question ,if the Y/N is no, then just the random of passwords is to run .

Thank you again.
Trevor3007
 

Trevor3007

Well-known Member
Joined
Jan 26, 2017
Messages
589
Hi Michael M,

some of your 'magic' must of rubbed off onto me :}

As I managed to sort...

Sub autocode()
Static IsRandomized As Boolean
Dim i As Integer, PW1 As String
Dim cell As Range, PW As String
Dim myValue As Variant
Range("C1").Select
If MsgBox("Do You Want To Enter Site Name?", vbYesNo) = vbYes Then
'Do Stuff'
myValue = InputBox("Please Enter your Name")
Range("b2").Value = myValue
Else
'Don't do that stuff
End If
If Not IsRandomized Then Randomize: IsRandomized = True
For Each cell In Range("A2:A3007")
PW = vbNullString
For i = 1 To 9
Do
DoEvents
PW1 = Chr(Int((96 - 123 + 1) * Rnd + 123)) ' Lower case alpha
Loop Until InStr(1, PW, PW1, 1) = 0
PW = PW & PW1
Next I
PW = Replace(PW, Mid(PW, Int(9 * Rnd + 1), 1), Int(9 * Rnd + 1))
cell.Value = PW
Next cell
End Sub


...I be getting dressed all by myself soon...HAHA
Many thanks again :LOL:
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,878
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows

ADVERTISEMENT

Maybe I just gave you a light globe...:LOL:
 

Michael M

Well-known Member
Joined
Oct 27, 2005
Messages
20,878
Office Version
  1. 2016
  2. 2013
  3. 2007
Platform
  1. Windows
Code:
Orbis non sufficit
I'd be happy with a small oil nation !!!! :LOL:
 

Forum statistics

Threads
1,141,060
Messages
5,704,038
Members
421,323
Latest member
Exidous

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
Top