VBA - Data spread across multiple columns needs to be consolidated into one column

jmcginley3

New Member
Joined
Mar 28, 2018
Messages
14
We have a system that exports billing information for insurance purposes. The bills are typically generated via PDF, but business want them in Excel so they can do calculations and manipulations with them on a spreadsheet. Whenever we export a bill as an Excel file it comes out looking pretty messed up. The vendor we use for our billing system can't do anything about it, so my final option is to hopefully create a macro that will format to make it more legible and useable.

I have done some work with creating macros in the past, but this is the biggest project I've ever attempted to take on. I'm hoping someone will be willing to help me understand how I can overcome some of these bigger issues.

One of the biggest issues I'm coming across is the data is spread across multiple columns:


  1. Column B is supposed to be the Employee Name column (I just used letters of the alphabet as example names), however our export has some random instances where the employee's name is listed in column C instead
  2. Column F is supposed to be "class", but in some instances the "class" indication is listed in column E instead.
  3. On most of the spreadsheet you'll see that "Class", "Dental", and "Vision" are in columns F, G, H. At the bottom of the spreadsheet, the last section has these columns in E, F, H instead.

Any idea how to make sure all the data is in it's correct column? There really is nothing on this spreadsheet that is always constant except for the fact that "Emp No" (Employee Number) is always in Column A.

Is there any way to attach the actual file I'm working with? I've edited it to remove any sensitive data.

Here is a small example of what I'm talking about:

g, j, and p need to move over with the rest of the letters of the alphabet in the "Employee Name" column and the EE, EE+DEPS, and EE+SP need to move over with the rest of the listings in the Class column.

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Emp No[/TD]
[TD]Employee Name[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Class[/TD]
[TD]Dental[/TD]
[TD]Vision[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]283[/TD]
[TD]a[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE[/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]547[/TD]
[TD]b[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE + SP[/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]16[/TD]
[TD]c[/TD]
[TD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R]
[TR]
[TD]4112[/TD]
[TD]m[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE + SP[/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]664[/TD]
[TD]n[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE[/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]166[/TD]
[TD]o[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE + DEPS[/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2365[/TD]
[TD][/TD]
[TD]p[/TD]
[TD][/TD]
[TD]EE + SP[/TD]
[TD][/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]74411[/TD]
[TD]q[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE[/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]684[/TD]
[TD]r[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE + DEPS[/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1235[/TD]
[TD]s[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE[/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Is it always the case that anything in col C belongs in col B (same row) and anything in col E belongs in col F (same row)? And are those two types of misplaced data (B-data in C and F-data in E) the only types you encounter?
 
Upvote 0
That seems to be the case with B and C. Move anything found in C to B and keep in the same row.

I've included a screen shot with examples of the ways E and F get messed up. On my spreadsheet it's actually E that has most of the Class data. Sometimes the data will be in D and sometimes it will be in F. Sometimes it's like both the Name and the Class are shifted one column to the right, sometimes it's just one or the other. Sometimes when Class data is in shifted to the left into column D, the data for Dental and Vision is also shifted over to the left one column along with it. So that means there will be times when the Class column will have a value on a row, but the value it has is actually the value that is supposed to be in the Dental column.

doVUAmO
doVUAmO
[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Emp No[/TD]
[TD]Employee Name[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Class[/TD]
[TD]Dental[/TD]
[TD]Vision[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]198152[/TD]
[TD]a[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE[/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD]This is normal.[/TD]
[/TR]
[TR]
[TD]2155[/TD]
[TD]b[/TD]
[TD][/TD]
[TD][/TD]
[TD]EE[/TD]
[TD]10[/TD]
[TD]15[/TD]
[TD][/TD]
[TD]Class, Dental, and Vision data is all shifted to the left one column.[/TD]
[/TR]
[TR]
[TD]576343[/TD]
[TD]c[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE[/TD]
[TD][/TD]
[TD]Class data is shifted to the right one column (there is no data present for Dental/Vision in this cases -- which is correct)[/TD]
[/TR]
[TR]
[TD]688909[/TD]
[TD][/TD]
[TD]d[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]EE[/TD]
[TD][/TD]
[TD]Both the Name and the Class are shifted to the right one column.[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
It appears there are more than a few deviations from the norm that occur randomly. Not a situation that lends itself readily to writing a few lines of code to straighten things out. Maybe someone else will see an easier way.
 
Upvote 0
Thanks for the insight. Just so I can try to get it as automated as possible -- could you help with the part that is constant? The fact that anytime the Employee Name shows up in Column C that it should be moved to Column B? I ran a couple more extracts and confirmed there is nothing that should ever be in Column C that I want to keep. Moving everything from C to B would fix the names, and then I can move on to the next problem for now.
 
Upvote 0
You are welcome. I'll write some code that moves names back to col B for you to try and post back later.
 
Upvote 0
This assumes the layout shown in Post #1 where Emp No is in A1 and the only content in the whole of col C is the misplaced names.
Code:
Sub NamesToB()
Application.ScreenUpdating = False
On Error Resume Next
With Range("C:C").SpecialCells(xlCellTypeConstants, xlTextValues)
   .Offset(0, -1).Value = .Value
   .ClearContents
End With
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Thanks for the help! My actual spreadsheet has a few lines of headers, so all the actual data starts in row 7. I was messing around and modified your code to make it start at row 7 and it worked, but only for the first name that it came across. On this particular document I've got, there are 3 names in Col C that should be in Col B. It moved the name on line 299 like it should, but then on lines 342 and 350 the names are still in the wrong column. Does it need to loop through that code to process every name? Did I inadvertently break something when I tried to get it to start at row 7?

Code:
        Application.ScreenUpdating = False    On Error Resume Next
    With Range(ActiveSheet.Range("C7"), ActiveSheet.Range("C7").End(xlDown)).SpecialCells _
    (xlCellTypeConstants, xlTextValues)
       .Offset(0, -1).Value = .Value
       .ClearContents
    End With
    On Error GoTo 0
    Application.ScreenUpdating = True
 
Upvote 0
You set the target range in col C wrong. C7 to end(xlDown) will go from C7 to the next filled cell in col C. So, only C7, if it has a name in it, and that end cell will be moved.
Try this, and please copy it directly from your browser and test it before you do any "messing around" with it.
Code:
Sub NamesToB()
Dim lR As Long
lR = Cells(Rows.Count, "C").End(xlUp).Row
Application.ScreenUpdating = False
On Error Resume Next
With Range("C7:C" & lR).SpecialCells(xlCellTypeConstants, xlTextValues)
   .Offset(0, -1).Value = .Value
   .ClearContents
End With
On Error GoTo 0
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Awesome! That worked great. Thanks for your help! The only reason I tried modifying that first one was because it didn't work correctly (since my actual data started at Row 7 and you wouldn't have known that based on what I shared originally). I just copied this straight into my macro and it worked. Thanks for your explanation on why what I modified did not work correctly. I'm learning more and more everyday and trying to understand VBA and this has been immensely helpful!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top