Consulting

Results 1 to 7 of 7

Thread: Solved: Organize inconsistant data from 2 columns to 1 column on different sheet

  1. #1
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location

    Solved: Organize inconsistant data from 2 columns to 1 column on different sheet

    I am trying to copy data to a second sheet and some of if is in one column and some in the column next to it..

    from sheet 1 I need to copy Column A and B to one column on sheet 2 and have their associated labels(in col C) put next to them.

    I'm kinda stuggling with this so If anyone can point me in the right direction I would appreciate any input.

    Attached file has some notes to help you understand.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    You will also notice that if they are found in column B that their label does not follow them to sheet 2.....just to make it more interesting.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Does this do it Steve

    [vba]

    Public Sub ProcessData()
    Const TEST_COLUMN As String = "D"
    Const SHEET_NAME As String = "Memory Map"
    Dim i As Long
    Dim LastRow As Long
    Dim NextRow As Long
    Dim NumRows As Long
    Dim sh As Worksheet

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    NextRow = 4
    On Error Resume Next
    Application.DisplayAlerts = False
    .Parent.Worksheets(SHEET_NAME).Delete
    Application.DisplayAlerts = True
    On Error GoTo 0
    Set sh = Worksheets.Add
    sh.Name = SHEET_NAME
    For i = 4 To LastRow

    NumRows = 1
    sh.Cells(NextRow, "B").Value = .Cells(i, TEST_COLUMN).Offset(0, -2)
    .Cells(i, TEST_COLUMN).Copy sh.Cells(NextRow, "C")
    If .Cells(i, TEST_COLUMN).Offset(0, -1).Value <> "" Then

    NumRows = 2
    sh.Cells(NextRow + 1, "B") = .Cells(i, TEST_COLUMN).Offset(0, -1)
    End If
    With sh.Cells(NextRow, "C").Resize(NumRows, 2)
    .BorderAround ColorIndex:=xlAutomatic, Weight:=xlThin
    .Interior.ColorIndex = 15
    End With
    If NumRows = 2 Then NextRow = NextRow + 1
    If .Cells(i, TEST_COLUMN).Offset(0, 1).Value <> _
    .Cells(i + 1, TEST_COLUMN).Offset(0, 1).Value Then

    NextRow = NextRow + 1
    .Cells(i, TEST_COLUMN).Offset(0, 1).Copy
    sh.Cells(NextRow, "D").PasteSpecial Paste:=xlValues
    sh.Cells(NextRow, "C").Resize(, 2).BorderAround _
    ColorIndex:=xlAutomatic, Weight:=xlThin
    End If

    NextRow = NextRow + 1
    Next i
    sh.Columns("C").AutoFit
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Forgot a small bit

    [vba]

    Public Sub ProcessData()
    Const TEST_COLUMN As String = "D"
    Const SHEET_NAME As String = "Memory Map"
    Dim i As Long
    Dim LastRow As Long
    Dim NextRow As Long
    Dim NumRows As Long
    Dim sh As Worksheet

    With ActiveSheet

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    NextRow = 4
    On Error Resume Next
    Application.DisplayAlerts = False
    .Parent.Worksheets(SHEET_NAME).Delete
    Application.DisplayAlerts = True
    On Error GoTo 0
    Set sh = Worksheets.Add
    sh.Name = SHEET_NAME
    For i = 4 To LastRow

    NumRows = 1
    sh.Cells(NextRow, "B").Value = .Cells(i, TEST_COLUMN).Offset(0, -2)
    sh.Cells(NextRow, "C").Value = .Cells(i, TEST_COLUMN)
    If .Cells(i, TEST_COLUMN).Offset(0, -1).Value <> "" Then

    NumRows = 2
    sh.Cells(NextRow + 1, "B") = .Cells(i, TEST_COLUMN).Offset(0, -1)
    End If
    With sh.Cells(NextRow, "C").Resize(NumRows, 2)
    .BorderAround ColorIndex:=xlAutomatic, Weight:=xlThin
    .Interior.ColorIndex = 15
    End With
    If NumRows = 2 Then NextRow = NextRow + 1
    If .Cells(i, TEST_COLUMN).Offset(0, 1).Value <> _
    .Cells(i + 1, TEST_COLUMN).Offset(0, 1).Value Then

    NextRow = NextRow + 1
    .Cells(i, TEST_COLUMN).Offset(0, 1).Copy
    sh.Cells(NextRow, "D").PasteSpecial Paste:=xlValues
    sh.Cells(NextRow, "C").Resize(, 2).BorderAround _
    ColorIndex:=xlAutomatic, Weight:=xlThin
    End If

    NextRow = NextRow + 1
    Next i
    sh.Columns("C").AutoFit
    End With
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Just amazing Bob. Thanks.
    Love the pastespecial to get rid of the formula's. This was one of the if statements I couldn't wrap my head around(seems obvious enough now):
    [VBA]If .Cells(i, TEST_COLUMN).Offset(0, -1).Value <> "" Then

    NumRows = 2
    sh.Cells(NextRow + 1, "B") = .Cells(i, TEST_COLUMN).Offset(0, -1)
    End If
    With sh.Cells(NextRow, "C").Resize(NumRows, 2)
    .BorderAround ColorIndex:=xlAutomatic, Weight:=xlThin
    .Interior.ColorIndex = 15
    End With[/VBA]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  6. #6
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Borders cleaned up nicely with the second one Bob.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yeah, I still had a copy in the first which I hadn't changed to a set to value, so the border came with it.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •