VB code to insert into your spreadsheet
To insert, go into spreadsheet, do a "Tools","Macro",
"VB Editor" then do "Insert","Module" and paste the code
Strip Name - This
function strip the first,middle, and last from one field
cell formula example:
- =Switchname(a1) ---this function will take it from "Last,
First Middle" to "First Middle Last"
- =Firstname(a1) or proper(firstname(a1))
- =Middlename(a1)
- =Lastname(a1) Public Function Firstname(instring As String) As String
'MsgBox InStr(1, instring, " "), vbOKCancel
If InStr(1, instring, " ") Then
Firstname = Mid(instring, 1, InStr(1, instring, " "))
Else
Firstname = instring
End If
End Function
Public Function middlename(instring As String) As String
Dim firstfound As Integer
'clear it just in case
middlename = " "
'need to see if there are 2 spaces in the string
firstfound = InStr(1, instring, " ")
If firstfound Then
If InStr(firstfound + 1, instring, " ") Then
middlename = Mid(instring, firstfound + 1, InStr(firstfound + 1, instring, " ") - firstfound)
Else
middlename = " "
End If
End If
End Function
Public Function lastname(instring As String) As String
'MsgBox InStr(1, instring, " "), vbOKCancel
If InStr(1, instring, " ") Then
lastname = Right(instring, Len(instring) - findr(instring, " "))
Else
lastname = " "
End If
End Function
Public Function switchname(instring As String) As String
'this function will take a "lastname, Firstname Middlename" format and switch it "First Middle Last" without comma
cPos = InStr(1, instring, ",")
If cPos Then
switchname = Trim(Mid(instring, cPos + 1)) & " " & Trim(Left(instring, cPos - 1))
End If
End Function
Function findr(instring As String, findval As String) As Integer
Dim x As Integer
x = Len(instring)
'MsgBox (x)
Do Until x = 0
If Mid(instring, x, 1) = findval Then
findr = x
Exit Function
End If
x = x - 1
Loop
End Function
Public Function SEARCHR(instring As String) As Integer
Dim yBytes() As Byte
Dim i As Integer
yBytes = StrConv(instring, vbFromUnicode)
i = Len(instring) - 1
Do While i <> 0
If Chr(yBytes(i)) = " " Then
SEARCHR = i + 1
Exit Function
Else
i = i - 1
End If
Loop
End Function
combine columns - VB code (change the columns throughout the code)
this example takes A thru J and puts them in K
Public Sub CombineColumns() Dim SH As
Worksheet Dim rng As Range Dim srcRng As
Range Dim destRng As Range Dim col As Range
Dim LastRow As Long Dim iColour As Long
'NEW VARIABLE
Set SH = ActiveSheet Set rng =
SH.Range("A:J")
With SH iColour =
.Cells(1, "K").Interior.ColorIndex ''NEW CODE LINE
.Columns("K:K").ClearContents For
Each col In rng.Columns LastRow =
.Cells(Rows.Count, col.Column).End(xlUp).Row
Set srcRng = col.Cells(1).Resize(LastRow)
Set destRng = .Cells(Rows.Count, "K").End(xlUp)(2)
srcRng.Copy Destination:=destRng
Next col
On Error Resume Next
Range("K:K").SpecialCells(xlBlanks).Delete Shift:=xlUp
On Error GoTo 0
'NEW CODE LINE
Intersect(.Range("K:K"), .UsedRange).Interior.ColorIndex = iColour
End With
End Sub
Merging multiple sheets to
one Sub Merge() '-this will create a new
Master sheet '-this assumes all sheets are the same and each sheet has
a header row '-firsttime is used to copy the header row out of the
first sheet
Dim ws As Worksheet Dim
firsttime As Integer Dim i As
Integer firsttime =
0 On Error Resume Next Set ws =
Worksheets("Master") If ws Is Nothing
Then Sheets.Add
After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "Master" End
If
Sheets("Master").Activate
ActiveSheet.UsedRange.Offset(0).Clear
For Each ws In
ActiveWorkbook.Worksheets
If ws.Name <> ActiveSheet.Name
Then
If firsttime = 0
Then
ws.UsedRange.Offset(0).Copy
firsttime =
1
Else
ws.UsedRange.Offset(1).Copy
End
If
With Range("A65536").End(xlUp).Offset(1,
0)
.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=
_
True,
Transpose:=False '
.PasteSpecial Paste:=xlFormats, Operation:=xlNone, SkipBlanks:=
_ '
False,
Transpose:=False
End
With
End If
Next 'turn off the auto calc
With Application .Calculation =
xlCalculationManual
.ScreenUpdating = False
'select the range of all the data
Sheets("Master").Activate Range(Range("A1"),
ActiveCell.SpecialCells(xlLastCell)).Select 'We work
backwards because we are deleting rows. For i =
Selection.Rows.Count To 1 Step -1
If WorksheetFunction.CountA(Selection.Rows(i)) = 0
Then
Selection.Rows(i).EntireRow.Delete
End If Next
i 'turn back on the auto
calc .Calculation =
xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
You are here: Home-Computer Tips & Help-PC-MS Office-Excel/Macro/VB code
|