Home |
Search |
Today's Posts |
#3
![]()
Posted to microsoft.public.excel.misc
|
|||
|
|||
![]()
You need to do a little setup for the code below to work. It needs a
'header' row in row 1. In that row are the labels that match the data in proper sequence, as: A B C D ..... U 1 Name Address Phone B-Day LastEntry Make sure those entries are spelled, or misspelled, exactly as they are in your data entries, but do not include the colon with them. Then your data starts at A2 and continues down/across the sheet. And there is nothing below the last name in column A, and nothing to the right of the last entry in the Header row (row 1). This does also assume that each entry has a Name entry. Next, make a copy of that sheet - in case my best laid plan goes awry, at least you'll only have messed up a copy, not the source data. It worked for me with the very limited data you provided, with a little extra added to it to test with. With the copy sheet selected, use Tools | Macro | Macros to run this code once you've got it in the workbook. To put it in your workbook, press [Alt]+[F11] to open the VB Editor, then choose Insert | Module from the VBE menu. Copy the code below and paste it into the module and close the VB Editor. Ready to rock and roll! Sub RightShiftData() 'assumes a full header row in row 1 'with 'tags' used for the fields in the data area 'also assumes each entry has a Name 'and that if there's something in the last field/column 'then that row needs no attention 'and that each entry in the data is identified by a 'unique word or phrase that ends with a colon [:] ' Dim lastRow As Long Dim lastCol As Integer ' only dealing with 20 or so Dim baseCell As Range Dim rOffset As Long ' for looping Dim cOffset As Integer ' for looping Dim hOffset As Integer 'for looping in header row Dim fieldName As String If Val(Left(Application.Version, 2)) < 12 Then 'in Excel 2003 or earlier lastRow = Range("A" & Rows.Count).End(xlUp).Row Else 'in Excel 2007 (or later?) lastRow = Range("A" & Rows.CountLarge).End(xlUp).Row End If Set baseCell = Range("A1") lastCol = baseCell.Offset(0, _ Columns.Count - 1).End(xlToLeft).Column Application.ScreenUpdating = False ' for performance For rOffset = 1 To lastRow - 1 If IsEmpty(baseCell.Offset(rOffset, lastCol - 1)) Then 'have work to do, nothing in far right column For cOffset = lastCol - 1 To 1 Step -1 If Not IsEmpty(baseCell.Offset(rOffset, cOffset)) Then 'may need to move this one 'dig out the field name, assumes field name ends 'with a colon [:] character. fieldName = UCase(Trim(Left(baseCell.Offset(rOffset, cOffset), _ InStr(baseCell.Offset(rOffset, cOffset), ":") - 1))) If UCase(Trim(baseCell.Offset(0, cOffset))) < fieldName Then 'must move it, not in proper column For hOffset = cOffset To lastCol - 1 If UCase(Trim(baseCell.Offset(0, hOffset))) = fieldName Then baseCell.Offset(rOffset, hOffset) = _ baseCell.Offset(rOffset, cOffset) 'erase old entry baseCell.Offset(rOffset, cOffset).ClearContents End If Next ' find column loop End If ' ck for already in proper column End If Next ' cOffset loop End If Next ' rOffset loop Application.ScreenUpdating = True End Sub "Gast" wrote: I have imported a large group of data and would like to sort it into some sort of order. Each row has 20-30 cells of data. All of the data is in the same order, but some of the rows are lacking some of the cells of data. I'd like to line up all the same types of data and insert either a blank cell or a "n/a" type value into the rows in the proper spot to align the data. I guess the best way is to give an example. Here's the data I have: Name | Address | Phone | B-Day | Name | Phone | B-Day | Name | Address | B-Day | Here's what I want to end up with: Name | Address | Phone | B-Day | Name | --------- | Phone | B-Day | Name | Address | ------- | B-Day | Each of the different types of data are identifiable based off the first part of the string. For example, all address cells start with "Address: " and all the phone cells start with "Phone: ". Thanks for the help in advance ^_^ |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
lining up 2 columns of zip codes | Excel Discussion (Misc queries) | |||
Data lining up correctly when printing | Excel Discussion (Misc queries) | |||
Change the data in one cell based on content of another. | Excel Discussion (Misc queries) | |||
Lining up data from concatenation | Excel Discussion (Misc queries) | |||
Extracting data based on content | Excel Discussion (Misc queries) |