You need to add a ListBox in a UserForm and copy this two simple procedures:
Private Sub UserForm_Initialize()
Dim sht As Worksheet
For Each sht In ActiveWorkbook.Worksheets
ListBox1.AddItem sht.Name
Next sht
End Sub
Private Sub ListBox1_Click()
Sheets(ListBox1.ListIndex + 1).Activate
End Sub
Once you show the UserForm it will add the names of the sheets in the ListBox, then when you click on one of the sheets name in the ListBox it will select the corresponding sheet in the WorkBook. I hope it works
Thursday, September 13, 2007
Change the worksheet name
Change the worksheet name by asking user
Private Sub Workbook_Open()
Dim name As String
name = InputBox("Sheet name, please?")
Sheets(1).Select
ActiveSheet.name = name
Range("A1").Value=name
End Sub
Private Sub Workbook_Open()
Dim name As String
name = InputBox("Sheet name, please?")
Sheets(1).Select
ActiveSheet.name = name
Range("A1").Value=name
End Sub
To split firstname,middle name & lastname
In C4 (first name) use:
=LEFT(B4,FIND(" ",B4)-1)
In D4 (middle name or initial) use
=IF(ISERR(FIND(" ",B4,1+FIND(" ",B4))),"",MID(B4,FIND(" ",B4)+1,FIND(" ",B4,1+FIND(" ",B4))-FIND(" ",B4)-1))
In E4 (last name) use
=IF(ISERR(FIND(" ",B4,1+FIND(" ",B4))),MID(B4,FIND(" ",B4)+1,LEN(B4)),MID(B4,1+FIND(" ",B4,1+FIND(" ",B4)),LEN(B4)))
Note:
a] This will put a blank in middle name if there are only first and last names.
b] It will not work if you have Jr. or II/III in the names and no middle initial.
You could extrapolate these formulas to handle it by looking at the number of blanks though.
=LEFT(B4,FIND(" ",B4)-1)
In D4 (middle name or initial) use
=IF(ISERR(FIND(" ",B4,1+FIND(" ",B4))),"",MID(B4,FIND(" ",B4)+1,FIND(" ",B4,1+FIND(" ",B4))-FIND(" ",B4)-1))
In E4 (last name) use
=IF(ISERR(FIND(" ",B4,1+FIND(" ",B4))),MID(B4,FIND(" ",B4)+1,LEN(B4)),MID(B4,1+FIND(" ",B4,1+FIND(" ",B4)),LEN(B4)))
Note:
a] This will put a blank in middle name if there are only first and last names.
b] It will not work if you have Jr. or II/III in the names and no middle initial.
You could extrapolate these formulas to handle it by looking at the number of blanks though.
insert data into Access Table from Excel
Bellow code lists two functions -
- First to insert data into Access Table from Excel
- Second to fetch data from the Access table to Excel (Sheet 2)
Sub InsertRecord()
Dim Com As New ADODB.Command
Set Com = New ADODB.Command
Dim ConStr As String, ComStr As String
ConStr = "Provider = Microsoft.jet.oledb.4.0; data source = H:\trial.mdb; Persist Security Info=False"
ComStr = "Insert into PersonalRecords(Name, Age, DOB) Values ('" + Range("B2").Text + "','" + Range("B3").Text + "','" + Range("B4").Text + "')"
Com.ActiveConnection = ConStr
Com.CommandText = ComStr
Dim RecordsAffected As Integer
Call Com.Execute(RecordsAffected, CommandTypeEnum.adCmdText Or ExecuteOptionEnum.adExecuteNoRecords)
End Sub
''''
Sub FatchRecords()
Dim Com As New ADODB.Command
Set Com = New ADODB.Command
Dim ConStr As String, ComStr As String
ConStr = "Provider = Microsoft.jet.oledb.4.0; data source = H:\trial.mdb; Persist Security Info=False"
ComStr = "Select * from PersonalRecords"
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Call rs.Open(ComStr, ConStr, adOpenForwardOnly)
Call Worksheets(2).Range("A2").CopyFromRecordset(rs)
With Sheet2.Range("A1")
Dim offset As Integer
offset = 0
Dim Field As ADODB.Field
For Each Field In rs.Fields
.offset(0, offset).Value = rs.Fields(offset).Name
offset = offset + 1
Next Field
End With
Sheet2.UsedRange.EntireColumn.AutoFit
End Sub
- First to insert data into Access Table from Excel
- Second to fetch data from the Access table to Excel (Sheet 2)
Sub InsertRecord()
Dim Com As New ADODB.Command
Set Com = New ADODB.Command
Dim ConStr As String, ComStr As String
ConStr = "Provider = Microsoft.jet.oledb.4.0; data source = H:\trial.mdb; Persist Security Info=False"
ComStr = "Insert into PersonalRecords(Name, Age, DOB) Values ('" + Range("B2").Text + "','" + Range("B3").Text + "','" + Range("B4").Text + "')"
Com.ActiveConnection = ConStr
Com.CommandText = ComStr
Dim RecordsAffected As Integer
Call Com.Execute(RecordsAffected, CommandTypeEnum.adCmdText Or ExecuteOptionEnum.adExecuteNoRecords)
End Sub
''''
Sub FatchRecords()
Dim Com As New ADODB.Command
Set Com = New ADODB.Command
Dim ConStr As String, ComStr As String
ConStr = "Provider = Microsoft.jet.oledb.4.0; data source = H:\trial.mdb; Persist Security Info=False"
ComStr = "Select * from PersonalRecords"
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Call rs.Open(ComStr, ConStr, adOpenForwardOnly)
Call Worksheets(2).Range("A2").CopyFromRecord
With Sheet2.Range("A1")
Dim offset As Integer
offset = 0
Dim Field As ADODB.Field
For Each Field In rs.Fields
.offset(0, offset).Value = rs.Fields(offset).Name
offset = offset + 1
Next Field
End With
Sheet2.UsedRange.EntireColumn.AutoFit
End Sub
Get data from MS Access using ADODB.
Use the below code to retrieve the data from access database.
Dim DB as new ADODB.DATABASE
dim RS as New ADODB.Recordset
set db = ("Some File Name")
set rs = db.openrecordset("Tablename")
while not rs.eof
cells(x,1) = rs(0)
rs.movenext
wend
Dim DB as new ADODB.DATABASE
dim RS as New ADODB.Recordset
set db = ("Some File Name")
set rs = db.openrecordset("Tablename")
while not rs.eof
cells(x,1) = rs(0)
rs.movenext
wend
Excel Speaks
You can make Excel speak by using the following Macro...
In Cell "A1" type text, you want Excel to speak and than run the following macro
Sub SayThisCell()
Cells(1, 1).Speak
End Sub
Hope you all enjoy it.
In Cell "A1" type text, you want Excel to speak and than run the following macro
Sub SayThisCell()
Cells(1, 1).Speak
End Sub
Hope you all enjoy it.
Saturday, September 1, 2007
vbYesNo
There are times you may want users to click Yes or No. Just insert this line of code. Here the Select Case statement is used.
YesNo = MsgBox("This macro will ... Do you want to continue?", vbYesNo + vbCritical, "Caution")
Select Case YesNo
Case vbYes
'Insert your code here if Yes is clicked
Case vbNo
'Insert your code here if No is clicked
End Select
Subscribe to:
Posts (Atom)