Vba Basic Flashcards
insert row
Rows(i & “:” & i ).Insert Shift:=xlDown
delete row
Rows(i & “:” & i).Delete shift:=xlUp
export table DB
Call JDdumpvariable2d(output,false)
open XML:
Workbooks.OpenXML “C:\TEMP\AMF\feedback_20150116.xml”
convert column number to letter
ColumnLetter = Split(Cells(1, ColumnNumber).Address, “$”)(1)
Loop function:
in RealTimeFunction
Application.OnTime Now + TimeValue(“00:00:10”), “RealTimeFunction”
save PDF:
ActiveSheet.Range(“B1:H60”).ExportAsFixedFormat Type:=xlTypePDF, fileName:=myFileName
insert image from net:
Sheets(1).Shapes.AddPicture “http://www.testsite.com/pix/temp1.png” , msoFalse, msoTrue, 100, 100, 500, 600
send email:
Dim olMail As Object Dim OlApp As Object Set OlApp = CreateObject("Outlook.Application") Set OlMail = OlApp.CreateItem(0) With OlMail .To = myTo .Subject = mySubject '.Body = myBody .htmlBody = myHTMLbody .Attachments.add myfullPath .Display End With
is file there:
isFileThere = (Dir(fileName) <> “”)
is Folder Exist
isFileThere = (Dir(myPath, vbDirectory) <> “”)
find all Files in folder:
fileName = Dir(myPath & "*.*") Do While fileName <> "" 'code fileName = Dir() Loop
try to create folder
On Error Resume Next
‘create folder
mkdir Location
On Error Goto 0
checkOpenFileName
Dim Wb As Workbook
For Each Wb In Workbooks
If InStr(Wb.Name, myFileName) <> 0 Then ‘found it
Next Wb
change in Worksheet event:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
mycolumn = Target.Cells.Column
myline = Target.Cells.Row
change in selection event
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Color a cell in red than back to blank
Cells(1, 1).Interior.Color = 255
Cells(1, 1).Interior.Color = xlNone
Open Workbook
Workbooks.Open myPathFileName
init Userform
Sub UserForm_Initialize()
Copy file from one location to another and rename it
FileCopy oldDirectory & OldFileName, newDirectory & NewFileName
get a random number 0-100
Randomize
mynum = Application.RoundUp(Rnd() * 100, 0)
Read a text file line by line
Dim fs, f As Variant Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.OpenTextFile(pathFileName, 1, , 0) While f.atendofline = False sloc = f.readline 'do things Wend f.Close
Open text file to append it
Sub OpenTextFileTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fs, f
Set fs = CreateObject(“Scripting.FileSystemObject”)
Set f = fs.OpenTextFile(“c:\testfile.txt”, ForAppending, TristateFalse)
f.Write “Hello world!”
f.Close
End Sub
Connect to DB
Dim conn As ADODB.Connection Dim rs As ADODB.Recordset Dim sConnString As String Dim oCmd As ADODB.Command sConnString = "..." Set conn = New ADODB.Connection conn.CommandTimeout = 60 Set rs = New ADODB.Recordset
If left(UCase(mysql), 6) = "SELECT" Then Set rs = conn.Execute(mysql) If Not rs.EOF Then Call JDrstomat(rs, result) Else Set oCmd = New ADODB.Command With oCmd .ActiveConnection = conn .CommandType = adCmdText .CommandText = mysql .Execute , , adExecuteNoRecords End With
End If
Format the column M with number comma separation
Columns(“M:M”).NumberFormat=“#,##0”
Risize columns
Columns(A:B).EntireColumn.Autofit
Remove autocorrect
Application.autocorrect.replacetext=false
Email on behalf of someone
Set OutMail = OutApp.CreateItem(0)
Set OutMail.SendUsingAccount = OutApp.Session.Accounts.Item(“xxx@xxx.com”)