Merge rows with same Column A - vba

I am new to VBA and from what I have seen it will be the best way to go about doing what I want to do. I have an excel spreadsheet with Column A being the name of a company and Column B being the state which it is in. Since companies are in multiple states there are repeated company names, I would like to make it so that Column B has a list of the states so that Column A does not repeat. I know that similar questions have been asked but like I said I am new to this and have never used VBA before so some of the code without an explanation is not all that helpful.
For example I would like to go from:
Company 1 | MI
Company 1 | IA
Company 1 | MD
Company 2 | MI
Company 2 | OH
To this:
Company 1 | MI, IA, MD
Company 2 | MI, OH
Any help would be greatly appreciated or suggestions of ways other than VBA would be appreciated.

I suggest you put both columns in the ROWS area of a PivotTable and ensure that has no subtotals and is in tabular format.

You may not need vba at all. One way to get the results you want is to put the following equation in Column C (assuming Company is in A and States are in B):
"=IF(A2<>A1,A2,"")"
Without the outside quotes. Then just drag the formula to fill to the bottom of the data. Also, if you use this formula, start the data on A2, as the formula will look at the cell above.
The D column is simply =B2.
The VBA code will follow this logic as well, if you still want to use VBA.

Related

SQL different null values in different rows

I have a quick question regarding writing a SQL query to obtain a complete entry from two or more entries where the data is missing in different columns.
This is the example, suppose I have this table:
Client Id | Name | Email
1234 | John | (null)
1244 | (null) | john#example.com
Would it be possible to write a query that would return the following?
Client Id | Name | Email
1234 | John | john#example.com
I am finding this particularly hard because these are 2 entires in the same table.
I apologize if this is trivial, I am still studying SQL and learning, but I wasn't able to come up with a solution for this and I although I've tried looking online I couldn't phrase the question in the proper way, I suppose and I couldn't really find the answer I was after.
Many thanks in advance for the help!
Yes, but actually no.
It is possible to write a query that works with your example data.
But just under the assumption that the first part of the mail is always equal to the name.
SELECT clients.id,clients.name,bclients.email FROM clients
JOIN clients bclients ON upper(clients.name) = upper(substring(bclients.email from 0 for position('#' in bclients.email)));
db<>fiddle
Explanation:
We join the table onto itself, to get the information into one row.
For this we first search for the position of the '#' in the email, get the substring from the start (0) of the string for the amount of characters until we hit the # (result of positon).
To avoid case-problems the name and substring are cast to uppercase for comparsion.
(lowercase would work the same)
The design is flawed
How can a client have multiple ids and different kind of information about the same user at the same time?
I think you want to split the table between clients and users, so that a user can have multiple clients.
I recommend that you read information about database normalization as this provides you with necessary knowledge for successfull database design.

Create a list from Table

I managed to enter data to a database via a form;
actually works like a charm.
Now, what I need, is a lookup function (preferably not a form), with which I can search a table on another worksheet.
Let's say, I have an edit field or a cell, in which I enter a term which shall be looked for in a certain column on the table in another worksheet.
I would like to get a list of all entries which contain the word and the value from another cell (an ID).
Example:
Search term: Tom
Table:
Tim | 2
Tom | 3
Tommy | 5
The List should Show Tom and Tommy and their respective IDs,
but everything I tried didn't turn out as intended (mostly didn't work at all)...

SSRS Lookup on single dataset

Hopefully you can help!
I have a single data source in my SSRS report. With this data source, I have populated a tablix. The tablix looks something like this:
SalesPerson ID Group Sales
Sarah 1 1 1234
Ross 2 1 555
Gemma 3 2 678
Jill 4 2 345
Jack 5 3 987
Peter 6 2 432
Henry 7 2 356
The report is set up to create a different page for each of the sales people. for example, on the first page of the report, only first record would be shown (the record that holds Sarah's information, the second page would show the record for Ross' information ,etc..)
The issues I face is this:
At the bottom of the report, I need to include a textbox that displays the group number that the specific employee belongs to (the employee who is currently being displayed on the page).
I think that I need to do some sort of lookup on the IDReportItem to return the group ID in order to do this, but have had no luck in my attempts.
I understand that this is a horrible way of doing things, but I am limited to using this single dataset for performing this task.
Any help you can provide will be greatly appreciated,
Thanks you!
Unfortunately there doesn't seem to be a way to do this in a single textbox, but you can do it with a second tablix that uses the same dataset.
Create your second tablix and position it at the bottom of the page, then set your grouping to be the same on both tablixes and use the second tablix to only display the group ID, plus whatever label you want.
Create a new row group for each tablix (grouping on group ID), then right click the group and browse to Group Properties -> Page Breaks and check the box that says "Between each instance of a group". Do this for both tablixes.
This is what grouping is designed for. Build your table, and set the page break attribute to true.
You can have multiple rows under your group. Since your group is a field, simply add it to the detail row.
Your grouping is obviously set up right to get the report paging correctly.
You could add a List to the report, set the grouping on that (with page break between groups)
Inside the list - Add a RECTANGLE. (this be important!)
Once you've added the rectangle, you can add another as many objects as you like. In your case I think that may be a matrix and a text box
eg
Then it just becomes as spacing issue (to get the page looking right)

Spitting long column values to managable size for presenting data neatly

Hi I was wondering if there is a way to split long column values in this case I am using SSRS to get the distinct values with the number of product ID against a category into a matrix/pivot table in SSRS. The problem lies with the amount of distinct category makes it a nightmare to make the report look pretty shall we say. Is there a dynamic way to split the columns in say groups of 10 to make the table look nicer and easy to read. I was thinking of using in operator then the list of values but that means managing the data every time a new category gets added. Is there a dynamic way to present the data in the best way possible? There are 135 distinct category values
Also I am open to suggestions to make the report to nicer if anyone has any thoughts. I am new to SSRS and trying to get to grips with its.
Here is an example of my problem
enter image description here
Are your column names coming back from the database under the SubCat field you note in the comments above? If so I imagine your dataset looks something like this
Subcat | Logno
---------+---------------
SubCatA | 34
SubCatB | 65
SubCatC | 120
SubCatD | 8
SubCatE | 19
You can edit this so that there is an index of each individual category being returned also, using the Row_Number() function. Add the field
ROW_NUMBER() OVER (ORDER BY SubCat ASC) AS ColID
To your query. This will result in the following.
Subcat | LogNo | ColID
-----------+--------------+----------
SubCatA | 34 | 1
SubCatB | 65 | 2
SubCatC | 120 | 3
SubCatD | 8 | 4
SubCatE | 19 | 5
Now there is a numeric identifier for each column you can perform some logic on it to arrange itself nicely on the page.
This solution involves a Tablix, nested inside a Matrix nested inside a Matrix as follows
First create a Matrix (Matrix1), and set it’s datasource to your dataset. Set the Row Group Properties to group on the following expression where ‘4’ is the number of columns you wish to display horizontally.
=CInt(Floor((Fields!ColID.Value - 1) / 4))
Then in the data section of the Matrix (bottom right corner) insert a rectangle and on this insert a new Matrix (Matrix 2). Remove the leftmost row. Set the column header to be the Column Name SubCat. This will automatically set the column grouping to be SubCat.
Finally, in the Data Section of Matrix 2 add a new Rectangle and Add a Tablix on it. Remove the Header Row, and set it to be one column wide only. Set the Data to be the information you wish to display, i.e. LogNo.
Finally, delete the Leftmost and Topmost rows/columns from Matrix 1 to make it look tidier (Note Delete Column Row only! Not associated groups!)
Then when the report is run it should look similar to the following. Note in my example SubCat = ColName, and LogNo = NumItems, and I have multiple values per SubCat.
Hopefully you find this helpful. If not, please ask for clarification.
Can you do something like this:
The following gives the steps (in two columns, down then across)

Excel 2007 Pivot Table Group Multiple Sets by One Other Set

This seems like it should be easy. I have a data set consisting of binary survey answers, "yes" and "no". I also demographic data such as gender, "male" and "female". I am trying to generate a single pivot table in Excel 2007 that shows the answer counts of multiple questions by a single demographic field. I can do one at a time but when I try to do more, they seem to get nested under each other. I have tried putting gender in the rows and the questions in the labels and vice versa to no avail. The closet I have gotten is by putting gender in the rows and a question under that but that's it. For the values I am simply using the count of responses.
Here is an example of what my data....
Gender Q1 Q2 Q3
Female yes yes yes
Male yes no yes
Male no no yes
Female no yes no
Here is what I want to get....
Gender Q1 Q2 Q3
yes no yes no yes no
Female 1 1 2 0 1 1
Male 1 1 0 2 2 0
Any tips would be great. Thanks in advance.
The structure of your data is not what the PivotTable needs.
You'd need to re-order the data in the following way, so that you have one column for each field you want to use in the results: Gender, Quartal, Answer (see screenshot).
Then your PivotTable will work perfectly.
For re-arranging the data you could use a VBA macro which splits the contents of one line in your original table in 3 lines.
Another way would be to use MS Access to re-arrange the data - you'll find a solution here: http://www.meadinkent.co.uk/accqueryjournal.htm . This might be also possible with MS Query in MS Excel(I have not tried it with Excel yet, but the access solution works fine).
Guess it can't be done. I have tried several different approaches and could get none of them to work. I ended up breaking up the data manually using the old fashioned way, cell equations. Too bad, I thought this would be a pretty basic thing to accomplish.
If you place gender for the first row label, use the filter tool to show what you want. i.e. filter one gender out or the other.