Macro to execute net user through cmd

akshay20

New Member
Joined
Feb 28, 2018
Messages
6
Hi All,

Need to execute net user command using CMD through macro. the account name is being captured in excel and then using macro need to execute below command:

net user "username from excel" /do

here is my code below and appreciate your help:

Sub Button3_Click()
Dim wb1 As Workbook, wb2 As Workbook, Wb3 As Long, wb4 As String, wb5 As Long


Set wb1 = ThisWorkbook
Wb3 = Range("C3").Value
wb4 = "moe@" & Wb3


Set wb2 = Workbooks.Open("C:\Bulk\Source.csv")


wb1.Sheets("Master").Range("C25").Copy
wb2.Sheets("source").Range("A2").PasteSpecial xlPasteValues
Range("B2") = wb4
wb5 = Range("C25").Value


Shell ("cmd.exe /k net user" & Range("C25") & "/do")


ActiveWorkbook.Save
ActiveWorkbook.Close True


End Sub
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
does the shell command not work?


No the Shell command only returns below result. But i am trying to execute this command
net user am123 /do (Considering am123 is the account name read from the excel file Range("C25")


The syntax of this command is:


NET
[ ACCOUNTS | COMPUTER | CONFIG | CONTINUE | FILE | GROUP | HELP |
HELPMSG | LOCALGROUP | PAUSE | SESSION | SHARE | START |
STATISTICS | STOP | TIME | USE | USER | VIEW ]
 
Upvote 0
Shell ("cmd.exe /k net user" & Range("C25") & "/do")
should be:
Shell ("cmd.exe /k net user " & Range("C25") & " /do")

(added spaces where needed)
 
Upvote 0
Thanks Pat, I tried that as well but for some reason it doesnt accept the username in (Range "C25") cell. if i replace with "abc123" it works fine. Am i doing something wrong as i need shell command to read the cell value from C25
Shell ("cmd.exe /k net user " & Range("C25") & " /do")
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,397
Members
449,081
Latest member
JAMES KECULAH

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