Saturday, March 31, 2007
Save RTF document as word
Sub Open_n_Save()
sFile = Dir$("C:\ReFormated\*.rtf", vbNormal)
Do Until Len(sFile) = 0
Documents.Open "C:\ReFormated\" & sFile
ActiveDocument.SaveAs "C:\ReFormated\" & sFile & ".doc", wdFormatDocument
ActiveDocument.Close False
sFile = Dir$
Loop
End Sub
See also :
Excel VBA - 1004 -- The file could not be accessed
Save and Reopen all Workbooks (Excel VBA)
Save copy of the workbook
SaveAs Dialog - Controlled Save
Using Excel Functions in VBA
Evaluate Method converts a Microsoft Excel name to an object or a value
For example , the following method adds the values of the cells A1 to A6.
Sub Evaluate_Usage()
Dim lSum As Long
lSum = Evaluate("=SUM(A1:A6)")
End Sub
USe of evaluate has reduced a roundabout way of looping thru the cells and summing it up.
What next.. use the sum if, count if functions and evaluate!!!
Display text in a multiple lines
Set MultiLine property to True either in the Properties window
alternatively this can be set in runtime like:
TextBox1.MultiLine = True
Display Time Dynamically
Add a Timer Control to your form and set the timer interval.
In the Timer event for that timer set (refresh) the time
Private Sub Timer1_Timer()
Label1.Caption = Date & " " & Time
End Sub
In the Form Load Event Intialize the interval
Private Sub Form_Load()
Timer1.Interval = 1000
End Sub
Execute Excel Macro in All Files
Sub Exec_Macro_For_All()
Dim sPath As String
Dim sFile As String
Dim sDir As String
Dim oWB As Workbook
Dim i1 As Long
Dim iMax As Long
On Error GoTo Err_Clk
sPath = "" ' Your Path
If Right$(sPath, 1) <> "\" Then sPath = sPath & "\"
sDir = Dir$(sPath & "*.xls", vbNormal)
Do Until LenB(sDir) = 0
Set oWB = Workbooks.Open(sPath & sDir)
Exec_MyMacro() ' Your MAcro here
oWB.Save
oWB.Close False
sDir = Dir$
Loop
Err_Clk:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub
Cheers
Shasur
Get User Name using Split Function
Sub Get_EMail_User_Name()
sEmail = "joe@gmail.com"
arTemp = Split(sEmail, "@")
sDomain = arTemp(LBound(arTemp))
End Sub
Cheers
Shasur
Get Domain Name from eMail (Split Function)
Sub Get_Domain()
sEmail = "joe@gmail.com"
arTemp = Split(sEmail, "@")
sDomain = arTemp(UBound(arTemp))
End Sub
Cheers
Shasur
Monday, March 26, 2007
Disabling Excel events
In some cases, we would have written some code in Worksheet_SelectionChange, Workbook_SheetActivate, etc and do not want them to be fired. This will be used if you have some event to be fired when the user enters a value, but do not want them if the value is from VBA code etc. In that case switch off the Application.EnableEvents and turnit on after the
process.
Sub Donot_Fire_Events()
Application.EnableEvents = False
' Coding to skip these events
Application.EnableEvents = True
End Sub
Excel Not Calculating Formula
Sub Switch_On_XL_Calculation()
Application.Calculation = xlCalculationAutomatic
End Sub
Friday, March 02, 2007
Transferring array to Excel range
Is there any method to transfer the contents of the array to an Excel Range.. Most often programmers used to loop thru the array and put it to the Excel (of course with another loop). Here is a simple code that will transfer the array contents to Excel. Many thanks to Sharmila Purushotaman for this thoughful article
Sub Sheet_Fill_Array()
Dim myarray As Variant
myarray = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Range("A1:A10").Select
Range("A1:A10").Value = Application.WorksheetFunction.Transpose(myarray)
End Sub
' Keywords : ARRAY to Excel, Transferring array to range
Character to ASCII
Sub Convert2_Asc_Values()
For i1 = 1 To 256
Cells(i1 + 1, 3).Value = Asc(Cells(i1 + 1, 2).Value)
Next i1
End Sub
AddMe - Search Engine Optimization
ASCII to Character
Sub Print_Asc_Values()
For i1 = 1 To 255
Cells(i1 + 1, 1) = i1 Cells(i1 + 1, 2) = Chr(i1) Next i1
End Sub
Cheers
Shasur