Sheetname in combo box userform - excel

I'm looking for a way to add the sheetnames to my combobox.
Final result should be:
2 combo boxes next to each other were in the first box I can enter the sheetname and with the second box data from the sheet I just selected.
Thanks!

As a starting point, do the following:
Starting with a new Workbook, add 2 ComboBoxes to Sheet1, then add this code to the 'ThisWorkbook' code module:
Private Sub Workbook_Open()
Dim ws As Worksheet
'iterate through all worksheets and add each one to the combobox
For Each ws In Worksheets
Sheet1.ComboBox1.AddItem ws.Name 'add sheet name to combobox
Next ws
End Sub
Then add this code to the 'Sheet1' code module:
Private Sub ComboBox1_Change()
Dim i As Integer
If ComboBox1.Value <> "" Then
ComboBox2.Clear 'clear out the combobox
'add values from A1 to A5 to ComboBox2 from selected worksheet
For i = 1 To 5
ComboBox2.AddItem Worksheets(ComboBox1.Value).Range("A" & i).Value
Next i
End If
End Sub
Use the above code as a starting point, read each line and research what you don't understand until you can explain what each line does. Then you will be able to use the same concept in your solution.

Related

Copy and paste cell value to another sheet based on hyperlink click

Hope you can help me.
I want to copy and paste a cells value based on when you click a hyperlink on that cell.
So for example, I have a sheet called Form1, I want to click on an ID in column A it will then copy the value of that cell and paste it to B2 in sheet1 and then take me to sheet 1.
Currently I have a macro that allows me to click on an active cell and then press a button which then does what is mentioned above. I just think a hyperlink press would be more user friendly and would result in less errors.
Here is the code I have at the moment:
Sub Rectangle13_Click()
ActiveCell.Copy Destination:=Sheets(“Sheet1”).range(“B2”)
Worksheets(“Sheet1”).Activate
End Sub
Any help would be appreciated! Thank you
Worksheet FollowHyperLink
Copy this code to the sheet module of worksheet Form1. From there, run the second sub to convert the A column to hyperlinks.
Click away.
Option Explicit
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Not Intersect(Columns("A"), Target.Range) Is Nothing Then
Me.Parent.Worksheets("Sheet1").Range("B2").Value = Target.Range.Value
End If
End Sub
' Run this to create hyperlinks out of the values in column 'A'
' which will point to cell 'B2' in 'Sheet1'. You can then reformat
' the cells (remove underline, change font color, ...).
Private Sub CreateHyperlinks()
Dim lRow As Long: lRow = Range("A" & Rows.Count).End(xlUp).Row
Dim cell As Range
For Each cell In Range("A2:A" & lRow).Cells
cell.Hyperlinks.Add cell, "", "Sheet1!B2", , CStr(cell.Value)
Next cell
End Sub
It is inconvenient to use Hyperlik, I think. If you try changing the cell value, you cannot simple click it and write something... But if you want that, you can create such a hyperlink in the next way:
Sub testAddHyperlink()
Dim DestSh As Worksheet
Set DestSh = Sheets("Sheet1") 'you can use any sheet name here
ActiveSheet.Hyperlinks.Add Range("A1"), Address:="", SubAddress:="'" & DestSh.name & "'" & "!A1"
'it will keep the existing cell value
End Sub
Then, please copy the next event code in the sheet code module (where the hyperlink exists):
Option Explicit
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim rngCopy As Range, shDest As Worksheet
Set shDest = Sheets("Sheet1") 'you may use here any sheet name
shDest.Range("B2").value = Target.Parent.Value 'the sheet where the hyperlink targets, is already activated...
End Sub
If you want to use the BeforeDoubleClick approach, paste this code into your Form1 worksheet object in the VBA editor ...
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Cells.Count = 1 And Target.Cells(1, 1).Column = 1 Then
Cancel = True
With ThisWorkbook.Worksheets("Sheet1")
.Range("B2") = Target.Value
.Activate
End With
End If
End Sub
... naturally, this is a base example and you may need to modify it accordingly. For example, when you double click, you may want to ignore the first row if it's a header and not invoke the core parts of the logic.
That will then do what you want.

Comboboxes where in first one is list of the sheets and in second one is data from first column from selected sheet in first combobox

I'm new to VBA and I have some problems with how to do everything automatically. I have an active sheet for analysis and 10 sheets with data. Due to the type of analysis, I would like to use comboboxes to select data for analysis.
I would like to:
To use combobox1 on activesheet to select sheet from where I read the data.
To use combobox2 where is a list of names from the first column from the sheet selected in combobox1.
Example: Sheets 2-10 are names of countries, Column 1 are cities in those countries with data related with each city. So in combobox1 I select UK =sheet(4) , and then in combobox2 London= cell A40. Then i calculate B40 x C40 /F40...
I started like:
Public Sub Worksheet_Activate()
Dim x As Integer
totalcountries = Sheets.Count
For x = 2 To totalcountries
Me.Combobox1.AddItem Sheets(x).Name
Next x
End Sub
And:
Sub selectcity()
Sheets(1).combobox2.List = Sheets(4).Range("A2:A56").Value
End Sub
I don't know how to connect it. Thank you.
We could use the 'events' related to the comboboxes.
The method I have used here is:
(1 )When you click ComboBox1, it populates the names of all sheets except the ActiveSheet. You could now choose the sheet you desire from the dropdown.
(2 )When you TAB away from ComboBox1 (by pressing TAB), it populates ComboBox2 with the city names in the sheet you selected in ComboBox1.
I have named the ActiveSheet as "acSht" just for the purpose of explaining here. You could use your name. If you use a different name, replace "acSht" by the name you provide to the ActiveSheet.
Go to Developer and click 'Design Mode' under the 'Controls' section.
Now double-click ComboBox1. This will take you to Visual Basic Editor window. In the white pane that appears, You could see two dropdown boxes at the top. choose 'GotFocus' from the dropdown box on top right.
Use the following code.
Private Sub ComboBox1_GotFocus()
Worksheets("acSht").ComboBox1.Clear
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If Not ws.Name = "acSht" Then ThisWorkbook.Worksheets("acSht").ComboBox1.AddItem ws.Name
Next ws
End Sub
Next choose 'KeyDown' from the dropdown box on top right and use the following code.
Private Sub ComboBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Worksheets("acSht").ComboBox2.Activate
If KeyCode = vbKeyTab Then
Worksheets("acSht").ComboBox2.Clear
Dim ws As Worksheet, rng As Range, wsName As String
If Not ThisWorkbook.Worksheets("acSht").ComboBox1.Value = Empty Then
wsName = ThisWorkbook.Worksheets("acSht").ComboBox1.Value
Set ws = ThisWorkbook.Worksheets(wsName)
Else
Exit Sub
End If
For Each rng In ws.Range("A2", ws.Range("A" & Rows.Count).End(xlUp))
ThisWorkbook.Worksheets("acSht").ComboBox2.AddItem rng.Value
Next rng
End If
End Sub

How to display all sheet name in combobox | excel vba |

I have a scenario where i want to display all the sheet name in combobox
On Main sheet i have one dropdown combobox
this combobox should display all the sheet names present in excel
output :
Sheet1
Sheet2
Sheet3
.
.
.
How to modify this below code to get all sheet name in combobox
Private Sub ComboBoxpgname_DropButtonClick()
With Worksheets("A1")
ComboBoxpgname.List = .Range("B2:B" & .Cells(.Rows.Count, "A").End(xlUp).Row).Value
End With
End Sub
Try following code
Private Sub ComboBoxpgname_DropButtonClick()
Dim sh As Worksheet
ComboBoxpgname.Clear 'Clear combobox
For Each sh In ThisWorkbook.Worksheets
ComboBoxpgname.AddItem sh.Name
Next sh
End Sub

Only show visible sheets in ComboBox

I have created the following Excel-spreadsheet:
A B C D E
1 Sheet1
2 Sheet2
3 Sheet5
4 Sheet6
5 Sheet8
6 Sheet9
7
As you can see, in Column A I have listed some of the sheets within the Excel file.
Now, I use the below VBA to get this list into a ComboBox:
Sub UserForm_Activate()
ComboBox1.List = Sheet1.Range("A1:A8").Value
End Sub
All this works fine so far.
However, now it can happen that some of the sheets are not visible. Therefore, I also do not want them to appear in the ComboBox.
One solution I found was the answer from here. However, this VBA only works over the entire Excel file but as you can see in my list above some sheets (Sheet3, Sheet4, Sheet7) are excluded from the list. Those sheets should also remain excluded no matter if they are visible or not.
So for example, when I make Sheet2 and Sheet6 invisible the list in the ComboBox should automatically be adjusted and look like this:
Sheet1
Sheet5
Sheet8
Sheet9
What do I need to change in my VBA to achieve this?
The code below puts the sheets from the worksheet into a dynamic array and checks if it's visible, so you're not hard-coding the visible sheets. Therefore, excluding the sheets you don't want (Sheet3, Sheet4 and Sheet7) from the worksheet will also exclude them from your ComboBox.
Private Sub UserForm_Activate()
Dim sheet_list As Variant
sheet_list = Sheet1.Range("A1:A8").Value
'get the list of Sheets from Column A
Dim combo_list As Variant
combo_list = Array()
'create and empty array
Dim sheet_name As Variant
For Each sheet_name In sheet_list
'loop through sheets
If ThisWorkbook.Worksheets(sheet_name).Visible Then
'check if they are visible
Dim ub As Long
ub = UBound(combo_list) + 1
ReDim Preserve combo_list(ub)
'increment array dimension
combo_list(ub) = sheet_name
'add Sheet to array
End If
Next sheet_name
ComboBox1.List = combo_list
'populate the ComboBox
End Sub
Based on linked topic I modified the code from answer to reach what You want to achieve
Private Sub ChangeComboBox()
Dim xSheet As Worksheet
' Clear the combobox
ComboBox1.Clear
' Add the sheets again
For Each xSheet In ThisWorkbook.Sheets
'~~> Check if they are visible
If xSheet.Visible = xlSheetVisible Then
If xSheet.Name <> Sheet3.Name And xSheet.Name <> Sheet4.Name And xSheet.Name <> Sheet7.Name Then
ComboBox1.AddItem xSheet.Name
End If
End If
Next
End Sub

Copy Check Box to Every 5th Cell with Command Button

I have an issue which i can't solve.I wrote this code:
Private Sub CommandButton2_Click()
Sheets("sheet2").OLEObjects("CheckBox1").Copy
Sheets("sheet3").Range("V7").PasteSpecial
End Sub
This code copy a checkbox from (sheet 2) to (sheet 3) starting from V7 cell. Now I want the next time I press the command button to paste the data to cell V12,next time to V17 etc. My vba knowledge is not very good as you can see.
This code will see how many checkboxes are already in the sheet you are pasting to and add 5 rows for each check box, then paste five rows under the last one.
Private Sub CommandButton2_Click()
' copy checkbox
Sheets("sheet2").OLEObjects("CheckBox1").Copy
Dim wks As Worksheet
Set wks = Sheets("Sheet3")
Dim cb As OLEObject, i As Integer
'determine how many boxes are already there and get count of cell to paste to
i = 7
For Each cb In wks.OLEObjects
If InStr(1, cb.Name, "CheckBox") Then i = i + 5
Next
'paste new checkbox
Sheets("sheet3").Range("V" & i).PasteSpecial
End Sub
Use a global variable. These must be at the top of your sheet, module, or form code above all subs and functions.
Then use that as the row number in your range. Range("V" & lRow)
Private lRow As Long
Private Sub CommandButton2_Click()
'Let's check if this is the first time the button has been used.
If lRow = 0 then
lRow = 7
Else
'Increment the row from the one we wrote to last time.
lRow = lRow + 5
End If
'Do the copy
Sheets("sheet2").OLEObjects("CheckBox1").Copy
Sheets("sheet3").Range("V" & lRow).PasteSpecial
End Sub
I dont know what data you got between in Sheet(3).Range("V7") and Sheet(3).Range("V12")
but juste before you're PasteSpecial, you shoud keep track where was the last time you paste data in Sheets("sheets3") in a specific cell in Sheet("sheets3"), in exemple : Sheets("Sheet3").Range("A1")
Then you'll be able to pastespecial to this cell 5 row under like this :
Sheets("sheet3").Range(Sheets("Sheets3").Range("A1").Offset(5,0)).PasteSpecial
right after that you update the Sheets("Sheets3").Range("A1") = Sheets("sheet3").Range(Sheets("Sheets3").Range("A1").Offset(5,0)).Address
So this should do the work :
Private Sub CommandButton2_Click()
Dim oWsSource as Worksheet
Dim oWsDestination as Worksheet
Set oWsDestination = ThisWorkbook.Worksheet("Sheets3")
Set oWsSource = ThisWorkbook.Worksheet("Sheets2")
'Do the copy
oWsSource.OLEObjects("CheckBox1").Copy
oWsDestination.Range(oWsDestination.Range("A1").Value).Offset(5,0)).PasteSpecial
oWsDestination.Range("A1").Value = oWsDestination.Range(oWsDestination.Range("A1").Value).Offset(5, 0)).Address
End Sub
All the answers put the first checkbox but the next one put it again to the same cell as before.I don't know if its matter but I use excel 2010.

Resources