Consulting

Results 1 to 12 of 12

Thread: Address SPLIT

  1. #1
    VBAX Regular
    Joined
    Aug 2017
    Posts
    52
    Location

    Address SPLIT

    Hi all,

    I am currently working on a macro that transfers data from a worksheet to another. I'm having difficulties with splitting up the address into 3 columns. I need the address in Sheets("Sheet1").Range("C19") to be copied and split into Sheets("Sheet2").Range("AD3:AF3"). The address should be split at the commas, with the first section in AD3, second in AE3, and the last in AF3. add split.xlsx

  2. #2
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Sheets("Sheet2").Range("AD3:AF3") = WorksheetFunction.Transpose(Split(Sheets("Sheet1").Range("C19"), ","))
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Sam
    I don't think you need the transpose
    Sheets("Sheet2").Range("AD3:AF3") = Split(Sheets("Sheet1").Range("C19"), ",")
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Regular
    Joined
    Aug 2017
    Posts
    52
    Location
    Thank you for you response. Is it possible to loop the split to do the same for rows AD3:AF3 to AD17:AF17?

  5. #5
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Does that include C19 to C33?

    For i = 1 to 15
    With Sheets("Sheet2")
    .Range("AD" & i+2 &":AF" & i+2) = Split(Sheets("Sheet1").Range("C" & i + 18), ",") 
    End with
    Next
    Put many more Rows in there and this will start slowing down and you will need to look at All-Array codes.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  6. #6
    VBAX Regular
    Joined
    Aug 2017
    Posts
    52
    Location
    Nope, it will be using C19 as the address, just copying it to AD3:AF3 to AD17:AF17 in sheet2. Thank you for your prompt reply!

  7. #7
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Did you figure it out?
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    VBAX Regular
    Joined
    Aug 2017
    Posts
    52
    Location
    SamT, I managed to figure out, embedded an if statement as well. Does exactly what I need it to!

    For c = 1 To 15
        If Cells(c + 2, 1) <> "" Then
        With Sheets("Sheet2")
            .Range("AD" & c + 2 & ":AF" & c + 2) = Split(Sheets("Sheet1").Range("C19"), ", ")
        End With
        Else: Range("AD" & c + 2 & ":AF" & c + 2).Value = ""
        End If
        Next c
    Thank you for your help!

  9. #9
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    FYI "Resize" is easier to handle and adjust
    Range("AD" & c + 2 & ":AF" & c + 2) ==> Range("AD" & c + 2).Resize(,3)
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  10. #10
    reducing the worksheet interaction:

    Sub M_snb()
         sn = [C19:E24]
         
         For j = 1 To UBound(sn)
            st = Split(sn(j, 1), ",")
            sn(j, 1) = st(0)
            sn(j, 2) = st(1)
            sn(j, 3) = st(2)
         Next
         
         Sheet2.Cells(Rows.Count, 30).End(xlUp).Offset(1).Resize(UBound(sn), UBound(sn, 2)) = sn
    End Sub

  11. #11
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Resize? Yep.
    All-Array? Yep.
    Newbie? Yep. *KISE? Yep?

    *Self Explanatory
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  12. #12
    VBAX Regular
    Joined
    Aug 2017
    Posts
    52
    Location
    Hi snb,

    I'm not entirely sure I understand what your subroutine does. Could you elaborate a little so that I could incorporate it into my code? Thank you !

Posting Permissions

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