VBA UserForm.Lable.Caption VALUE FOR INSERT CELL

kar2rost

Banned user
Joined
Jul 6, 2022
Messages
20
Office Version
  1. 2019
Platform
  1. Windows
Hello my friends
I have a form that contains these buttons
I want the form to open and to click on each of the buttons, put its caption in the cell
I tested the following code

VBA Code:
' Function to edit the value in column B for a given cell.
Sub EditValue(cell)
    ' Initialize the UserForm1 object
    Dim UserForm1 As UserForm1
    Set UserForm1 = New UserForm1
   
    UserForm1.Show
    Set UserForm1 = Nothing
  
        cell.Offset(0, 1).Value = UserForm1.ActiveControl.Caption
 
End Sub

And I get the following error,
"object variable or with block variable not set"

code of button
VBA Code:
Private Sub CommandButton1_Click()
Unload Me
End Sub

what is the reason?
 

Attachments

  • FORM.jpg
    FORM.jpg
    78.8 KB · Views: 4

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Is "cell" a variable that you don't show as being declared in your code? It's not a valid reference to any cell if that's what you're trying to do. There is "cells" and variations of the word cell (such as ActiveCell) but there is no "cell". Also, AFAIK, Offset is a property of a range object, not a cell.
 
Upvote 0
Is "cell" a variable that you don't show as being declared in your code? It's not a valid reference to any cell if that's what you're trying to do. There is "cells" and variations of the word cell (such as ActiveCell) but there is no "cell". Also, AFAIK, Offset is a property of a range object, not a cell.
how to fix this .can you help me by code? I want when click on each of button in form caption of button or value that ref to button insert in the cells
 
Last edited:
Upvote 0
try to replace
VBA Code:
Dim UserForm1 As UserForm1
with
VBA Code:
Dim UserForm1 As UserForm
 
Upvote 0
I want the form to open and to click on each of the buttons, put its caption in the cell

Not fully sure understood your requirement but see if this update to your code does what you want

Place all code in your userforms code page

Rich (BB code):
Private Sub CommandButton1_Click()
    EditValue Range("A2")
End Sub

Private Sub CommandButton2_Click()
    EditValue Range("A10")
End Sub

Private Sub CommandButton3_Click()
    EditValue Range("A20")
End Sub

Private Sub CommandButton4_Click()
    EditValue Range("A30")
End Sub

Private Sub CommandButton5_Click()
    EditValue Range("A40")
End Sub


' Function to edit the value in column B for a given cell.
Sub EditValue(ByVal cell As Range)
        cell.Offset(0, 1).Value = ActiveControl.Caption
End Sub

You will need to change the specified ranges in each of the Click events as required

Dave
 
Upvote 0

Forum statistics

Threads
1,215,092
Messages
6,123,063
Members
449,090
Latest member
fragment

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