+ Reply to Thread
Results 1 to 4 of 4

Button functionality separation

Hybrid View

  1. #1
    Registered User
    Join Date
    08-05-2015
    Location
    Stockholm, Sweden
    MS-Off Ver
    2013
    Posts
    7

    Button functionality separation

    I'm new to Excel macros & VBA and I'm trying to invoke Shell commands from 2 buttons. They have similar functionality except for one line in them. I simply copy/pasted Button1 into another cell and modified it. The problem I'm having is that Button1 works perfectly but Button2 does nothing. When I try to modify the script for one button, the other stops working (and vice versa) and afterwards it looks like the same code is somehow copied to both buttons (not wanted). My underlying script for both buttons:

    Button1 (works):

    Option Explicit

    Private Sub CommandButton1_Click()



    Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
    myFile = "C:\test\source-destination.txt"
    'myFile = Application.GetOpenFilename()

    Dim RetVal

    RetVal = Shell("C:\test\launch-this.bat", 1)

    RetVal = Shell("C:\Program Files\Wireshark\tshark.exe -i ""Local Area Connection"" -a duration:10 -w c:\test\streamcapture.cap", 1)

    RetVal = Shell("C:\Program Files\Wireshark\tshark.exe -r c:\test\streamcapture.cap -R ""rtmpt.handshake.c0 == 03"" -w c:\test\handshake.cap -2", 1)

    RetVal = Shell("C:\Program Files\Wireshark\tshark.exe -r c:\test\handshake.cap -E separator=, -t ad > c:\test\source-destination.txt", 1)



    Open myFile For Input As #1
    Do Until EOF(1)
    Line Input #1, textline
    text = text & textline
    Loop
    Close #1

    posLat = InStr(text, "latitude")
    posLong = InStr(text, "longitude")

    Range("D5").Value = Mid(text, posLat + 47, 13)
    Range("E5").Value = Mid(text, posLat + 60, 14)

    Range("D6").Value = Mid(text, posLat + 196, 13)
    Range("E6").Value = Mid(text, posLat + 210, 14)



    End Sub


    /////////////////////////////////////////////////////////////////////////////

    Button2 (does not react. yes I did try changing the button name in properties)

    Option Explicit

    Private Sub CommandButton1_Click()



    Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
    myFile = "C:\test\source-destination.txt"
    'myFile = Application.GetOpenFilename()

    Dim RetVal

    RetVal = Shell("C:\test\launch-this.bat", 1)

    RetVal = Shell("C:\Program Files\Wireshark\tshark.exe -i ""Local Area Connection"" -a duration:10 -w c:\test\streamcapture.cap", 1)

    RetVal = Shell("C:\Program Files\Wireshark\tshark.exe -r c:\test\streamcapture.cap -R ""rtmpt.handshake.c0 == 03"" -w c:\test\handshake.cap -2", 1)

    RetVal = Shell("C:\Program Files\Wireshark\tshark.exe -r c:\test\handshake.cap -E separator=, -t ad > c:\test\source-destination.txt", 1)



    Open myFile For Input As #1
    Do Until EOF(1)
    Line Input #1, textline
    text = text & textline
    Loop
    Close #1

    posLat = InStr(text, "latitude")
    posLong = InStr(text, "longitude")

    Range("D5").Value = Mid(text, posLat + 47, 13)
    Range("E5").Value = Mid(text, posLat + 60, 14)

    Range("D6").Value = Mid(text, posLat + 196, 13)
    Range("E6").Value = Mid(text, posLat + 210, 14)



    End Sub

    Private Sub CommandButton2_Click()

    End Sub


    In summary, I would like to the buttons to be independent of each other since they have different functionality.
    Last edited by wifinut; 08-05-2015 at 05:21 PM.

  2. #2
    Forum Expert daffodil11's Avatar
    Join Date
    07-11-2013
    Location
    Phoenixville, PA
    MS-Off Ver
    MS Office 2016
    Posts
    4,465

    Re: Button functionality separation

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE] [/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    The names of the subroutines has to reflect the name of the button.
    You may indeed have a Button1 and a Button2, but you're using procedures both named Button1.

    Button1 should have code like this:
    Private Sub CommandButton1_Click()
     'do stuff
    End Sub
    Button2
    Private Sub CommandButton2_Click()
     'do stuff
    End Sub
    Make Mom proud: Add to my reputation if I helped out!

    Make the Moderators happy: Mark the Thread as Solved if your question was answered!

  3. #3
    Registered User
    Join Date
    08-05-2015
    Location
    Stockholm, Sweden
    MS-Off Ver
    2013
    Posts
    7

    Re: Button functionality separation

    Thank you. I have attempted the above.

    However, my procedure is as follows:

    Change properties of both buttons (Button1 = CommandButton1, Button2 = CommandButton2)

    So my resulting code is similar to the following:


    Button1
    Private Sub CommandButton1_Click()
     'do stuff
    End Sub
    Button2
    Private Sub CommandButton2_Click()
     'do OTHER stuff
    End Sub
    The problem: When I made changes to the code in button2, it gets mirrored in button1 and vice versa. I'm not sure how/why they are linked.
    Last edited by wifinut; 08-06-2015 at 02:31 PM.

  4. #4
    Registered User
    Join Date
    08-05-2015
    Location
    Stockholm, Sweden
    MS-Off Ver
    2013
    Posts
    7

    Re: Button functionality separation

    Thank you. I have attempted the above.

    However, my procedure is as follows:

    Change properties of both buttons (Button1 = CommandButton1, Button2 = CommandButton2)

    So my resulting code is similar to the following:


    Button1
    
    Private Sub CommandButton1_Click()
     'do stuff
    End Sub

    Button2
    
    Private Sub CommandButton2_Click()
     'do OTHER stuff
    End Sub
    The problem: When I made changes to the code in button2, it gets mirrored in button1 and vice versa. I'm not sure how/why they are linked.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Pop up with policy information with a "voting button" functionality
    By Postlki1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-13-2013, 08:28 AM
  2. How to work with button functionality of a workbook in web?
    By ravikumar00008 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-25-2012, 06:51 AM
  3. Auto send button/functionality!?
    By Wilgoss in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-20-2012, 01:21 PM
  4. Cannot figure out how to create macro to work with button for needed functionality
    By chairokeeman in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-11-2011, 11:59 AM
  5. Printing a Workbook with a push Button functionality
    By dreams in forum Excel General
    Replies: 2
    Last Post: 05-12-2009, 05:21 PM
  6. Name Separation
    By zbatchel in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-13-2008, 02:10 PM
  7. Tab Separation
    By ProfDavis in forum Excel General
    Replies: 3
    Last Post: 05-02-2007, 11:59 AM
  8. [SOLVED] Cell Separation
    By Freshman in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 04:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1