How to expand the hidden rows in excel using vba? - vba

I have a spreadsheet, which has 100 rows. Among these 100 rows, only 10 rows are required to be displayed at first, other 90 rows should be collapsed(hidden) at first. If user wants to read the whole 100 rows, he/she can click the button to expand the spreadsheet of 10 rows to 100 rows. How to implement this kind of function in VBA?

You could use a command button:
Private Sub CommandButton1_Click()
'// Label button "Show Rows"
With Me.CommandButton1
If .Caption = "Show Rows" Then
.Caption = "Hide Rows"
Rows("11:100").Hidden = False
Else
.Caption = "Show Rows"
Rows("11:100").Hidden = True
End If
End With
End Sub
Or a toggle button:
Private Sub ToggleButton1_Click()
'// Label Something Like "Show/Hide Rows"
Rows("11:100").Hidden = Not ToggleButton1.Value
End Sub

Related

Excel freezes after pasting text to a userform textbox

I have a vba userform text box with,
ControlSource = An Excel Cell
EnterKeyBehavior = True
MultiLine = True
MaxLength = 500
When I copy paste a text in the box such as a random text below:
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.
and step away for this text box (by clicking somewhere else such an another text box),
excel freezes (no response, cant open any excel file)
CPU usage for excel gets to ~50% (exceptionally high compared to normal use)
Do you know the reason for this freeze/behaviour?
Code behind the user form:
Private Sub CommandButton_PD_NP_Click()
If Me.Tb_PD_1.Value = "" Or Me.Tb_PD_2.Value = "" Then
MsgBox "Please specify your Business Area and Activity Name.", vbExclamation, "Error!"
Exit Sub
End If
Unload Me
Uf2_Security.Show
End Sub
Private Sub CommandButton_PD_PP_Click()
Unload Me
Uf1_Initiate.Show
End Sub
Private Sub UserForm_Initialize()
With Uf15_Project_Details
.Height = 357
.Left = 0
.StartUpPosition = 2
.Top = 0
.Width = 480
End With
End Sub

Create ComboBox's and AddItems to them all within the VBA code

I need to create ComboBox's and then AddItems to each ComboBox. This will all be done to a userform. I need to do this entirely within the VBA code, this is because each time the userform is opened new information will be shown.
this is what I have so far:
Private Sub UserForm_Initialize()
for i = 1 to size
Set CmbBX = Me.Controls.Add("Forms.ComboBox.1")
CmbBX.Top = ((90 * i) - 18) + 12 + 20
CmbBX.Left = 30
CmbBX.Text = "Please select an item from the drop down"
CmbBX.TextAlign = fmTextAlignCenter
CmbBX.Width = 324
CmbBX.Visible = False
CmbBX.Name = "ComBox2" & i
Next
end sub
the problem is, once each ComboBox is created its like its name isnt there. I cannot referance the combobox. this is what I have tried:
ComBox21.AddItems "Test1"
ComBox22.AddItems "Test2"
And it errors out. When I look at the UserForms function bar at the top of the screen (where I would usually select ComBox22_Change() for example), It shows that no ComboBoxes even exist!
Any Ideas on how to dynamically create and additems to comboboxes?
Thank you in advance
Here an sample of the code.
You need still to change it for you needs but this will be easy.
I have created a simple userform and one button to do test and it works fast.
To imput the comboboxes replace ".additem" with a loop to load each of them.
How to do that -- search in google
how to Populate a combobox with vba
You cannot refferance any controls on userform if they dont exist.
You need to search for them after creation and then modify them.
Example below with button code.
I think this should bring you to an idea how to manage this.
Option Explicit
Private Sub CommandButton1_Click()
Dim refControl As Control, frm As UserForm
Dim x
Set frm = Me
With Me
For Each x In Me.Controls
If TypeName(x) = "ComboBox" Then
Select Case x.Name
Case "cmbDemo3"
MsgBox "works!"
'here you can put your code
End Select
MsgBox x.Name
End If
Next x
End With
End Sub
Private Sub UserForm_Initialize()
Dim combobox_Control As Control
Dim i
For i = 0 To 5
Set combobox_Control = Controls.Add("forms.combobox.1")
With combobox_Control
.Name = "cmbDemo" & i
.Height = 20
.Width = 50
.Left = 10
.Top = 10 * i * 2
.AddItem "hihi" 'here you can add your input code
End With
Next i
End Sub

Expand and collapse rows using Excel VBA

I'm building a form with different sections. I do not want to use either a Command button or a Toggle button (their look is ugly) and since this is for clients I want it to be user friendly. Anyway so basically I want to build a macro that when they click on the shape it will collapse the unnecessary rows and expand the right rows.
So far this is what I was able to find... but it only applies to a command button.
Private Sub CommandButton1_Click()
With Me.CommandButton1
If .Caption = "Initial Request" Then
.Caption = "Hide Rows"
Rows("12:20").Hidden = False
Else
.Caption = "Initial Request"
Rows("12:200").Hidden = True
End If
End With
End Sub
This works perfectly... but, is there a way to transform this so that it can be added to a Module and therefore assigned to a shape?
Thank you very much for your help
You can add a shape, say a Rectangle and assign it a macro with this equivalent code to yours:
Sub Rectangle2_Click()
With Sheet3.Shapes("rectangle 2").TextFrame2.TextRange
If .Text = "Initial Request" Then
.Text = "Hide Rows"
sheet3.Rows("12:20").Hidden = False
Else
.Text = "Initial Request"
sheet3.Rows("12:20").Hidden = True
End If
End With
End Sub
Obviously in this example I added a rectangle shape to Sheet3 and it got the name "rectangle 2". You should adjust these names to your case.
Give this code a try:
Option Explicit
Sub Cloud_Click()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet2")
If ws.Shapes("Cloud 2").TextFrame2.TextRange.Text = "Initial Request" Then
ws.Shapes("Cloud 2").TextFrame2.TextRange.Text = "Hide rows"
ws.Rows("12:20").Hidden = False
Else
ws.Shapes("Cloud 2").TextFrame2.TextRange.Text = "Initial Request"
ws.Rows("12:20").Hidden = True
End If
End Sub
I sure hope that the following screen-cast will explain the rest.

Listbox Modification in VBA

I created a Userform in Word which imports 3 columns of data from an excel sheet, inserts it into bookmarks and in the name of the word document and saves it as a pdf.
Now I wanted to add a Listbox into the form to be able to add, modify and delete the inputs manually which are usually imported from the excel sheet .
I already figured out how to add data from 3 textboxes into a 3 Column Listbox but even after googling for hours I can't find a good way to modify selected data.
VB.net has the .selectedItem property, VBA does not. Can anybody give me an example how to modify a multi column listbox with the values of 3 textboxes?
Thanks in advance
You need to iterate through ListBox.Selected and check if it is True. Once you get a True one, you can process that item.
Sample code adds some items with columns and sets up a click event to run through the Selected items.
Private Sub ListBox1_Click()
Dim i As Integer
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
Debug.Print ListBox1.List(i, 0)
End If
Next i
End Sub
Private Sub UserForm_Initialize()
ListBox1.AddItem "test"
ListBox1.AddItem "test1"
ListBox1.AddItem "test2"
ListBox1.ColumnCount = 3
ListBox1.ColumnHeads = True
ListBox1.List(1, 0) = "change to 1,0"
ListBox1.List(1, 1) = "change to 1,1"
ListBox1.List(1, 2) = "change to 1,2"
End Sub
Picture of form with Immediate window after clicking each item in turn.

Select combobox if wrong item selected

I have an MS-Word 2013 document with several (legacy) formfields; some text boxes, some comboboxes. The first list item on all of the comboboxes is "(select one)" to let the end user know they need to make a selection (I did not draft or design this document, I've just been asked to write the VBA code for it). So I coded each to give a simple VBA message box, ran on exit, if that first selection was not changed, for example:
Public factor1 As Integer
Sub MyFormFieldFactor1()
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
Exit Sub
End If
If ActiveDocument.FormFields("cbofactor1").Result = "Yes" Then
factor1 = 1
Else
factor1 = 0
End If
End Sub
The word document automatically goes to the next formfield when you click ok on the message box. Through VBA, I want it to stay on the current formfield when "(select one)" is chosen. Bonus points if it stays on the current field and pulls up the list selection automatically.
Will this work:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
Exit Sub
End If
You can auto drop the list with something like:
SendKeys "%{down}", True
DoEvents
Full code:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
SendKeys "%{down}", True
DoEvents
Exit Sub
End If