Skip to content

Instantly share code, notes, and snippets.

@kubrick06010
Created February 16, 2012 23:53
Show Gist options
  • Select an option

  • Save kubrick06010/1848899 to your computer and use it in GitHub Desktop.

Select an option

Save kubrick06010/1848899 to your computer and use it in GitHub Desktop.
snippets for excel and VBA
Sub AddWorksheet()
Worksheets.Add().Name = "MySheet"
End Sub
Sub AddAsLastWorksheet()
Worksheets.Add (After:=Worksheets(Worksheets.Count)).Name = "MySheet"
End Sub
Sub AddXWorksheets()
Worksheets.Add After:=Worksheets(Worksheets.Count), Count:=4
End Sub
Sub CreateCSV()
' Create CSV file from sheet
Dim rCell As Range
Dim rRow As Range
Dim sOutput As String
Dim sFname As String, lFnum As Long
'Open a text file to write
sFname = "C:\MyCsv.csv"
lFnum = FreeFile
Open sFname For Output As lFnum
'Loop through the rows'
For Each rRow In ActiveSheet.UsedRange.Rows
'Loop through the cells in the rows'
For Each rCell In rRow.Cells
sOutput = sOutput & rCell.Value & ";"
Next rCell
'remove the last comma'
sOutput = Left(sOutput, Len(sOutput) - 1)
'write to the file and reinitialize the variables'
Print #lFnum, sOutput
sOutput = ""
Next rRow
'Close the file'
Close lFnum
End Sub
Sub usedrows()
'To count used range of cells
MsgBox ActiveSheet.UsedRange.Rows.Count
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment