I'm trying to do a User Form that allows to insert a number in a ListBox, in case this number matches with one of sheet names, to select this work sheet. In case there is not a match, to give a message box, that the number was not found.
But i have a problem with defining that the ListBox text must be compared with Sheet names.
It looks in a following way:
The code is following:
Private Sub CommandButton1_Click()
Option Explicit
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
'this line i could not manage
If ws.Name Like "Tel*" Then
Sheets("Tabella Riepilogativa").Select
End If
Else: MsgBox "Phone number was not found"
Next
End Sub
Private Sub Label1_Click()
End Sub
Private Sub ListBox1_Click()
End Sub
Can someone help with it, please?
The Option Explicit belongs at the top of the file.
To find a sheet named "Tel{whatever the user entered in a TextBox}":
Private Sub CommandButton1_Click()
Dim ws As Worksheet, wsFound As Worksheet, searchFor As String
searchFor = "TEL" & UCase$(Trim$(TextBox1.Text))
For Each ws In ThisWorkbook.Sheets
If UCase$(ws.Name) = searchFor Then
Set wsFound = ws
Exit For
End If
Next
If wsFound Is Nothing Then
MsgBox "Not Found (I should probably be a label to save the user an unnecessary click)"
Else
wsFound.Select
End If
End Sub
Related
I will always have Workbook SQ_Macro_v1 as my main DB.
Two named Workbooks old_wk and new_wk will have different names, as I will choose them among the currently active WB on my computer.
I am going for a VBA ComboBox listing it all to a choice of mine, but in the end I am not able to store the name of my chosen WB.
Sub Macro1()
Dim main_wk, old_wk, new_wk As Workbook
Set main_wk = Workbooks("SQ_Macro_v1.xlsm")
Set old_wk = Workbooks(old_chosen) 'also tried UserForm1.ComboBox1.Value
Set new_wk = Workbooks(FileName_New) '
main_wk.Sheets("Main_DB").Range("C4").Value = old_wk.Worksheets("Sheet 1 Synthese").Range("C35").Value
As I run the UserForm code below, the old_chosen variable I set as empty in the main Sub. It seems that as I close the UserForm after it runs, nothing remains stored. Any clues to keep that variable saved after I close the UserForm?
Option Explicit
Public Sub UserForm_Activate()
Dim vWorkbook As Workbook
ComboBox1.Clear
For Each vWorkbook In Workbooks
ComboBox1.AddItem vWorkbook.Name
Next
End Sub
Public Sub CommandButton1_Click()
If ComboBox1.ListIndex <> -1 Then
Call YourMacro(ComboBox1)
End If
End Sub
Public Sub YourMacro(vWorkbookName As String)
Dim old_chosen As String
old_chosen = Me.ComboBox1.Value
MsgBox "You choose: " & Workbooks(vWorkbookName).Name
End Sub
The MsgBox pops up but no value is stored afterward:
Public Sub YourMacro(vWorkbookName As String)
Dim old_chosen As String
old_chosen = Me.ComboBox1.Value
MsgBox "You choose: " & Workbooks(vWorkbookName).Name
End Sub
You have declared the variable at procedure level and hence it is not visible after the userform is closed.
To make this variable available to all procedures in the project, precede it with the Public statement. Insert a module and paste this there
Public old_chosen As String
Having said that, I would recommend moving Macro1 inside the userform and handle the code from there after declaring the variable at module level
Hy, I am working on Excel VBA which will get me the Cell Address of the Checkbox I tick at the moment. I am able to get the cell address using following code.
Sub test()
Dim ws As Worksheet
Set ws = Sheets("Sheet 1")
Dim chk As CheckBox
For Each chk In ws.CheckBoxes
Debug.Print chk.TopLeftCell.Offset(0, 0).Address
Next chk
End Sub
This code returns the Cell address of all Check-boxes but I only want one cell address of the checkbox I tick. How can I achieve this. Currently I am trying events to achieve this but have no success so far.
Use the next way, please:
Copy the next Sub in a standard module:
Sub GetChkBoxAddress()
MsgBox ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address
End Sub
Copy the next code, too, and run it:
Sub textAssignMacroChkBox()
Dim sh As Worksheet, s As Shape, chkB As CheckBox
Set sh = ActiveSheet
For Each s In sh.Shapes
If TypeName(s.OLEFormat.Object) = "CheckBox" Then
s.OnAction = "GetChkBoxAddress"
End If
Next
End Sub
Try checking and unchecking the check boxes...
If you need to return the cell address only when the check box is checked (not for unchecking) the code can be adapted...
Edited:
In order to retrieve the cell address only for checking, you can use the next adapted Sub:
Sub GetChkBoxAddress()
If ActiveSheet.Shapes(Application.Caller).OLEFormat.Object.Value = 1 Then
MsgBox ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address
End If
End Sub
Suppose I have a listbox in a userform module. When shown, it contains three range names. Let's call them Range1, Range2, and Range3. When the user clicks on one, I want the corresponding range to be cleared and the range name to be deleted.
Thanks to anyone who can advise me on the code to do this.
Sorry I can't comment.But I think this link will help you.
http://www.ozgrid.com/forum/showthread.php?t=83396
I actually deleted the range. Alternatively, you could use Range(.Value).ClearContents to clear only the data or Range(.Value).Clear to clear the data and formatting from the range.
Private Sub ListBox1_Click()
With ListBox1
If Not IsNull(.Value) Then
On Error Resume Next
Range(.Value).Delete
ThisWorkbook.Names(.Value).Delete
On Error GoTo 0
End If
End With
RefreshRangeList
End Sub
Sub RefreshRangeList()
Dim n As Name
ListBox1.Clear
For Each n In ThisWorkbook.Names
ListBox1.AddItem n.Name
Next
End Sub
Private Sub UserForm_Initialize()
RefreshRangeList
End Sub
basically I have a userform which I would like to use to enter 2 data into another macro which I already have. The userform is as below:
Basically, I would like the OK button to be clicked and the data in the two boxes will be entered into another macro that I have. It would also be great if the OK button can help in a sense that it will prompt a warning if one of the boxes is not filled up.
So far, I do not have much of a code for this..
Private Sub UserForm_Click()
TextBox1.SetFocus
Sub Enterval()
End Sub
Private Sub TextBox1_Change()
Dim ID As String
ID = UserForm3.TextBox1.Value
End Sub
Private Sub TextBox2_Change()
Dim ID2 As String
ID2 = UserForm3.TextBox2.Value
End Sub
Private Sub OKay_Click()
Enterval
End Sub
Would appreciate any tips and help. Thanks!
My other macro
Private Sub CommandButton1_Click()
Dim Name As String
Dim Problem As Integer
Dim Source As Worksheet, Target As Worksheet
Dim ItsAMatch As Boolean
Dim i As Integer
Set Source = ThisWorkbook.Worksheets("Sheet1")
Set Target = ThisWorkbook.Worksheets("Sheet2")
Name = Source.Range("A3")
Problem = Source.Range("I13")
Do Until IsEmpty(Target.Cells(4 + i, 6)) ' This will loop down through non empty cells from row 5 of column 2
If Target.Cells(4 + i, 6) = Name Then
ItsAMatch = True
Target.Cells(4 + i, 7) = Problem ' This will overwrite your "Problem" value if the name was already in the column
Exit Do
End If
i = i + 1
Loop
' This will write new records if the name hasn't been already found
If ItsAMatch = False Then
Target.Cells(3, 6).End(xlDown).Offset(1, 0) = Name
Target.Cells(4, 6).End(xlDown).Offset(0, 1) = Problem
End If
Set Source = Nothing
Set Target = Nothing
End Sub
Thats the macro i have. As u said, i change the
othermacro
to CommandButton1_Click()
but it doesn't work
Quoting geoB except for one thing: when you .Show your UserForm from a main Sub, you can also .Hide it at the end and the macro that called it will continue its procedures.
Sub Okay_Click()
Dim sID1 As String, sID2 As String
' A little variation
If Me.TextBox1 = "" Or Me.TextBox2 = "" Then
MsgBox "Please fill all the input fields"
Exit Sub
End If
Me.Hide
End Sub
To address your TextBox, you can write in your main Sub UserForm3.TextBox1 for example
There is no need for an Enterval function. Instead, assume the user can read and follow instructions, then test whether that indeed is the case. Note that in your code ID and ID2 will never be used because they exist only within the scope of the subroutines in which they are declared and receive values.
To get started:
Sub Okay_Click()
Dim sID1 As String, sID2 As String
sID1 = UserForm3.TextBox1.Value
sID2 = UserForm3.TextBox2.Value
If Len(sID1 & vbNullString) = 0 Then
MsgBox "Box A is empty"
Exit Sub
End If
If Len(sID2 & vbNullString) = 0 Then
MsgBox "Box B is empty"
Exit Sub
End If
'Now do something with sID1, sID2
otherMacro(sID1, sID2)
End Sub
For your other macro, declare it like this:
Sub otherMacro(ID1, ID2)
...
End Sub
Also, the SetFocus method should occur in the form open event.
I work with sheets named; Rev00, Rev01, Rev02 etc - among other sheets in my workbook.
It would be very helpful (in order to compare the sub-summaries of different revisions) to set the exact same multiple-filter - as set in active sheet - in only all sheets beginning with "Rev".
This action should most wanted be activated by double Click in Range("A1") or somewhere like that (I dont want button on this one).
If possible next double Click in Range("A1") should reset filters.
Sub Test()
Dim ws As Worksheet, str As String
For Each ws In Worksheets
str = Left(ws.Name, 3)
If str = "Rev" Then
' set filter as in active.sheet
End If
Next ws
End Sub
... and I am stuck ....
will anyone guide me on this?
Yes it is possible. :) Here is a basic sample on how it should work.
Sub Test()
Dim ws As Worksheet, str As String
For Each ws In Worksheets
str = Left(ws.Name, 3)
If UCase(str) = "REV" Then
With ws
'~~> Remove any filters
.AutoFilterMode = False
With <YOUR RANGE>
.AutoFilter Field:=<RELEVANT FIELD>, _
Criteria1:=<YOUR CRITERIA>
'
'~~> Rest of the code
'
End With
'~~> Remove any filters
'.AutoFilterMode = False
End With
End If
Next ws
End Sub
Here you can see Autofilter in action :)
To call the above code by clicking Range A1, you can use the Worksheet_BeforeDoubleClick event.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A1")) Is Nothing Then
'
'~~> Your code goes here
'
Cancel = True
End If
End Sub
Regarding your query about making Range A1 respond as an ON/OFF switch, you can use a boolean variable s shown HERE