Is it possible to pass arguments to local hyperlinks to .cmd files?

SmartShark

New member
Local time
4:12 AM
Messages
3
I am facing the following interesting situation.

I have an excel file with many columns. One column contains line numbers (for example 3150).

Code:
Source                       Line Number
if(this.getValue()==0)        3150
for(i=0;i<value;i++)           3175
while(true)                       3200
...                                  ...
I want to set up hyperlinks, such that when the user clicks on one of the cells in the Line Number column, a batch program is called with the line number as its command line argument, so that the source file is opened at the specific line number.

I was able to set up a hyperlink without any command line arguments for one cell, but then I had to set the value explicitly in the batch file as follows -

Code:
cd "%~dp0"
notepad++ sourcefile.txt -n3150
So this batch file opens the "sourcefile.txt" at line number 3150 in the Notepad++ editor. However, it's not practical to write a separate batch file for every such line number in the excel sheet. So I am wondering if it is possible to somehow pass the line number as an argument to the batch program.
 

My Computer My Computer

At a glance

Windows 7 Professional 64 bit
OS
Windows 7 Professional 64 bit
You can pass a variable to a bath file. Change the line to read

notepad++ sourcefile.txt -n%1
 

My Computer My Computer

At a glance

Windows 10 Pro X64Intel Quad Core i7-4770 @ 3.4Ghz16.0GB PC3-12800 DDR3 SDRAM 1600 MHzIntel Integrated HD Graphics
Computer type
PC/Desktop
Computer Manufacturer/Model Number
Lenovo IdeaCenter 450
OS
Windows 10 Pro X64
CPU
Intel Quad Core i7-4770 @ 3.4Ghz
Memory
16.0GB PC3-12800 DDR3 SDRAM 1600 MHz
Graphics Card(s)
Intel Integrated HD Graphics
Sound Card
Realtek HD Audio
Monitor(s) Displays
HP 22" LCD
Screen Resolution
1680 x 1050
Hard Drives
250GB Samsung EVO SATA-3 SSD
2TB Seagate ST2000DM001 SATA-2
1.5TB Seagate ST3150041AS SATA
Keyboard
Dell USB
Mouse
Lenovo USB
Internet Speed
Cable via Road Runner 3MB Upload, 30MB Download
Antivirus
Windows Defender, MBAM Pro, MBAE
Browser
Seamonkey
Other Info
UEFI/GPT
PLDS DVD-RW DH16AERSH
I could find any information on getting this to work using a hyperlink but I did find a way to do it using some VB code. Took me a little while to get it to work though and it might not be perfect, plus you need to enable macros and remove your hyperlinks.

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Column = 3 Then
        Dim strArg As String
        Dim RetVal As Integer
    
        RetVal = MsgBox("Run Batch", vbYesNo)
    
        If RetVal = 6 Then
        strArg = Target
            RetVal = Shell("C:\ExcelExperiments\BatchFile.bat" & " " & strArg, 1)
        End If
    End If
End Sub
This is one example, it runs every time you change the selected cell and first checks if the newly selected cell is in Column C and if it is then it asks you if you want to run the batch file. It needs to be added to each worksheet you want it to work with.

To get to the VBA editor you need to add it to the ribbon by going to,
File > Options > Customize the ribbon and on the right select "Main tabs" and Check "Developer".

Next click the Developer tab on the ribbon and click "Visual Basic". If your using Excel 2003 a quick Google should find a way to do this.

When the editor opens you should see Sheet1,2,3 and ThisWorkbook on the left. Double click a Sheet to open it and then paste my code into it.

Changing the code.
To change the column change Target.Column = 3 to the column number you need, eg. A=1, B=2 etc...

Removing the prompt may prove a problem when entering new data or accidently clicking in the column but if you want to try it then remove the following lines,

1. RetVal = MsgBox("Run Batch", vbYesNo)
2. If RetVal = 6 Then
3. End If ---- Either one


Instead of automatically running you can run it either via a keyboard shortcut or a button.

I really don't know how much you know about this stuff but if you want to set it up as Macro to use a keyboard shortcut or a button and need help or if there's any other problems with this then let me know.
 

My Computer My Computer

At a glance

Windows 7 Ultimate x64Intel Pentium Dual Core E5200 2.5GHz (3.77GHz...Corsair 4GB DDR2 (4x1GB CM2X1024-6400C4)Palit GeForce GTS 250 (1024MB)
Computer Manufacturer/Model Number
Self built
OS
Windows 7 Ultimate x64
CPU
Intel Pentium Dual Core E5200 2.5GHz (3.77GHz OC)
Motherboard
Asus P5Q-E
Memory
Corsair 4GB DDR2 (4x1GB CM2X1024-6400C4)
Graphics Card(s)
Palit GeForce GTS 250 (1024MB)
Sound Card
On Board (ADI AD2000B 8ch HD)
Monitor(s) Displays
Samsung 32in LCD TV
Screen Resolution
1360x768
Hard Drives
2 x 1TB Samsung 103SJ (Raid0)
2 x External 500GB Samsung 502IJ (NexStar 3 HD Enclosures)
PSU
550W Antec Neo HE 550
Case
Antec P180
Cooling
Xigmatex Red Scorpion CPU Cooler. 3x120mm Fans
Keyboard
Logitech MX5000 Laser (Combo)
Mouse
Logitech MX5000 Laser (Combo)
Internet Speed
ADSL2+ (avg 10 Mbps Down, 0.80 Mbps up)
Other Info
Gigabyte GN-WP01GS 54g Wireless Lan Card
Back
Top