29 June 2019
Excel VBA : Search and Change Comments Box Using VBA
Jadi malam ni aku buat VBA untuk search and tukar comments box.
Enjoy
Zaiem
Sub changecommentbox2()
Dim rng As Range, cell As Range
Set rng = Range("D6:IW42")
For Each cell In rng
cell.Select
'Selection.ShapeRange.AutoShapeType = msoShapeRectangle
If Selection.Comment Is Nothing Then
'MsgBox "Has no comment"
Else
'MsgBox "There is comment in box"
'Selection.Comment.Text "Ladida"
Selection.Comment.Shape.Select True
Selection.ShapeRange.AutoShapeType = msoShapeRectangle
End If
Next cell
End Sub
26 November 2017
Excel VBA Switch Random File Name
Sub CopyAllJobs()
Dim myWindow As Window
For Each myWindow In Windows
If Left(myWindow.Caption, 8) = "All Jobs" Then myWindow.Activate '''': Exit Sub
Next myWindow
End Sub
22 November 2017
Excel VBA Only Allow Workbook To Run From Specific Location
Check current workbook link and compared to custom address
Sub Success()
'check current path
Path = ActiveWorkbook.Path
'Range("A1") = ActiveWorkbook.Path
'if current path not same, exit
If Path <> "C:\Users\othmmohd\Desktop" Then
MsgBox "Please use Jobs in Lab - Macro from shared drive."
ThisWorkbook.Close savechanges:=False
End If
End Sub
Excel VBA Check Current Workbook Path
Sub test1()
Path = ActiveWorkbook.Path
Range("B3") = ActiveWorkbook.Path
link = ActiveWorkbook.Path
Range("B4") = ActiveWorkbook.Path
End Sub
19 February 2016
Copy Cells From 2 Sheets and Paste to Outlook as Image
Hello,
Ini VBA yang aku edit dan tambah untuk snap cells untuk 2 sheets dan pasti ke dalam Outlook mail.
Disertakan juga sheet untuk To list dan Cc list.
==========================================================================
Sub OpenOutlook()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim strUser As String
Dim signature As String
Dim sTo As String
Dim sCC As String
Dim rng As Range
Set rng = Nothing
Set rng = ActiveSheet.UsedRange
'check outlook is open
Dim olApp As Object
CreateObject("shell.application").minimizeall
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
On Error GoTo 0
If Not olApp Is Nothing Then
'Outlook is open!
'MsgBox "Outlook is running"
Else
'Outlook not open
'MsgBox "Outlook is not running"
Shell ("OUTLOOK")
End If
'end check outlook is open
''''''
'For To field
Set emailRng = Worksheets("Email Contact").Range("A2:A30")
For Each cl In emailRng
sTo = sTo & ";" & cl.Value
Next
sTo = Mid(sTo, 2)
'For CC field
Set emailRngCC = Worksheets("Email Contact").Range("B2:B10")
For Each cl In emailRngCC
sCC = sCC & ";" & cl.Value
Next
sCC = Mid(sCC, 2)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
.Display
End With
signature = OutMail.HTMLBody
'strbody = "Good Morning;We have completed our main aliasing process for today. All assigned firms are complete. Please feel free to respond with any questions.Thank you."
strbody = "Hi all,Below are today's jobs in the lab."
'strbody = "This is line 1" & "
" & _
' "This is line 2" & "
" & _
' "This is line 3" & "
"
With OutMail
.SentOnBehalfOfName = ""
.To = sTo
.CC = sCC
.BCC = ""
.Subject = "Jobs in Lab on " & Format$(Date, "dd-mm-yyyy")
.HTMLBody = strbody & RangetoHTMLPassdown(rng) & RangetoHTMLReport(rng) & signature
.Display
'.Send
End With
'Sub GCloseJobsinLab()
'Close opened jobs in lab from iFAct.
Windows("Jobs in Lab").Close False
Windows("Jobs_in_lab_Macro.xlsm").Close False
End Sub
Function RangetoHTMLReport(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
' rng.Copy
Sheets("Report").Select
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTMLReport = ts.readall
ts.Close
RangetoHTMLReport = Replace(RangetoHTMLReport, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
Function RangetoHTMLPassdown(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2013
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
' rng.Copy
Sheets("Passdown Format").Select
'Range("A1").Select
'Range(Selection, Selection.End(xlToRight)).Select
'Range(Selection, Selection.End(xlDown)).Select
Range("A1:L27").Select
Selection.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTMLPassdown = ts.readall
ts.Close
RangetoHTMLPassdown = Replace(RangetoHTMLPassdown, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
========================================================================