Consulting

Results 1 to 2 of 2

Thread: vba help

  1. #1
    VBAX Newbie
    Joined
    May 2016
    Posts
    1
    Location

    vba help

    hi, hope someone can help?

    I have 4 columns :A,B,C,D

    If A is more than 10 chars i want it copied to B and deleted from A
    If C has more than 10 chars, i want it copied to D and deleted from C

    If B or D aready have chars, and the contents of A or C are moving to them, i need the contents of A or B to go after the contents of C or D

    I need a script as there are thousands of records and i need it to do it to every row

    Thanks for the help!

    before
    Column A Column B Column C Column D
    supermarket Sunday jackhammers Blue



    after
    Column A Column B Column C Column D
    Sunday, Supermarket Blue, jackhammers

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,738
    Location
    Something like this probably

    In the attachment, 'testing' is the result with data that looked like 'Starting' sheet


    Option Explicit
    
    'If A is more than 10 chars i want it copied to B and deleted from A
    'If C has more than 10 chars, i want it copied to D and deleted from C
    'If B or D aready have chars, and the contents of A or C are moving to them,
    '   i need the contents of A or B to go after the contents of C or D
    
    Sub MoveData()
        Dim iLastRow As Long, i As Long
        
        Application.ScreenUpdating = False
        
        With ActiveSheet
            iLastRow = .Cells(1, 1).CurrentRegion.Rows.Count
        
            For i = 1 To iLastRow       ' assumes not header row
                If Len(.Cells(i, 1).Value) > 10 Then
                    If Len(.Cells(i, 2).Value) = 0 Then
                        .Cells(i, 2).Value = .Cells(i, 1).Value
                    Else
                        .Cells(i, 2).Value = .Cells(i, 2).Value & "," & .Cells(i, 1).Value
                    End If
                    
                    .Cells(i, 1).ClearContents
                
                End If
                
                If Len(.Cells(i, 3).Value) > 10 Then
                    If Len(.Cells(i, 4).Value) = 0 Then
                        .Cells(i, 4).Value = .Cells(i, 3).Value
                    Else
                        .Cells(i, 4).Value = .Cells(i, 4).Value & ", " & .Cells(i, 3).Value
                    End If
                    
                    .Cells(i, 3).ClearContents
                
                End If
            Next I
            
        End With
        
        Application.ScreenUpdating = True
    
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Tags for this Thread

Posting Permissions

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