Reorganize data [UDF]
Sheet1A B C D
8 Country Europe
9 Lights 100
10 Type A 200
11
12 Country USA
13 Fuel 40
14 Diesel 200
15
16 Europe Lights Type A 100
17 USA Fuel Diesel 40Oscar,is there a way to organize this the information into a database format like row 16 onwards,
It picks up all non blanks between the countries putting each line into a separate column.
Answer:
I created a User Defined Function that rearranges non empty cells into rows, using a delimiting value. In the example below, "Country" is the delimiting value. The desired output is displayed in row 11 and 12 and the UDF is shown in row 15 and 16.
A User Defined Function is a custom function that anyone can use, simply copy the VBA code and paste to a code module in your workbook.
Array formula in cell A15:F17
How to enter array formula in cell range A15:F17
- Select cell range A15:F17.
- Type =OrganizeData("Country", A2:C8)
- Press and hold CTRL + SHIFT simultaneously.
- Press Enter once.
- Release all keys.
User defined Function Syntax
OrganizeData(srch, rng)
Arguments
srch | Required. A delimiting value. |
rng | Required. The range containing values you want to rearrange. |
VBA code
'Name User Defined Function Function OrganizeData(srch As String, rng As Variant) 'Declare variables and data types Dim cell As Range, temp() As Variant, ca As Single Dim iRows As Integer, i As Integer, c As Single, r As Single Dim chk As Boolean 'Make array temp as large as the cell range you entered the UDF in ReDim temp(Range(Application.Caller.Address).Columns.Count - 1, 0) 'Save False to variable chk chk = False 'Save values in cell range rng to array variable rng rng = rng.Value 'Iterate through rows in rng variable For r = LBound(rng, 1) To UBound(rng, 1) 'Iterate through columns in array variable For c = LBound(rng, 2) To UBound(rng, 2) 'If rng value is equal to delimiting value If rng(r, c) = srch Then 'If Chk variable is not equal to False If chk <> False Then 'Save blanks to temp variable based on value i For ca = i To UBound(temp, 1) temp(ca, UBound(temp, 2)) = "" Next ca 'Reset i to 0 (zero) i = 0 'Increase array variable temp by 1 ReDim Preserve temp(UBound(temp, 1), UBound(temp, 2) + 1) End If 'Save True to variable chk chk = True 'If rng variable is not equal to nothing and rng variable is not equal to delimiting value then ElseIf rng(r, c) <> "" And rng(r, c) <> srch Then 'Save value to array variable temp temp(i, UBound(temp, 2)) = rng(r, c) 'Increment i with 1 i = i + 1 End If Next c Next r 'Save blanks to remaining values in array variable temp For ca = i To UBound(temp, 1) temp(ca, UBound(temp, 2)) = "" Next ca 'Increase containers in arrat variable temp with 1 ReDim Preserve temp(UBound(temp, 1), UBound(temp, 2) + 1) 'Count the number of rows you have entered the UDF in iRows = Range(Application.Caller.Address).Rows.Count 'Save blanks to remaining cells For r = UBound(temp, 2) To iRows For c = LBound(temp, 1) To UBound(temp, 1) temp(c, r) = "" Next c ReDim Preserve temp(UBound(temp, 1), UBound(temp, 2) + 1) Next r 'Return values in temp to worksheet rearranged vertically OrganizeData = Application.Transpose(temp) End Function
Where to copy the code?
User defined function category
This article demonstrates how to convert a range of cells containing strings separated by a delimiter into a range of […]
This article demonstrates formulas that count values in cell based on a delimiting character. The image above shows a formula […]
This article demonstrates a User Defined Function (UDF) that counts unique distinct cell values based on a given cell color. […]
This article describes how to count unique distinct values in list. What is a unique distinct list? Merge all duplicates to one […]
Blake asks: I have a somewhat related question, if you don't mind: I have very large amount of text in […]
This article demonstrates two formulas that extract distinct values from a filtered Excel Table, one formula for Excel 365 subscribers […]
AJ Serrano asks: I have a column where each rows contains different values and I wanted to obtain the duplicate […]
This article demonstrates a user defined function that extracts duplicate values and also count duplicates. Example, the image below shows a list containing […]
This post describes a custom function (User defined Function) that extract values existing only in one out of two cell […]
The User Defined Function demonstrated above extracts unique distinct records also considering upper and lower case letters. For example, a record […]
The User Defined Function demonstrated in the above picture extracts unique distinct values also considering lower and upper case letters. […]
This blog post describes how to create a list of unique distinct words from a cell range. Unique distinct words […]
This blog post describes how to create a list of unique words from a cell range. Unique words are all […]
I tried the array formula in this post: Filter common values between two ranges using array formula in excel to […]
The image above demonstrates a User Defined Function that extracts all words containing a given string that you can specify. In […]
This article describes how to find a sum from a range of numbers using a user defined function. Let´s see […]
Question:I would like to reduce the number of open items by identifying positive and negative amounts that net to zero […]
In this post I will describe a basic user defined function with better search functionality than the array formula in […]
This user defined function creates a unique distinct list of words and how many times they occur in the selected […]
Macros and custom functions are great, they can automate many tedious tasks. To have them available whenever you need them, […]
In this vba tutorial I am going to show you how to return values from an udf, depending on where […]
This article demonstrates a user defined function that lists files in a ggiven folder and subfolders. A user defined function is […]
This blog post describes how to create permutations, repetition is NOT allowed. Permutations are items arranged in a given order meaning […]
This blog post demonstrates a custom function (UDF) that creates permutations. Repetition is allowed. The custom function lets you specify the […]
This article explains how to perform multiple lookups based on values in one cell with a delimiting character using a […]
The image above demonstrates a user-defined function in cell range B6:D7 that allows you to search a folder and subfolders […]
This article demonstrates formulas and a UDF that searches for values in a table based on concatenated values and returns […]
Question: How do I divide values equally into groups (3 lists or less)? This post shows you two different approaches, […]
The SUBSTITUTE and REPLACE functions can only handle one string, the following User-Defined Function (UDF) allows you to substitute multiple […]
This post describes a User Defined Function that searches multiple ranges and adds corresponding values across worksheets in a workbook. A […]
This article describes a User Defined Function that lookups values in multiple cross reference tables based on two conditions. A […]
Functions in this article
More than 1300 Excel formulas
Excel categories
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.