Showing posts with label Records. Show all posts
Showing posts with label Records. Show all posts

November 24, 2010

Copy Paste Pl/Sql developer queried table data and Auto Format the content

Paste and Auto-format Table records in Excel

While taking backup of the tables, we are doing the following steps.
1.       Executing the query
2.       Copying the content from pl/sql developer
3.       Opening excel and paste the copying content
4.       If we want multiple tables to take backup, we need select next sheet and paste the content
5.       If we need more than 3 sheets we are adding new sheets.
6.       Just copying content from the pl/sql developer doesn’t finish our work, am I right?
7.       We need to format the data too.

So, I just tried to create one auto-formatter which will paste the data and auto-format the data.

Please download the attachment and unzip the file.

After opening the excel file, do the following.

a) Please change the macro settings to "Disable all macros with notification"
     Note: To see how you can change the macro settings, please see the following link


b) Please click on the "User Friendly Formatter" button and follow the procedure.
c) It will open a new file and asks you to save the file.
d) Copy the content from pl/sql developer, when it displayed the following pop-up window.


e) The data will be auto formatted and then asks user for continuation by displaying the following window.

f) If user wants to continue, user can click “Yes” button, and the next sheet will be auto-selected.
g) Goto step (d)
h) When your clicks on “No” button, the file will be autosaved.


Please see the macro code and change according to your requirements.

Source Code:



Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long

Sub CreateNewWorkbook()
    Dim oWorkbook As Workbook
    Dim wbName, fileSaveName As String
    Dim sCount, decision, copied, sIndex As Integer
    Dim oSheet As Worksheet
    
    'On Error GoTo errHandler
    
    Set oWorkbook = Workbooks.Add
    
    Save_ActiveWorkbook
    
    sCount = ActiveWorkbook.Sheets.Count
    'MsgBox (sCount)
    
    If MsgBox("Please copy the content and then click on OK", vbOKOnly, "Decision") = vbOK Then
        CopyAndFormatData
    End If
    
    sIndex = 1
    
askUser:
    decision = MsgBox("Do you want to continue with the next sheet?", _
    vbYesNo, "Decision")
  
'If user wants to continue
    If decision = vbYes Then
    
        'Asking user to copy the data first
        copied = MsgBox("Please copy the content and then click on OK", vbOKCancel, "Decision")
        
        'If user copied data
        If copied = vbOK Then
        
            'Selecting next sheet
            If sIndex < 3 Then
                Sheets(sIndex + 1).Select
                sIndex = sIndex + 1
            'ElseIf sIndex = 3 Then
            '    Sheets(sIndex).Select
            'Adding additional sheet from sheet4
            ElseIf sIndex >= 3 Then
                    Set oSheet = Worksheets.Add(After:=Worksheets(Worksheets.Count))
            End If
            
            'copying and formatting data
            CopyAndFormatData
            
        ElseIf copied = vbCancel Then
            'Confirm the user whether user want to quit and save file
            If MsgBox("Do you want to remain in the same sheet", vbOKOnly, "Save File") = vbOK Then
                Sheets(ActiveSheet.Index).Select
            End If
        End If
        GoTo askUser
    End If
    

savingFile:
    ActiveWorkbook.Save
    
End Sub

Sub CopyAndFormatData()

'If Range("A1").Font.Bold = True Then
    ActiveSheet.Range("A1").Select
    
    On Error Resume Next
    ActiveSheet.PasteSpecial Format:=Text, Link:=False, DisplayAsIcon:=False
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    If Range("A1").Value Is Null Then
        Range("A:A").Delete
    End If
    
    'select header row and make it as bold
    Cells(1, 1).EntireRow.Select
    Selection.Font.Bold = True
    
    'Autofit column width
    Range("A1").CurrentRegion.Select
    Selection.Columns.AutoFit
    
ClearClipboard

End Sub

Sub ClearClipboard()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
End Sub
Sub Save_ActiveWorkbook()
'Working in Excel 2000-2010
    Dim fname As Variant
    Dim NewWb As Workbook
    Dim FileFormatValue As Long

    'Check the Excel version
    If Val(Application.Version) < 9 Then Exit Sub
    If Val(Application.Version) < 12 Then

        'Only choice in the "Save as type" dropdown is Excel files(xls)
        'because the Excel version is 2000-2003
        fname = Application.GetSaveAsFilename(InitialFileName:="", _
        filefilter:="Excel Files (*.xls), *.xls", _
        Title:="Save As the Workbook as ")

        If fname <> False Then
            'Copy the ActiveSheet to new workbook
            ActiveSheet.Copy
            Set NewWb = ActiveWorkbook

            'We use the 2000-2003 format xlWorkbookNormal here to save as xls
            NewWb.SaveAs fname, FileFormat:=-4143, CreateBackup:=False
            NewWb.Close False
            Set NewWb = Nothing

        End If
    Else
        'Give the user the choice to save in 2000-2003 format or in one of the
        'new formats. Use the "Save as type" dropdown to make a choice,Default =
        'Excel Macro Enabled Workbook. You can add or remove formats to/from the list
        
        fname = Application.GetSaveAsFilename(InitialFileName:="", filefilter:= _
        " Excel Macro Free Workbook (*.xlsx), *.xlsx," & _
        " Excel Macro Enabled Workbook (*.xlsm), *.xlsm," & _
        " Excel 2000-2003 Workbook (*.xls), *.xls," & _
        " Excel Binary Workbook (*.xlsb), *.xlsb", _
        FilterIndex:=1, Title:="Save As the Workbook as ")

        'Find the correct FileFormat that match the choice in the "Save as type" list
        If fname <> False Then
            Select Case LCase(Right(fname, Len(fname) - InStrRev(fname, ".", , 1)))
            Case "xls": FileFormatValue = 56
            Case "xlsx": FileFormatValue = 51
            Case "xlsm": FileFormatValue = 52
            Case "xlsb": FileFormatValue = 50
            Case Else: FileFormatValue = 0
            End Select

            'Now we can create/Save the file with the xlFileFormat parameter
            'value that match the file extension
            If FileFormatValue = 0 Then
                MsgBox "Sorry, unknown file extension"
            Else
                'Copies the ActiveSheet to new workbook
                Set NewWb = ActiveWorkbook

                'Save the file in the format you choose in the "Save as type" dropdown
                NewWb.SaveAs fname, FileFormat:= _
                             FileFormatValue, CreateBackup:=False

            End If
        End If
    End If
End Sub


Note: Please send your feedback and comments

September 16, 2010

Finding missing records in worksheet w.r.t other worksheet (or) intersection of two worksheets

Description:

We have two worksheets with similar records with same structure)(not in number of records). We want to know missing records in one worksheet with reference to other worksheet (Or) we want to find the intersection of both sheets.

Solution

Workbooks used are Workbook1.xls, Workbook2.xls, Results.xls
Workbook1 is having Src1 worksheet
Workbook2 is having Src2 worksheet
Results.xls is having Dest1 worksheet which will have Src1-Src2 records

Results.xls is having Dest2 worksheet which will have Src2-Src1 records

    Dim Workbook1, Workbook2, Results As Workbook
    Dim Src1, Src2, Dest1, Dest2 As Sheet1
  
    Application.DisplayAlerts = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
  
    Windows("Workbook1.xls").Activate
    Set Workbook1= ActiveWorkbook
  
    Windows("Workbook2.xls").Activate
    Set Workbook2= ActiveWorkbook
  
    Windows("Results.xls").Activate
    Set Results = ActiveWorkbook

    
    'Renaming Results workbook sheet1 and sheet2 to Dest1 and Dest2
    Results.Sheets(1).Name = "Dest1"
    Results.Sheets(2).Name = "Dest2"

    Workbook1.Activate
    ActiveWorkbook.Sheets("Src1").Activate
    Set Src1= ActiveSheet
  
    Workbook2.Activate
    ActiveWorkbook.Sheets("Src2").Activate
    Set Src2= ActiveSheet
  
    Results.Activate
    ActiveWorkbook.Sheets("Dest1").Activate
    Set Dest1 = ActiveSheet
    ActiveWorkbook.Sheets("Dest2").Activate
    Set Dest2 = ActiveSheet
   
    Dim Src1rng As Range
    Dim Src2rng As Range
  
    'Select and assign range for Src1 sheet data
    Src1.Activate

    'Selecting range without headers by using offset(1,0)
    Set Src1rng= Range("A1").CurrentRegion.Offset(1, 0)

    'Select and assign range for Src2 sheet data
    Src2.Activate
    Set Src2rng= Range("A1").CurrentRegion.Offset(1, 0)
  
    Dim row_not_exist As Boolean
    'Select each row from the Src1rng

    Src1.Activate
    For Each Src1Row In Src1rng.Rows
        row_not_exist = True
        Src2.Activate
        For Each Src2Row In Src2rng.Rows

            'Comparing cell(s) in Src1rng with cell(s) with Src2rng
            If Src1Row.Cells(1).Value = Src2Row.Cells(1).Value Then
                If
Src1Row.Cells(2).Value = Src2Row.Cells(2).Value Then
                            'if record exists in both sheets, no check needed further
                            row_not_exist = False
                            Exit For 'Don't compare with remaining records
                        End If
            End If
        Next Src2Row  'Go to next record in Src2



        'If Src1rng record not exists in Src2rng then 
        'copy the Src1 record into Results workbook Dest1 sheet
        If row_not_exist = True Then
            Src1.Activate
            Src1Row.Cells.Select
            Selection.Copy
            Dest1.Activate
            ActiveCell.Offset(1, 0).Select
            Dest1.Paste
            Application.CutCopyMode = xlCopy
        End If
    Next Src1Row  'Go to next record in Src1
    

    'we can find the viceversa by just changing the names from Src1 to Src2
  
    Application.DisplayAlerts = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True

Sample Output:

Src1 Sheet dataSrc2 Sheet data

Names Required
ABC  DEF
XYZ  WUV
PSR  DOT
Names Required
ABC  DEF
GHI  LKV
PSR  DOT


Dest1 Sheet Dest2 Sheet

Names Required
XYZ  WUV
Names Required
GHI  LKV

Note:
Dest1 contains the missing(intersection) data, which available in Src1 but not in Src2
Dest2 contains the missing(intersection) data, which available in Src2 but not in Src1

Please let me know if any further queries, contact me psrdotcom@gmail.com 

Featured Post

Java Introdcution

Please send your review and feedback to psrdotcom@gmail.com