Posts

Parsing a text file and importing values to MS Excel

We can use Excel VBA script to analyze a text file for a specific purpose. In this example, I have a text file that contains a list of files and their details like name, date created, file size, etc. This is an extraction of DIR command in the dos prompt. I am trying to find a text string "CNT" in each line and if it is present then I will extract the name of the file and store that in the Excel Cell along with the date the is created.  I have hardcoded the column here, you can modify it however you want. The first column will keep the name of the file and the second column will keep the file creation date.  Sub import_text_file_to_excel() Dim fileHandle As Integer Dim fileLine As String Dim r As Integer Dim c As Integer r = 1 c = 1 Open "C:\\Users\\krsanjee\\files.txt" For Input As #1 While Not EOF(1) Line Input #1, myline On Error Resume Next line_first_part = Left(myline, 40) line_second_part = Right(myline, Len(myline) -...

You should know these MS Excel skills if you are using MS excel

Image
If you are using excel for whatever reason then knowing these skills will definitely increase your productivity. These are not advanced but moderate skills. You can manage with excel without knowing these skills but knowing these skills will improve the proficiency in Excel. 1. Defining named tables. You can define a range as a table and give a name to it. Later you can use this name in a formula. You can also use these named tables in references. Under formulas menu bar, select Define names. Then give a name to the tables, for example, Sales. Then select the range and select ok. After you create the named table, you can use the table name 'Sales' in the formula to calculate the sum. 2. Autofill  Using autofill you can complete the series based on the current values. For example, if you want to fill multiple of a number, you can enter the first 2 numbers of the series and click on the autofill icon and drag down the column. In the newer version of the o...

How to swap 2 excel ranges | swap with multiple cells selected

I had created a macro to swap 2 cells values in MS Excel. By just clicking a button a macro could interchange the content of 2 excel cells.  There was a limitation on that macro. It could only work only if the selection is just 2 cells. It could interchange the value of just 2 cells but not the ranges. I used to get a lot of request from my viewers to extend this macro for more than 1 cell. Due to time constraint, I could not work on it. Excel VBA macro to swap 2 values in cells Now, I have written another macro which will work on the Excel ranges. This macro is completely different. My original macro was based on area selection. But the new macro is based on the ranges. It is very small and easy to understand, unlike another macro, which is comparatively bigger. I will explain each of the lines in the macro. Check out my youtube channel for more amazing excel and office automation videos My youtube channel 1. Sub swap_2_ranges() 2. string_all_selected_...

Practical Text Manipulation using MID LEFT RIGHT FIND Functions in MS Excel

Image
Text manipulation is one of the great features of MS Excel. It has a lot of built-in functions to extract desired values from a text. By using different combinations of these functions we can create a complex formula. I have listed a few practical examples which are very useful. Extracting a Door number from an address text.  8314 W. Galvin Lane West Des Moines, IA 50265 68 4th Ave. Neptune, NJ 07753 8 Oklahoma St. Ladson, SC 29456 I will use the LEFT and FIND functions to extract the Door number of an address.  =LEFT(B4,FIND(" ",B4)) Here I am using the delimiter space (" "). The formula first finds a space character which appears after the door number. Then extracts the value whatever if at the left side of the first space.  Extracting a ZIP code from an address  Generally, the ZIP code will at the end of the address. Using the RIGHT function, we can extract the ZIP code  =RIGHT(B9,5) ...

Practical examples on MS Excel date functions

Image
MS Excel has an amazing set of Date and Time functions. By tweaking these functions, we can create a complex and useful formula. I am trying to list down a few practical examples particularly on the Date part, which will be very useful for a moderate to advanced Excel user.  How to find the end of the month for a given date Note that  EOMONTH  will return a serial number. You have to change the format to date format.  =EOMONTH(TODAY(),0) =EOMONTH(DATE(2018,10,10),0) How to find the number of days remaining in a month This is useful for interest calculation on first and last month on any type of loan repayment schedule.  Use DAYS , EOMONTH,  and DATE functions. =DAYS(EOMONTH(TODAY(),0),DATE(2018,10,10)) Number of days remaining in this month or current month =DAYS(EOMONTH(TODAY(),0),TODAY()) Find the number of months between two dates In other words, find the date difference in terms of months. DATEDIF is one of the very ...

VBA Code to find a record in the MS word mail merge

Image
MS Word mail merge has an option to navigate through the records. The below VBA code will help you to type and search the record within the MS word document area, you do not have to go to the menu bar. This code first resets the document to the first record. This is required as the mail merge navigation is only forward. That means the VBA code cannot traverse backward. Reads a text from TextBox which is placed within the document. If the keyword entered and the current record is matched, then the Active document is activated with the current record. With ActiveDocument.MailMerge  .DataSource.ActiveRecord = wdFirstRecord End With Dim numRecord As Integer Dim searchText As String searchText = TextBox1.Text Set dsMain = ActiveDocument.MailMerge.DataSource If dsMain.FindRecord(FindText:=searchText, Field:="Name") = True Then numRecord = dsMain.ActiveRecord End If End Sub Watch the complete step by step tutorial Download the Excel file and the word docum...

VBA Code to copy and paste the Content from excel to word

Image
There are situations where you may have to copy and paste some content from excel to word. And save the newly created document into disk. The below VBA code or a macro will help you to do that. It is a very simple code, it is being executed from MS Excel. The MS Word object Library should included. This library has to functionality to interact with MS word object.  To include MS word object library, go to Tools and then select References. Select the required object library as shown in the picture below.   To copy the VBA code follow the below steps Open excel and make your table ready. This steps is as per your requirement.  Press Ctrl + F11, this will open a Visual Basic editor.  Click on insert and then module.  Module1 will appear if do not have any  module. Paste the below code into this module  Go back to excel and your macro will appear under macros list. Select and click on Run to execute the macro.  Watch...