Tuesday, February 6, 2007

Programming in VB for Excel


I always forget how to do simple things to program in VB for Excel, so here are some code snippets that should get people started. (Or me, the next time I forget how to do this)

Also, if you are working with buttons, you need to click on the little teal triangle/ruler/pen icon in the control toolbox to switch between button editing and clicking.


Cells(2 3).cut
Cells(2 4).Select
ActiveSheet.Paste


Cells(Row, 3).copy
Cells(Row, 4).Select
ActiveSheet.Paste


Cells.Select
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Columns("A:A").Select
Selection.Insert Shift:=xlToRight


Dim tag As String
Row = 1

Do While (Row < tag =" Cells(Row,"> 0) Then
If (Cells(Row, 3).Text <> "") Then
Cells(Row, 3).cut
Cells(Row, 2).Select
ActiveSheet.Paste
End If
Cells(Row, 2).Font.ColorIndex = 49
End If

Row = Row + 1
Loop



Sub convertDecimal()
Selection.NumberFormat = "0.00"
End Sub


Sub processTimeline()
subA
subB
'comment
End Sub


Sub MyPosition()
myRow = ActiveCell.Row
myCol = ActiveCell.Column
MsgBox myRow & "," & myCol
End Sub

Private Sub findEmpty()
ActiveCell.Row = 1
ActiveCell.Col = 1
Do
ActiveCell.Row = ActiveCell.Row + 1
While (ActiveCell.Value <> "")
End Sub

Private Sub CommandButton20_Click()
Range("A1").Select
ActiveCell = Timer ' sets A1 to the current time
Range("B1").Select
ActiveCell = "Start Transcript"
End Sub


Private Sub insert_timed_comment()
Range("A1").Select
startT = ActiveCell.Value
ActiveCell.Offset(Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row - 1, 0).Select
ActiveCell.Select
ActiveCell = (Timer - startT) * 1000
ActiveCell.Offset(0, 1).Select
ActiveCell = Range("G21").Value
ActiveCell.Offset(0, 1).Select
ActiveCell = "something about this value"
Range("A1").Select
End Sub