07-15-2019 08:32 AM
I need to combine cells in column B if the value in the cell to the left is blank while also deleting the blank cell to the left to close any gaps between cells in Column A. The best way for me to explain this is by showing an example. I need this:
Column A Column B
Test This
That
Other
Test2 Also
This
Test3 Please
Help
Me
To look like this
Column A Column B
Test This, That, Other
Test2 Also, This
Test3 Please, Help, Me
I am not quite sure how to word my question to search so I apologize if this has been asked many times! :)
07-15-2019 08:52 AM
If you want a formula to return your concatenated text and have Excel 2016 on Office 365 subscription (or Excel 2019), then you may copy down a TEXTJOIN formula:
=TEXTJOIN(",",TRUE,INDEX(B:B,MATCH(D2,A:A,0)):INDEX(B:B,IFERROR(MATCH(D3,A:A,0)-1,MAX(MATCH({"zzzzz",1E+307},B:B,1)))))
07-15-2019 08:52 AM
It could be done with Power Query - query the date, fill down first column, group by it, add column with initial second column as list and extract it with separator
07-15-2019 08:58 AM
Thanks @Brad_Yundt for your quick response here. I am trying this formula now. I am a novice when it comes to excel so I have a few questions.
I tried running that formula is column C and the return is #N/A. What do I need to change here?
Is column C the correct column?
Thanks in advance.
07-15-2019 09:01 AM
Thanks for responding so quickly @Sergei Baklan. I am pretty green when it comes to excel but have been working with it extensively in my new position. If you have the time, could you explain Power Query a bit more? I would like to know more about it from someone who has obviously used it.
07-15-2019 09:13 AM
Hi Jay,
there are a solution with help of the follwing macro:
Sub GroupData()
Dim lngRow As Long
Dim lngRowMax As Long
Dim lngz As Long
Dim lngzMax As Long
With Sheet1
.Range("G:H").ClearContents
.Range("G1:H1").Value = .Range("A1:B1").Value
lngz = 2
lngRowMax = .Range("B" & .Rows.Count).End(xlUp).Row
For lngRow = 2 To lngRowMax
If .Range("A" & lngRow).Value <> "" Then
.Range("G" & lngz).Value = .Range("A" & lngRow).Value
.Range("H" & lngz).Value = .Range("B" & lngRow).Value
lngz = lngz + 1
Else
lngzMax = .Range("H" & .Rows.Count).End(xlUp).Row
.Range("H" & lngzMax).Value = .Range("H" & lngzMax).Value & "," & .Range("B" & lngRow).Value
End If
Next lngRow
End With
End Sub
Best regards
Bernd
https://vba-tanker.com/
07-15-2019 09:18 AM
If you prefer a macro to concatenate, then consider:
Sub Sequelize() Dim rg As Range, targ As Range Dim delimiter As String, s As String Dim i As Long, j As Long, n As Long delimiter = ", " Set rg = Range("A2").CurrentRegion Set rg = rg.Offset(1, 0).Resize(rg.Rows.Count - 1, rg.Columns.Count) n = rg.Rows.Count j = 1 For i = n To 1 Step -1 If rg.Cells(i, 1).Value <> "" Then rg.Rows(i + 1).Resize(j - 1).EntireRow.Delete If rg.Cells(i, 2).Value <> "" Then s = rg.Cells(i, 2).Value & delimiter & s If s <> "" Then rg.Cells(i, 2).Value = Left(s, Len(s) - Len(delimiter)) j = 1 s = "" Else j = j + 1 If rg.Cells(i, 2).Value <> "" Then s = rg.Cells(i, 2).Value & delimiter & s End If Next End Sub
07-15-2019 09:20 AM
The formula was assuming that you already had the column A values listed as in your initial post.
When you subsequently changed your question, I wrote a macro to do the new request.
The attached workbook shows both the macro and the formula.
07-15-2019 09:36 AM
Thank you @Brad_Yundt. I am trying to test this now on my actual worksheet and the only problem I am having is that the sheet is 260,000 rows and my computer will freeze every time I try anything.
07-15-2019 09:43 AM
Please post a small sample workbook showing your actual data layout.
I propose to use array transfer for speed, and want to get the code right the first time. I therefore need to know the layout, extent of data and complicating factors (merged cells, worksheet protection, adjacent columns that must not be deleted, blank rows, etc).
Brad
07-15-2019 09:45 AM
Power Query - you may start from Getting Started with Get & Transform in Excel ,it gives the main idea. Technology requires some time to invest, but you may start doing something practical from very first steps.
For this sample you may open the query (Data->Queries $ Connections, double click on query in the right pane) and check step by step what it's doing.
07-15-2019 10:13 AM
Going somewhat out on a limb here without a sample workbook to look at. I revised the code to use array transfer into VBA and back. It will run a lot faster as a result.
Sub Sequelize() Dim rg As Range Dim delimiter As String, s As String Dim i As Long, k As Long, n As Long, nData As Long Dim vData As Variant, vResults As Variant delimiter = ", " Set rg = Range("A2").CurrentRegion Set rg = rg.Offset(1, 0).Resize(rg.Rows.Count - 1, rg.Columns.Count) n = rg.Rows.Count nData = Application.CountA(rg.Columns(1)) vData = rg.Value ReDim vResults(1 To nData, 1 To 2) For i = 1 To n If vData(i, 1) <> "" Then k = k + 1 vResults(k, 1) = vData(i, 1) If s <> "" Then If i > 1 Then vResults(k - 1, 2) = s End If s = IIf(vData(i, 2) = "", "", vData(i, 2)) Else If vData(i, 2) <> "" Then If s = "" Then s = vData(i, 2) Else s = s & delimiter & vData(i, 2) End If End If End If Next If s <> "" Then vResults(k, 2) = s rg.ClearContents rg.Resize(nData, 2).Value = vResults End Sub
07-15-2019 10:25 AM
I apoligize for the delay in response here @Brad_Yundt. I was having trouble with my laptop because excel was crashing often. I will attach a very small sample of the data I am working with. I am basically looking at all the software/applications installed on all the servers in my domain. I want all the applications belonging to each server to be in one cell block and be separated by a comma.
ServerA IE,Chrome,Windows Update, Intel driver, Edge, etc...
ServerB Chrome, Nvidia GPU Driver, Edge, etc....
Rather than what it currently is
ServerA IE
Chrome
Windows Update
Intel driver
Edge
etc...
Server B Chrome
Nvidia GPU Driver
Edge
etc....
07-15-2019 10:45 AM
07-15-2019 11:05 AM
My most recent macro is working on your test workbook. I installed the code, added a button to call it in the attached workbook so you may test. The duplicate data off to the right is so I can easily restore the original condition.
Brad
07-15-2019 11:13 AM
07-15-2019 11:18 AM
Here is the Power Query version. Applications are sorted alphabetically in the list.
07-15-2019 11:36 AM
07-15-2019 11:43 AM
Macro which Brad suggested works fine, but if you never used VBA it also takes time to learn how it works. VBA is much more universal, but, from my point of view, Power Query is more suitable for data transform tasks.
07-15-2019 11:45 AM
The macro assumes your data starts in row 2 and that header labels are in row 1. It further assumes that you have no blank lines in your data, and that there are only two columns of data.
Your question said that you wanted to start with two columns of data in columns A and B, then end up with two columns of compacted results in columns A and B. That's what the macro does.
The third from the last statement in the macro deletes the existing data (except header labels). The next to last statement puts the compacted results starting in cell A2. If you want them moved elsewhere, that statement is easily changed.
I made 6270 copies of your data, filling the sheet past row 260000 and ran the macro. It completed work in less than a second.
07-15-2019 12:00 PM
07-15-2019 02:34 PM
If you select column A with your raw data, how many values are there? It should say something like Count: 6267 at the right of the status bar at the bottom of the application window.
07-15-2019 03:30 PM
Currently 235493 entries on column A. @Brad_Yundt
07-15-2019 03:37 PM
07-15-2019 03:47 PM
Might you have empty strings or spaces in those "blank" cells? If you do, the macro won't work right.
07-15-2019 04:52 PM
You can test whether a cell is truly blank using a formula like:
=ISBLANK(A2)
My guess is that many of your column A cells look blank but actually contain an invisible value, such as a single quote, space, non-breaking space, Tab or Carriage Return character.
07-16-2019 09:41 AM
I formatted the empty cells and tested them with the =isblank and they all came up false, except for of course the cells with actual values. Although after deleting that =isblank column I still was unable to get the macro to run correctly. It is still deleting all the data in the worksheet. I am not sure what I am doing wrong. Will troubleshoot a bit more but I thought you might want an update. I also checked column B for blanks and came up with none as well. Thanks again Brad.
07-16-2019 09:53 AM
Here is a couple screenshots of the macro with a before and after running.
07-16-2019 10:17 AM
I would like test my code on your actual workbook. It's OK if you replace all the data with the letter "x" as long as you can reproduce the problem. You can post a workbook in this thread, or you could email it to me at first initial last name at my ISP, which is alum dot mit dot edu. I would then like to try reproducing the problem. Fixing the problem is easy if I can reproduce it.
It's possible that there is a limitation of Excel VBA present in your version of Excel that is not in mine. All my testing so far has been on 64-bit Excel 2016/Office 365 running on Windows 10--but I have other versions available for testing (both Mac and Windows, 32-bit and 64-bit, 2007 to date).
Please tell me:
Which version of Excel do you use, Mac or Windows?
If Windows, is 2007, 2010, 2013, 2016 or 2019?
Is it 32-bit or 64-bit?
07-16-2019 10:25 AM
07-16-2019 11:00 AM - edited 07-16-2019 11:01 AM
Thanks for sending me the file. I can reproduce the problem with my Excel 2016/Office 365 64-bit.
Excel cells can hold a maximum of 32,767 characters. Your problem server has 86,510 characters worth of software. I am guessing an overflow occurs that results in the loss of your data.
To test this hypothesis, I modified the code to truncate the software list to the first 32,767 characters. Now the macro seems to work.
Sub Sequelize() Dim rg As Range Dim delimiter As String, s As String Dim i As Long, k As Long, n As Long, nData As Long Dim vData As Variant, vResults As Variant delimiter = ", " Set rg = Range("A2").CurrentRegion Set rg = rg.Offset(1, 0).Resize(rg.Rows.Count - 1, rg.Columns.Count) n = rg.Rows.Count nData = Application.CountA(rg.Columns(1)) vData = rg.Value ReDim vResults(1 To nData, 1 To 2) For i = 1 To n If vData(i, 1) <> "" Then k = k + 1 vResults(k, 1) = vData(i, 1) If s <> "" Then If i > 1 Then vResults(k - 1, 2) = Left$(s, 32767) End If s = IIf(vData(i, 2) = "", "", vData(i, 2)) Else If vData(i, 2) <> "" Then If s = "" Then s = vData(i, 2) Else s = s & delimiter & vData(i, 2) End If End If End If Next If s <> "" Then vResults(k, 2) = Left$(s, 32767) rg.ClearContents rg.Resize(nData, 2).Value = vResults End Sub
Please try running the revised macro on your full dataset. If it works, we can then shift discussion to how best to handle the text in the overflow. Perhaps additional columns or rows?
07-16-2019 12:40 PM
It sounds like it will work. I will be able to try it in a couple hours or so and will report back.
As far as the overflow, additional rows are acceptable.@Brad_Yundt
07-16-2019 01:41 PM
SolutionI revised the macro to allow up to 200 more rows of results in case of overflow, as well as up to 165 characters in next pass through the concatenation loop. When an overflow situation is detected, the server name will be repeated as often as needed to get all software names listed on additional rows.
Sub Sequelize() Dim rg As Range Dim delimiter As String, s As String Dim i As Long, k As Long, n As Long, nData As Long Dim vData As Variant, vResults As Variant delimiter = ", " Set rg = Range("A2").CurrentRegion Set rg = rg.Offset(1, 0).Resize(rg.Rows.Count - 1, rg.Columns.Count) n = rg.Rows.Count nData = Application.CountA(rg.Columns(1)) nData = nData + 200 'Allow for servers whose software list exceeds 32,767 characters vData = rg.Value ReDim vResults(1 To nData, 1 To 2) For i = 1 To n If vData(i, 1) <> "" Then k = k + 1 vResults(k, 1) = vData(i, 1) If s <> "" Then If i > 1 Then vResults(k - 1, 2) = Left$(s, 32767) End If s = IIf(vData(i, 2) = "", "", vData(i, 2)) Else If vData(i, 2) <> "" Then If s = "" Then s = vData(i, 2) Else s = s & delimiter & vData(i, 2) End If End If End If 'Overflow occurs if you put more than 32,767 characters in a cell. This block allows up to 165 characters (plus delimiter) in next pass through loop. If Len(s) > 32600 Then If (i < n) And (vData(i + 1, 1) = "") Then vResults(k, 2) = s vResults(k + 1, 1) = vResults(k, 1) s = "" k = k + 1 End If End If Next If s <> "" Then vResults(k, 2) = Left$(s, 32767) rg.ClearContents rg.Resize(nData, 2).Value = vResults End Sub
07-16-2019 01:56 PM
You have done it! Thank you for your dedication to this issue of mine. This works perfectly. @Brad_Yundt
07-16-2019 02:01 PM
If not a secret, what's the reason keep list of server software in so long strings instead of columns cell?
07-16-2019 02:34 PM
I am importing this into a sharepoint webpage through the excel app and if I kept all the applications in columns then some of the servers would have 100+ columns. I am positive there is a better way to accomplish my task but am a new intern and I lack the knowledge to do so. :/
07-16-2019 03:03 PM
I see, thank you for the explanation. Perhaps servers in columns will be more compact and searchable, but it all depends on your concrete data.
07-16-2019 03:16 PM
Yeah, unfortunately I am working with such a large amount of data here that it doesn't want to cooperate by making it easy and convenient to read. There are thousands of servers and the software includes everything from Google Chrome to .net framework updates to security updates for various tools. But what I have now is the easiest to search through I believe. I can query the inventory for a server and then it will just show me the column(s) of data associated with it. From there I can just Ctrl+F to search more specifically. Thanks for trying to help me with my issue. I appreciate it. I have much to learn!!