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

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

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.

How to loop through cells in a sheet to search another sheet?

I need to search "Check" Sheet for values from "Text" sheet.
How can I create a loop for "Text" sheet from cell value A2 to A4 one by one.
I have created below code
Option Explicit
Dim i As Integer
Dim WS, W As Range
Sub Search_fn()
Set WS = Range("B2")
WS = Application.WorksheetFunction.Search(Sheet2.Range("A2"), Range("A2"), 1)
End Sub
This shows 1004 error and I am also not able to crate the loop for all values.
Sheet "Check"
Sheet "text"

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

How to print a duplicated sheet with VBA?

When I use the code below
ThisWorkbook.Sheets("DB_Main").Copy _
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
I end up with a new sheet named DB_Main (2)
I am looking for a way to print these 2 sheets from my "Main" sheet using a button.
From sheet DB_Mainthe pages 4 and 5, and from sheet DB_Main (2)the pages 4,5,6 and 7.
How can I detect a copied sheet?
Any ideas how to get this to work?
If you copy a sheet, it gets automatically the active sheet. Assign this to a variable, after that you can do whatever you want:
Dim oldWs As Worksheet
Set oldWs = ThisWorkbook.Sheets("DB_Main")
oldWs.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Dim newWs As Worksheet
Set newWs = ActiveSheet
' Rename the sheet if you want
newWs.Name = "I am brand new"
' Print out old Main and copy
wb.Worksheets(Array(oldWs.Name, newWs.Name)).PrintOut
I try to loop all sheets and compare the cell A1 if its equal to 1.
Its work like that.
Private Sub CommandButton6_Click()
Dim allshts As Worksheet
For Each allshts In ActiveWorkbook.Worksheets
If allshts .Range("A1").Value = 1 Then
allshts .PrintOut From:=1, To:=1
End If
Next allshts
End Sub

Sheetname in combo box userform

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.

Resources