Why can't my command button perform two functions

cheoksoon

Board Regular
Joined
Aug 25, 2016
Messages
56
Hello,

I'm a total newbie when it comes to VBA. I've created an ActiveX Control button with the following code:

Code:
Private Sub Save_Changes_Click()
    Dim findRow As Long
    
    findRow = Application.WorksheetFunction.Match(Range("C3"), ThisWorkbook.Sheets("Patient table").Range("A:A"), 0)
    
    With Worksheets("Patient table")
        .Cells(findRow, 4) = Current_Medications
        .Cells(findRow, 6) = diagnosis_problem_list
             
    End With
    
End Sub

For some reason, the button only works for the first line ".Cells(findRow, 4) = Current_Medications" but not the second line ".Cells(findRow, 6) = diagnosis_problem_list".

Can somebody help point out my mistake?
 
That's fine, it simply confirms that you hadn't misspelt the name of the textbox.
Couple of other things to check
1) Do you have a Private Sub Worksheet_Change(ByVal Target As Range) event in that module? If so does that look at col F?
2) If you select the cell in col F where the value from the diagnosis_problem_list textbox should go, is there anything in the formula bar?

1. Yes I do!

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$3" Then
    Call refreshEverything
    End If
    
End Sub

2. No, it doesn't have a formula. But because it is on a worksheet called 'Patient Table', so column F is part of a table with a heading called 'Diagnosis'.

(edit: sorry I misunderstood the second question. To reiterate, the cell in Column F where the value from diagnosis_problem_list, contains mainly text data, like "Asthma, depression, Chronic pain". etc.)
 
Last edited:
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Ok the change event, won't be having an effect.
The reason I asked if you could see anything in the formula bar, is that in the past, I have encountered problems where Somebody had changed the font colour to white (or used CF to the same end). It therefore looks like nothing is happening, when it is.
 
Upvote 0
Ok, so it's looking like something else is causing the code not to work...

I'm very sure the text is not 'white'. :)
 
Upvote 0
This worked for me... I bet you can combine the "separate module" code below with the button click, but I didn't feel like fussing with it more.

For the command button:

Code:
Private Sub CommandButton1_Click()
    runThis Range("C3").Value2
End Sub

In a separate module:

Code:
Sub runThis(ByVal name As String)
    Dim findRow As Long
    Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Worksheets("Patient table")
    With ws1
        Dim txt1 As MSForms.TextBox: Set txt1 = .OLEObjects("Current_Medications").Object
        Dim txt2 As MSForms.TextBox: Set txt2 = .OLEObjects("diagnosis_problem_list").Object
        findRow = Application.WorksheetFunction.Match(name, Range("Names"), 0)
        .Cells(findRow, 4) = txt1.Text
        .Cells(findRow, 6) = txt2.Text
    End With
End Sub

kCnkRZQ.png
 
Last edited:
Upvote 0
Thanks everybody for your help.

I've decided to scrap the whole excel spreadsheet and start anew. Strangely, my original code worked like a charm, and so did yours veryamusing.

I'm disappointed that I had to start all over and couldn't troubleshoot the original spreadsheet... sigh. I think it must've been some very tiny thing I did which made the whole thing dysfunctional.

Anyways, I consider the issue solved. :)

I wish to thank everybody who helped.
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,213
Members
453,024
Latest member
Wingit77

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