Pages

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

Wednesday, February 12, 2025

Convert SOQL result Date/Time field to Excel

Sample: SELECT Id, CreatedDate FROM Account

Result of CreatedDate: 2024-08-14T14:48:05.000+0000

To Convert into Excel as Date/Time format:

  1. Formula =VALUE(SUBSTITUTE(LEFT(A2, 19), "T", " "))
  2. Format Cells... (or Ctrl+1) for the above formula cell, select Custom, and Type: yyyy-mm-dd hh:mm:ss
  3. Done 




Tuesday, February 21, 2017

How to run Regex in Microsoft Excel?

1. Show Developer tab in Excel ribbon
If you do not see the tab, follow this URL to enable it.
https://msdn.microsoft.com/en-us/library/bb608625.aspx


2. VBA
To run Regex in Excel, you need to use VBA (Visual Basic for Applications).
From Developer tab, click Visual Basic icon.



3. Enable Regex for VBA for the workbook
In "Microsoft Visual Basic for Applications" window select "Tools" from the top menu.
Select "References".
Check the box next to "Microsoft VBScript Regular Expressions 5.5" to include in your workbook.
Click "OK".




Use Case:
We would like to parse specific patterns from range of cells in Excel, each cell could contain 0, 1 or more matches.
Here sample of data that user enter manually over times, to a text field without a good standard:
- Monitor 2X225,32C235, 21D2251
- 21A225; or 2C235; 21Z0251 keyboard

We need to capture and parse those ID, this looks like manual job, but with regex, we can parse this automatically. Regex should work with any new modern programming language, such as: Java, .Net and etc., but since Excel support VBA, we can make use of it.

If you notice above IDs, here is the possibility of patterns:
- 9X999
- 99X999
- 99X9999

With some testing with www.regex101.com, here is the RegEx to parse it:
[0-9]{2}[A-Z][0-9]{4}|[0-9]{2}[A-Z][0-9]{3}|[0-9][A-Z][0-9]{3}

From VBA window, copy and paste following script:
 Private Sub splitUpRegexPattern()  
   Dim regEx As New RegExp  
   Dim strPattern As String  
   Dim MyRange As Range  
   Dim i As Integer  
   Dim strMatch As String  
   'Source Data  
   Set MyRange = ActiveSheet.Range("A2:A15")  
   For Each C In MyRange  
     ' Regex pattern  
     strPattern = "[0-9]{2}[A-Z][0-9]{4}|[0-9]{2}[A-Z][0-9]{3}|[0-9][A-Z][0-9]{3}"  
     If strPattern <> "" Then  
       With regEx  
         .Global = True  
         .IgnoreCase = True  
         .Pattern = strPattern  
       End With  
       Set Matches = regEx.Execute(C.Value)  
       ' Reset the variables  
       strMatch = ""  
       i = 1  
       ' Iterate through the Matches collection  
       For Each Match In Matches  
         strMatch = "'" & Match.Value  
         ' Display the matches at right columns  
         C.Offset(0, i) = strMatch  
         i = i + 1  
       Next  
     End If  
   Next  
 End Sub  

Result:


The step and script is tested with Microsoft Excel 2013 and 2016.



Friday, January 27, 2017

Reverse VLOOKUP with INDEX + MATCH

VLOOKUP() in Excel is one of the most powerful and famous formula. This function is very easy to use to lookup a value from a table or range. You just need to refer a key value to the column located at the most left column of the range to get result value from another column in the same row. I'll not discuss more on VLOOKUP function in this blog, you can easily Google it or watch from Youtube.

One of the requirement to use VLOOKUP, which is also limitation of VLOOKUP, is the key value must be located at the most left column in the range, but that is not always how is out data structured. What happen when the key value located at the right of the result value?

Options:
1. Move the Result or Key value column 
Move the Result value column to the right of Key value, or move Key value column to the left of Result value, then use VLOOKUP.
This may work easily, but sometimes when you work with many columns and many user, move column is not really a desired option.


2. Copy the Key or Result value
Copy Key or Result value to have Key value located before of Result value, then use VLOOKUP. Same with option 1, sometimes option to move column in big worksheet and work many user is not really a good option.


3. INDEX + MATCH function
The INDEX function returns a value in a table based on the intersection of a row and column position within that table. The first row in the table is row 1 and the first column in the table is column 1.
The MATCH function searches for a specified item in a range of cells, and then returns the relative position of that item in the range.

With combination of INDEX + MATCH functions, we can get something similar with VLOOKUP, but the Key not must be located before Result column, see this sample:


What is the formula in I2? =INDEX(A:A,MATCH(H2,B:B,0),1)


A:A = result / target value
B:B = key
H2 = data for row 2
I2 = target result



Sunday, January 15, 2017

Excel Filter: Tips and Shortcut

As Salesforce admin, sometimes we need to prepare data before load into Salesforce correctly. Recently, I need to clean and prepare some pretty big amount of raw data. Microsoft Excel apparently is one the easiest and best available tool to clean and prepare the data before loading into Salesforce.

In this blog, I would like to share some tips learned from the exercise.

TIPS
- Ctrl+Shift+L – toogle enable and disable filter, make sure cursor in the range of value
Alt+A+C – clear ALL filters
- Place cursor at header, Alt+Down arrow to show drop down menu
- Place cursor at body, Alt+Down arrow to show drop down of available values (except number)
* the last one will work even no filter added

In drop down Filter menu (Alt+Down arrow)
- E key – type search
- C key – clear filter in current cursor column
- F+E key – select blank value
- F+N key – select non-blank value
- Up and Down arrow – move cursor, Space bar key to select, and Enter to perform action
- When cursor in range of values, Home – move to top value
- When cursor in range of values, End – move to bottom value
- Alt+Down Arrow+S – sort A to Z
- Alt+Down Arrow+O – sort Z to A
- Alt+Down Arrow+T – sort by Color sub menu
- Alt+Down Arrow+I – filter by Color sub menu
- Alt+Down Arrow+F – text or Date Filter sub menu



DO
1. Double click at bottom right of a cell will copy value to visible rows only


Double click bottom right corner to auto-fill value of C2 to visible rows below it (C4 and C6). When we clear the filter, only C4 and C6 is filled, while C3 and C5 is skipped. Value of C2 can be static or formula.


You can apply this to multiple columns too.


2. Similar to point 1, copy paste will to copy value to visible rows only


- Select range C2-D2 -- copy
- Select range C4-D6 -- paste
- Result: only value in C4, C6, D4, D6 will be copied, while row 3 & 5 skipped

** the same result if you select range C2-D6 for fill with a color, row 3 & 5 will not be colored


3. Copy paste will copy only from visible rows value 
- This is not applicable for manually hidden rows
- Select cells / range to copy, example: copy as below screenshot


Paste to new area, I put my cursor to cell A8 -- only visible cells are copied.



4. Deal with blank row
If you need to deal with blank row in filter, make sure to highlight/select the area (in sample below, select area A1-D10 or the whole A-D column), before hit Ctrl+Shift+L, otherwise filter will not include area below empty row (row 7 and below).



DON'T
1. Copy more than 1 row from source into target with filtered rows

Don't copy more than 1 row from source into target with filtered rows, this cause value in target hidden filtered rows will be overwritten, sample: copy 3 rows of "B" from source (B11-B13)

Paste it to target which is filtered rows, for this example: paste into cell C2

Instead of copy value "B" into C2, C4, C6 -- "B" will be copied into C2, C3, C4.

Summary, copy from multiple rows will skipped filtered target.

This action will only work well, if there is no skipped rows in the applied filter, example: Jawa in sample above is at continuous rows e.g. 2,3,4.


Excel Table
By using the Table features, you can manage the data in the table rows and columns independently from the data in other rows and columns on the worksheet.
- Ctrl+L – create Excel Table
- To delete Excel Table table without losing the data:
   - Select Convert to Range from DESIGN tab menu, or
   - Right-click on the table and click Convert to Range under Table menu
- Filtering controls are added to the table headers automatically
- Place cursor anywhere in table, Alt+Shift+Down arrow – show the drop down menu
- You can have filter for more than one range of data on a sheet



Page-level ad