Showing posts with label Excel. Show all posts
Showing posts with label Excel. Show all posts

Friday, 27 April 2012

Excel VLookup


At times we may end in searching a value in column of a sheet with other column in the same sheet or a different sheet. Here VLookup is function that is so useful to identify the value.  The most common definition available in internet is “Searches for a value in the first column of a table array and returns a value in the same row from another column in the table array”. The meaning of the sentence is we need to identify a value of a cell in one column from the other column in the same row. VLookup represents vertical lookup.

Syntax
VLOOKUP(<lookup value>,<table array>,< Column index number>,<range lookup>)
  • Lookup value is the value to search in the first column of the table array. Lookup_value can be a value or a reference. If lookup_value is smaller than the smallest value in the first column of table_array, VLOOKUP returns the #N/A error value.
  • Table array may contain two or more columns of data. Use a reference to a range or a range name. The values in the first column of table array are the values searched by lookup value. These values can be text, numbers, or logical values. Uppercase and lowercase texts are equivalent.
  • Column index number    The column number in table_array from which the matching value must be returned. A Column index number of 1 returns the value in the first column in table_array; a Column index number of 2 returns the value in the second column in table array, and so on. If Column index number is:Less than 1, VLOOKUP returns the #VALUE! error value.Greater than the number of columns in table_array, VLOOKUP returns the #REF! error value.
  • Range lookup    A logical value that specifies whether you want VLOOKUP to find an exact match or an approximate match:If TRUE or omitted, an exact or approximate match is returned. If an exact match is not found, the next largest value that is less than lookup_value is returned.The values in the first column of table array must be placed in ascending sort order; otherwise, VLOOKUP may not give the correct value.If FALSE, VLOOKUP will only find an exact match. In this case, the values in the first column of table_array do not need to be sorted. If there are two or more values in the first column of table_array that match the lookup_value, the first value found is used. If an exact match is not found, the error value #N/A is returned.


Example:
Here in this example I will use a sample Order Table with the Product table. Our intention is used to find the Product name from the product id from the Order table for Order Detail table.

A
B
C
Order
Customer Name
Order ID
Product Code
Cameron White
1
AT123
James Peterson
2
BG456
Andrew Thomas
3
BT456
James Peterson
4
CY956
Jason Taylor
5
DR098
James Peterson
6
BT456
Michel Adams
7
CY956

A
B
Product
Product Code
Product Name
AT123
Amber Light
BG456
Big Plug
BT456
Big Tatoo
CY956
Crimson yarn
DR098
Digital Diary
BG456
Big Plug
CY956
Crimson yarn

From the both these table we need to achieve the below result
A
B
Order Detail
Customer Name
Product Code
Cameron White
Amber Light
James Peterson
Big Plug
Andrew Thomas
Big Tatoo
James Peterson
Crimson yarn
Jason Taylor
Digital Diary
James Peterson
Big Tatoo
Michel Adams
Crimson yarn

Type the formula “=VLOOKUP (C4, A14:B21, 2, FALSE)” this Formula (fx) textbox in the excel document
C4 – Lookup value
A14:B21 – It is the table array in the order detail table
2 – Column Index
False – It is the exact match of the lookup value.
Then drag the cell till the end of the row and see the result.
Please your queries on this post and I will get to this very soon.

Thursday, 26 April 2012

Excel Short cut to generate MS SQL query from excel data

Just few days back I use to receive lot of raw data in excel to migrate into MS SQL server. I used some features in MS SQL management studio to upload the data from excel to target tables. It was so problematic that I could not able to migrate all the data from excel to MS SQL. Because some data got truncated, data type excel found to be mismatching, transferred data are not placed in correct target column in the target table, etc.   


To overcome this problem, I formatted the input Excel document as below,
  1. Arrange column in the excel document as that of the columns in the SQL table.
  2. Modify the data types of the columns in the excel document as that of columns in SQL table using some of the predefined functions available in excel. Some time it is not necessary to use the predefined function to format columns.
  3. Place the insert query [insert into <table_name> values (”] in one of the column in excel and drag it till the end where there is no record for that row.
  4. Similarly place the ['','] & ['')] in two separate columns and you should use two single codes in the beginning then only excel will recognize as a single quotes.
  5. Finally frame the formula in the order of columns to form a sql query.
  6. Go to the Function (fx) and type “=”
  7. Hold the control key (block plus symbol appears) and press on the cell and type “&”. Report the process until you form the SQL query.
  8. The formula appears as this “=A1&B1&C1&D1&F1&G1” Or else you predefined concatenate function as this “=CONCATENATE(A1,B1,C1,D1,F1,G1)”
  9. Drag the formula column till the end where there is no record for that row. Copy all formula cells and paste in sql management studio and execute it.
This is found be more helpful and less time consuming for preparing the query.

Please post me your queries if you have doubt in this post.

Wednesday, 25 April 2012

Excel Simple Short Cut to generate Macro code without writing


During many occasions I use to write macro code in excel and it will be so time consuming process or at some time it was so problematic to remember all the syntax and keywords used while writing the code.

Very often I use to notice an option in Excel called “Record Macros” But I don’t find any time to dig into this menu choice. Later I find out that there is an existing feature in excel (in all the versions 2003/2007/2010) underneath “Record Macros” to generate the macro code for whatever operation we process in excel document. Here I will show the sample which is developed in Excel 2007 version.

For all clarification or doubt please post your comment s and I will get back to you very soon.

Create an Excel Macro document <filename>.xlsm in the local path.

I will show a small sample dummy sales data as an example for populating macro
1. Please refer the data as in the below screen shot


2. Click on the developer tab -> Records Macros, it will pop up a small window as in the below screen shot and Click OK

3. Then I will select data and create a line chart as below.

4. Click on the developer tab -> Stop Record, it will pop up a small window as in the below screen shot and Click OK

5. In developer tab Click on the Visual Basic Editor icon and click on the Module item under tree menu in the visual basic editor window to see the generate code.


Monday, 23 April 2012

Excel Multiple Sheet creation based on data in an input work sheet


Multiple Sheet creation based on data in an input work sheet.
1.       Consider this below table in a excel sheet. You may required to separate the data based on the item column and put it in separate sheet.


Item
Count
AC
1
VN
2
AW
3
XT
4
TU
5
AW
6
AC
7
XT
8
AC
9
VN
10
AW
11
AC
12
VN
13
TU
14
AW
15


2.       Create a excel document 2007 save as xyz.xlsm (Macro enabled excel document)
3.       See sample data as below

        4.       Click on the developer tab that appears on the Top Menu

        5.       Click on Insert Control and Click the button icon as in the below screens shot

        6.       Right click on the button control to see the Assign Macro as in the below screen shot

  7.      Click on the Assign Macro as in the below screen shot and it will popup the popup window as in the below screen shot

  8.      Then Click the New button from the above popup and it will open a macro editor as in the below screen shot. 

   9.    Paste the code in the editor.
  10.   Finally you can see the result as in the below screen shot.
Code:
Sub Button1_Click()
    If SheetExists("Sheet2") Then
     Exit Sub
     End If
    Sheets("Sheet1").Copy After:=Sheets("Sheet1")
    Sheets(2).Name = "Sheet2"
    Sheets("Sheet2").Select
    finalRow = ActiveSheet.UsedRange.Rows.Count
   
    'Sort Sheet
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("A1:A" & finalRow) _
        , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet2").Sort
        .SetRange Range("A1:B" & finalRow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
   
    'Delete dummy button
    ActiveSheet.Shapes("Button 1").Delete
    Dim state As String
    For x = 1 To finalRow
        LastSheet = Sheets.Count
        Sheets("Sheet2").Select
        ThisTerr = Range("A" & x).Value
        state = Range("A" & x).Value
        If Not SheetExists(state) Then
            Sheets("Sheet2").Copy After:=Sheets(LastSheet)
            Sheets(LastSheet + 1).Name = state
            Sheets(state).Select
            Sheets(state).Activate
            'delete unwanted rows
            For y = finalRow To 1 Step -1
               If (Range("A" & y).Value <> state) Then
                    Range("A" & y).EntireRow.Delete
               End If
            Next y
        End If
    Next x
    Application.DisplayAlerts = False
    ActiveWorkbook.Worksheets("Sheet2").Delete
    Application.DisplayAlerts = True
End Sub
Function SheetExists(SheetName As String) As Boolean
' returns TRUE if the sheet exists in the active workbook
    SheetExists = False
    On Error GoTo NoSuchSheet
    If Len(Sheets(SheetName).Name) > 0 Then
        SheetExists = True
        Exit Function
    End If
NoSuchSheet:
End Function