VBA Shell to batch file -- adding command line variables

BlackieHamel

Board Regular
Joined
May 9, 2014
Messages
93
I am able to run a Shell command to run a batch file from within a VBA macro, but I'm failing when I attempt to add a command-line argument. Can you tell me what is wrong with this?

Code:
Sub MassageData()

  Dim PathCrnt As String
  Dim sString As String

    sString = InputBox("Enter Search String")
    PathCrnt = ActiveWorkbook.Path
    Call Shell(PathCrnt & "\TCwithvar.bat" & sString & PathCrnt)
  
  MsgBox ("Results copied to Clipboard!")
  

End Sub

Thanks.

Blackie
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Thanks for that syntax correction. Unfortunately, it still doesn't work, because I guess I have two problems.

I found the "call shell" command here and repeated it without understanding why the second PathCrnt was there, so from your message I guess I misplaced it.

But it still doesn't work, and the execution of the batch file (which works at the cmd prompt) is not visible when the macro runs. Is there a way I can see what's happening at the cmd level and perhaps see any error messages?
 
Upvote 0
I would pause execution at the line that shells to the batch file. Then open the Immediate window in the Visual Basic Editor and in that window, type this (and press enter):

Print PathCrnt & "\TCwithvar.bat " & PathCrnt & "\" & sString

...and see if the result looks correct. If it does, you should be able to copy and paste it into a Command window and it should work. If not, then we need to fix the format in the Shell command in the code.
 
Upvote 0
Thanks. I didn't know about the Immediate window (I'm very new at this), so I appreciate the tip!

And it did seem to reveal the problem. My sString variables need to include periods (an example would be "...ABC..*" without the quotes), and when viewed in the Immediate window, the periods have been passed through as commas. I presume that it's using a different encoding. Any way to overcome that?

Blackie
 
Upvote 0
I ran your code and when I enter "...ABC..*" and print sString in the immediate window they are not converted to commas. I have no idea why that would happen on your system.
 
Upvote 0
Hmm. Problem Exists Between Keyboard And Chair. I guess I typed those commas -- hey, the key is right next to the period -- because it didn't happen a second time. Thanks for all your quick responses.

My batch file still doesn't work, though, and it does work both at the command line and through VBA without arguments. The last part of the batch file (not the command-line argument) is a "|clip" to copy results to the clipboard. Is there a way I can handle an error by reporting that the clipboard is empty?
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,900
Members
449,194
Latest member
JayEggleton

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