VBA code not pulling up my form when button is pressed - excel

I want to use VBA code to pull up my form. Right now the code is causing a runtime error on Sub UserForm_Initilize(). I don't know a lot about VBA. I know I might need to do something like column.selectAll but I am not sure assume I am trying to select the first 4 columns for my form. Thanks
Sub UserForm_Initialize()
Me.TextBox1.Value = Sheet1.Range("H" & Rows.Count).End(xlUp).Value
End Sub
Sub Rectangle1_Click()
UserForm1.Show
End Sub

Related

Excel freeze while entering a value into textbox within the userform

I have an Excel user form with two text boxes. When I try to enter a value in one of them, Excel freezes and stops working. But the strange thing is that this works on other computers in the office which are exactly the same.
It is a really simple user form with two text boxes, set to change, and one button.
I'm not sure if it's because of Excel, maybe something in Windows.
I have Excel 2016 and Windows 10 on my computer.
Has anyone had this problem?
Private Sub applyCommandButton_Click()
Range("A3").Value = TextBox1.Value & " " & TextBox2.Value
End Sub
Private Sub TextBox1_Change()
Range("A1").Value = TextBox1.Value
End Sub
Private Sub TextBox2_Change()
Range("A2").Value = TextBox2.Value
End Sub

Check Box VBA does not save

The VBA code below is meant to allow only certain users to tick & un-tick a checkbox. However, the problem is that if I check the box and then close the spreadsheet, when I re-open the excel file the 'tick' is no longer there. It's like the code does not save the 'tick' action. Basically, if I check the box I want that to stay like that, even after closing the spreadsheet. In the VBA code below i added ThisWorkbook.Save in order to save the "Tick" action but it simply saves the spreadsheet instead of saving the "Tick" in the check box. Could you please advise what's wrong in my code? I've asked this question before but unfortunately nobody seemed to be able to find a solution. thanks so much
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
If (UCase(Environ("username")) = "TESTNAME") Then
'Do nothing
Else
'Uncheck because user not matching
CheckBox1.Value = False
MsgBox ("You are not authorized to tick this box.")
End If
End If
ThisWorkbook.Save
End Sub
on my sheet1 I have a commandButton (bShow) and in the code behind:
Private Sub bShow_Click()
UserForm1.Show
End Sub
then I have a UserForm named UserForm1 with a checkbox (named checkBox1) and a button named saveButton, and with a code behind:
Private Sub saveButton_Click()
Sheets(1).Cells(1, 1).Value = UserForm1.CheckBox1.Value
End Sub
with this setup in cell("A1") appears TRUE or FALSE depending on the checkbox state
i hope it helps
EDIT1:
by opening the Form reading the value from the sheet:
Private Sub UserForm_Activate()
UserForm1.CheckBox1.Value = Sheets(1).Cells(1, 1).Value
End Sub
EDIT2:
be aware of error handling (eg.: what if cell value is neither TRUE nor FALSE) But that I leave to you

Run popup message in protected excel sheet when the value of two cells are equal

I want to confirm with a popup message and sound effect in a protected Excel 2010 worksheet when the values of two cells are the same. I have tried this formula in data validation:
IF(D4=D5,beepnow(),"")
but it does not run. Can anyone assist with the formula or a VBA code replacement instead? Thanks!
Here's a program that will run whenever you change to the worksheet... which may get pretty annoying... You should get the idea though and be able to modify it to fit your needs.
Private Sub Worksheet_Activate()
If Range("D4").Value = Range("D5").Value Then
Beep
MsgBox "Equal", vbInformation, "Check"
End If
End Sub
You should just be able to copy and paste it into your worksheet class.
If you go to the VB COde Window, select the relevant sheet, you can have the following
Private Sub Worksheet_Change(ByVal Target As Range)
If (Range("D4:D4").Cells(1, 1)) = Range("D5:D5").Cells(1, 1)) Then
MsgBox ("Hi")
End If
End Sub
The worksheet activate will run the code when you select this sheet.
The Worksheet_change will run the code when you make a change in this sheet.
IF you want to have the check only when D4/D5 is modified
If Target.Address = "$D$4" Or Target.Address = "$D$5" Then
If (Range("D4:D4").Cells(1, 1)) = Range("D5:D5").Cells(1, 1)) Then
MsgBox ("Hi")
End If
End If

define an input suggestion with vba in excel

im not really sure how to word this but im going to try to describe the issue.
I am building a simple excel spreadsheet to keep track of my spending. each cost i input needs to be classified into a category. I have about 20 different categories and forget what they are.
Rather than scrolling up I would like to script a continuous suggestion prompt when i select a cell in a specific column of the spreadsheet. I want to just be able to read off the suggestion as to which category to input (manually).
See nothing too complex, i just know very little about vba syntax and how to phrase the issue. I think i need like a continuous running sub? again not sure what that is called.
Any help is appreciated.
Thank you
Ken
You could you a simple userform to select and parse data selection into the row you clicked.
First use an Event when you click in the column you want the reminder to pop up in. In this case I choose "D".
Private Sub Worksheet_Change(ByVal Target As Range)
Dim arr
If Not Intersect(Target, Range("D:D")) Is Nothing Then
Application.EnableEvents = 0
UserForm1.Show
Target = a
Application.EnableEvents = 1
End If
End Sub
Then load the user form with your reminder data and select the reminder data you need from a userform combobox.
Private Sub UserForm_Initialize()
Dim arr
arr = Array("Unit1", "Unit2", "Unit3", "Unit4", "Unit5", "Unit6", _
"Unit7", "Unit8", "Unit9", "Unit10", "Unit11", "Unit12", _
"Unit13", "Unit14", "Unit15", "Unit16", "Unit17", "Unit18", _
"Unit19", "Unit20")
Me.ComboBox1.List = arr
End Sub
Private Sub ComboBox1_Change()
a = ComboBox1.Value
Unload Me
End Sub
And you will need to put a public variable in a standard module to store the data you want pasted in your sheet.
Public a As String
Be sure to change any control names to suit you.

Excel VBA Drag and Drop

I'm having some difficulty finding a workable solution (been looking for 2 days now).
Hopefully you can help me figure it out.
Purpose - I'm trying to use VBA to drag and drop text between listboxes
(see image)
Note: I know there are Pivot Wizards already, I'm not so interested in them (long story)
Question
Is there any solution that you know of that could help me move "Column A" to any of the other listbox?
If you don't know of a solution, a blog or site might be helpful as well.
Further to my comments above here is the most simple way to do it.
Create a Userform with 2 Listboxes and 1 Command Button as shown in the below image.
And paste this code in the Userform Code area
Dim i As Long
Private Sub UserForm_Initialize()
For i = 1 To 10
ListBox1.AddItem i
Next i
End Sub
Private Sub CommandButton1_Click()
If ListBox1.ListIndex = -1 Then
MsgBox "Please select an item from listbox1"
Exit Sub
End If
ListBox2.AddItem ListBox1.List(ListBox1.ListIndex)
ListBox1.RemoveItem (ListBox1.ListIndex)
End Sub
HTH

Resources