run additional code in vb & yes/no

Trevor3007

Well-known Member
Joined
Jan 26, 2017
Messages
675
Office Version
  1. 365
Platform
  1. Windows
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

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
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
 
Upvote 0
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
 
Upvote 0
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 :laugh:
 
Upvote 0
Maybe I just gave you a light globe...:lol:
 
Upvote 0
Code:
Orbis non sufficit
I'd be happy with a small oil nation !!!! :lol:
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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