Consulting

Results 1 to 4 of 4

Thread: Left & Copy

  1. #1

    Left & Copy

    Hi,

    Want to copy contents of cells in column K (sheet1), insert LEFT function (9) and copy to sheet2 column B. Following code looks
    like it should work but only copies the first item i.e. K1 to B1

    Sub COPYLEFT()
        Dim cell        As Range
        Dim sourceRange As Range
    
        Set sourceRange = Range(Sheets("Sheet1").Range("K1"), Selection.End(xlDown))
    
        For Each cell In sourceRange
            If IsEmpty(cell.Value) Then Exit For
    
            Sheets("Sheet2").Range("B" & cell.Row).Value = Left$(cell.Value, 9)
    
        Next
    
    End Sub
    Any help appreciated

    thanks
    Jon

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Sub COPYLEFT()
        Dim cell        As Range
        Dim sourceRange As Range
        
        With Sheets("Sheet1")
            Set sourceRange = .Range("K1", .Range("K" & .Rows.Count).End(xlUp))
        End With
        
        For Each cell In sourceRange
           If IsEmpty(cell.Value) Then Exit For
            
           Sheets("Sheet2").Range("B" & cell.Row).Value = Left$(cell.Value, 9)
            
        Next
         
    End Sub

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,877
    If there are empty cells amongst the data in column K then it will stop copying at the first empty cell - don't use Exit For.
    Sub COPYLEFT()
    Dim cell As Range
    Dim sourceRange As Range
    
    With Sheets("Sheet1")
      Set sourceRange = .Range("K1", .Range("K" & .Rows.Count).End(xlUp))
    End With
    For Each cell In sourceRange
      If Not IsEmpty(cell.Value) Then Sheets("Sheet2").Range("B" & cell.Row).Value = Left$(cell.Value, 9)
    Next
    End Sub
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  4. #4
    Thanks Guys,

    Both work fine

    regards
    Jon

Posting Permissions

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