Showing posts with label VBA. Show all posts
Showing posts with label VBA. Show all posts

Sunday, February 10, 2013

Change the formatting of text to match the your style in Excel while doing copy-paste

 If you want to copy-paste text and want it should copy-paste automatically matching your style , then  copy the below code in "thisworkbook" of you excel


Private Sub WorkBook_Open()

  'MsgBox "this happens when workbook is opened"
    Application.OnKey "^v", "my_paste"
    '--------------call macro Paste option set to my style
End Sub
Option Explicit

Sub my_paste()
    MsgBox "you have pressed ctrl+v"
'Enter the password if your sheet is protected .
'worksheet.Protect Password:="urPassword", UserInterFaceOnly:=True, AllowFormattingColumns:=True, AllowFiltering:=True

'enter here your sheet style which is to be matched.
        Selection.PasteSpecial Format:="HTML", Link:=False, DisplayAsIcon:= _
        False, NoHTMLFormatting:=True, SkipBlanks:=False, Transpose:=False
   
End Sub

Coverts Display Hyperlink to Address of Link Hyperlink

If you want to convert  the hyeprlink display address to Actual address of Hyperlink  , We can do as below

Sub x()
    
     ' Select a range or column first! Enter the range you want to look in for
     Columns("A:A").Select

         Dim lngTemp As Long
    
    For lngTemp = 1 To Selection.Hyperlinks.Count
        
        Selection.Hyperlinks(lngTemp).TextToDisplay = Selection.Hyperlinks(lngTemp).Address
        
    Next
    
End Sub

Monday, January 21, 2013

Custom Message Box Buttons using Hooking in Excel VBA

This summary is not available. Please click here to view the post.

IE (Internet Explorer) Automation using Excel VBA

This summary is not available. Please click here to view the post.

Define a Position of MessageBox using VBA in Excel

  • You must create a CBT hook
  • Run a Message Box with CBT hook
  • Catch a HCBT_ACTIVATE message in the Hook procedure
  • Set new position using the SetWindowPos function
  • Release the CBT hook
Example: Hooking MessageBox using VBA in Excel:
Option Explicit
 
' Import
Private Declare Function UnhookWindowsHookEx Lib "user32" _
    (ByVal hHook As Long) As Long
 
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
 
Private Declare Function SetWindowsHookEx Lib "user32" _
    Alias "SetWindowsHookExA" _
    (ByVal idHook As Long, _
     ByVal lpfn As Long, _
     ByVal hmod As Long, _
     ByVal dwThreadId As Long) As Long
 
Private Declare Function SetWindowPos Lib "user32" _
    (ByVal hwnd As Long, _
     ByVal hWndInsertAfter As Long, _
     ByVal x As Long, _
     ByVal y As Long, _
     ByVal cx As Long, _
     ByVal cy As Long, _
     ByVal wFlags As Long) As Long
 
' Handle to the Hook procedure
Private hHook As Long
 
' Position
Private msgbox_x As Long
Private msgbox_y As Long
 
' Hook type
Private Const WH_CBT = 5
Private Const HCBT_ACTIVATE = 5
 
' SetWindowPos Flags
Private Const SWP_NOSIZE = &H1      ' Retains the current size
Private Const SWP_NOZORDER = &H4    ' Retains the current Z order

Sub TestMsgBox()
    MsgBoxPos "Set non-Center Position", _
              vbOKOnly, _
              "Message Box Hooking", _
              400, 300
End Sub
 
Public Sub MsgBoxPos(strPromt As String, _
              vbButtons As VbMsgBoxStyle, _
              strTitle As String, _
              xPos As Long, _
              yPos As Long)
 
    ' Store position
    msgbox_x = xPos
    msgbox_y = yPos
 
    ' Set Hook
    hHook = SetWindowsHookEx(WH_CBT, _
                              AddressOf MsgBoxHookProc, _
                              0, _
                              GetCurrentThreadId)
 
    ' Run MessageBox
    MsgBox strPromt, vbButtons, strTitle
End Sub
 
Private Function MsgBoxHookProc(ByVal lMsg As Long, _
                                ByVal wParam As Long, _
                                ByVal lParam As Long) As Long
    If lMsg = HCBT_ACTIVATE Then
        ' Change position
        SetWindowPos wParam, 0, msgbox_x, msgbox_y, _
                     0, 0, SWP_NOSIZE + SWP_NOZORDER
 
        ' Release the Hook
        UnhookWindowsHookEx hHook
    End If
 
    MsgBoxHookProc = False
End Function

Using InputBox Method in Excel VBA

This summary is not available. Please click here to view the post.

Disable Alert (Warning) Messages in Excel

This summary is not available. Please click here to view the post.

Saturday, January 19, 2013

Save Workbook as New File using VBA in Excel

Q. How Save Workbook as New File? A. Use the following VBA code:
Private Sub SaveWorkbookAsNewFile(NewFileName As String)
    Dim ActSheet As Worksheet
    Dim ActBook As Workbook
    Dim CurrentFile As String
    Dim NewFileType As String
    Dim NewFile As String
 
    Application.ScreenUpdating = False    ' Prevents screen refreshing.

    CurrentFile = ThisWorkbook.FullName
 
    NewFileType = "Excel Files 1997-2003 (*.xls), *.xls," & _
               "Excel Files 2007 (*.xlsx), *.xlsx," & _
               "All files (*.*), *.*"
 
    NewFile = Application.GetSaveAsFilename( _
        InitialFileName:=NewFileName, _
        fileFilter:=NewFileType)
 
    If NewFile <> "" And NewFile <> "False" Then
        ActiveWorkbook.SaveAs Filename:= NewFile, _
            FileFormat:=xlNormal, _
            Password:="", _
            WriteResPassword:="", _
            ReadOnlyRecommended:=False, _
            CreateBackup:=False
 
        Set ActBook = ActiveWorkbook
        Workbooks.Open CurrentFile
        ActBook.Close
    End If
 
    Application.ScreenUpdating = True
End Sub
 
How does it work? Let's look inside.
  • First, turn off screen updating:
    Application.ScreenUpdating = False
     
     
  • Store the opened file full path:
    CurrentFile = ThisWorkbook.FullName
    
    
  • Open window to choose new filename and folder:
  • NewFile = Application.GetSaveAsFilename( _
        InitialFileName:=NewFileName, _
        fileFilter:=NewFileType)
     
  • And now save file as new Workbook:
  • ActiveWorkbook.SaveAs Filename:= NewFile, _
        FileFormat:=xlNormal, _
        Password:="", _
        WriteResPassword:="", _
        ReadOnlyRecommended:=False, _
        CreateBackup:=False
     
  • We have to close new file and open the origin workbook:
  • Set ActBook = ActiveWorkbook
    Workbooks.Open CurrentFile
    ActBook.Close
     
  • and turn on screen updating:
    Application.ScreenUpdating = True