Results 1 to 3 of 3

Thread: [RESOLVED] For next output to Excell cells

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Location
    Los Angeles, CA
    Posts
    4

    Resolved [RESOLVED] For next output to Excell cells

    Originally posted this by mistake in the classic VBA forum a few weeks ago and haven't received any responses... still looking for any help. Thanks.

    I have just started trying to program some macros for Excel. I am trying to change the code for a program I have. The current version will take a string of characters in a single cell in Excel such as "ABCDEFG" and put each letter in its own cell. I want to adapt this to three letters in each cell such as it will put "ABC" in one cell and "DEF" in another.

    Here is my code so far:


    VB Code:
    1. Dim Words() As String
    2.     Dim cell As Range, rng As Range, ans As Variant, rowformat As Variant, rng1 As Variant
    3.     Dim i As Integer, k As Integer, N As Integer, L As Integer
    4.    
    5.     ans = MsgBox("place words in rows (Yes) or columns(No)?", vbYesNoCancel)
    6.     If ans = vbYes Then
    7.         rowformat = True
    8.     ElseIf ans = vbNo Then
    9.         rowformat = False
    10.     ElseIf ans = vbCancel Then
    11.         Exit Sub
    12.     End If
    13.    
    14.     Set rng = Selection
    15.     N = rng.Rows.Count
    16.     rng.NumberFormat = "@"
    17.    
    18.     For Each cell In rng
    19.         If Len(cell.Value) > L Then
    20.             L = Len(cell.Value)
    21.         End If
    22.     Next cell
    23.    
    24.     ReDim Words(1 To N, 1 To L)
    25.     For k = 1 To N
    26.         For i = 1 To L Step 3
    27.             Words(k, i) = UCase(Mid(rng.Cells(k), i, 3))
    28.         Next i
    29.     Next k
    30.    
    31. 10  If rowformat Then
    32.         On Error GoTo errorhandler
    33.         Set rng1 = rng.Range(Cells(1, 1), Cells(N, L))
    34.         rng1.Value = Words
    35.     Else
    36.         Set rng1 = rng.Range(Cells(1, 1), Cells(L, N))
    37.         rng1.Value = Application.WorksheetFunction.Transpose(Words)
    38.     End If
    39.     rng1.NumberFormat = "@"
    40.    
    41.     Exit Sub
    42. errorhandler:
    43.     ans = MsgBox("string too long - column format?", vbYesNo)
    44.     If ans = vbYes Then
    45.         rowformat = False
    46.         GoTo 10
    47.     Else
    48.         Exit Sub
    49.     End If
    50.    
    51. End Sub
    I just added the "Step 3" to get what I want, but the problem is that the output is also skipping 3 lines and I have two blank cells in between my ouput. How can I get rid of these blank cells? Thanks for all help in advance!

    Matt

  2. #2
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: For next output to Excell cells

    Your Step 3 causes the blank cells, as I'm sure you're aware. Why not just use i from 1 to L/3 and then L/3 in subsequent lines?

    Alternatively, you could go through the entire range afterwards cell by cell checking the value - if "" then delete the cell using the Delete method. There is a Shift keyword which tells the cells which way to move.

    Try recording a macro in VB and then delete a cell - it's a very handy way to get code.

    HTH

    zaza

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Location
    Los Angeles, CA
    Posts
    4

    Re: For next output to Excell cells

    Hi Zaza thanks for the reply. I tried replacing with i = 1 to L/3 and L/3 in the code, but still couldn't get the correct result. I'm wondering if you can be a little more specific or possibly show me the code you mean?

    Also an alternate method I came up with is to remove blanks in the array using code like this:

    VB Code:
    1. j = 0
    2. For m = 1 To L
    3.     If Sequences(N, m) <> "" Then
    4.     ReDim Preserve Sequences2(N, j)
    5.     Sequences2(N, j) = Sequences(N, m)
    6.     j = j + 1
    7.     End If
    8. Next

    This almost works, but is giving me two arrays; one with all blanks and one with the correct values with no blanks, although I can't seem to get the rest of the code to output it correctly to the cells

    M

Posting Permissions

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



Click Here to Expand Forum to Full Width