[VBA] Display a variable in a label

Solved
JC_56 Posted messages 4 Status Member -  
JC_56 Posted messages 4 Status Member -
Hello,

In Excel, I created a macro that tests cells. When a cell is below a certain value, a userform pops up asking the user if they want to delete or keep that cell (by checking yes or no).

For example:
------------------------------------------------------------------------------------------------------------------------------
The value of the cell is "X". Do you want to keep this value for future operations?

YES NO
------------------------------------------------------------------------------------------------------------------------------

In short, everything works well except that in the Label of my userform, I can't display the value of the concerned cell, and under these conditions, the user doesn't know what they are going to delete or keep.

To summarize, how can I display the value of a cell or a variable in a Label of a Userform?

Thank you in advance! :)
Configuration: Windows XP Firefox 2.0.0.14

3 answers

  1. zavenger Posted messages 817 Status Member 161
     
    Hi,

    does something like

    UserForm1.Label1.Caption = Sheets("Sheet1").Range("A2").Value

    work for you?
    8
  2. JC_56 Posted messages 4 Status Member 13
     
    Hi,

    Thank you Zavenger, it's exactly what I was looking for!

    Problem definitely solved. :)
    7
  3. JC_56 Posted messages 4 Status Member 13
     
    Good evening,

    Thank you, Zavenger, for your response that partially solved my problem.

    I say "partially" because I was actually expecting a response with the same syntax as the message boxes (MsgBox "You have decided to delete the cell " & i & "" where i represents the variable). This allows for directly including the value of the variable in the sentence.

    Your answer allows assigning a value to a label. In my userform, I thus created 3 Labels:
    Label1: first part of the sentence: "Do you want to delete the cell"
    Label2: contains the variable
    Label3: end of the sentence: "?".

    By putting the 3 labels together, we obtain a sentence with a variable.

    In my macro, I add the assignment of Label 2 to the variable before displaying the userform:

    UserForm1.Label2.Caption = Sheets("Sheet1").Range("a2").Value
    Userform1.Show

    Well, I don't know if it's the most elegant way to code this, but in the end, it works ;)
    4
    1. zavenger Posted messages 817 Status Member 161
       
      In this case, simply do

      UserForm1.Label1.Caption = "Do you want to delete the cell " & Sheets("Sheet1").Range("a2").Value & " ?"

      and you only have one label.
      0