Results 1 to 3 of 3

Thread: Excel macro problem (Simple?)

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    2

    Excel macro problem (Simple?)

    I'm a newbie to macro editing (though I do know basic VB) and am trying to create a macro which takes each cell in row B in a sheet from row 26 to 11230 and test if there are less than two spaces in the text of that cell (less than three words). If there's only one or zero spaces, the cell should be made invisible by making the text color white. Also, if the cell contains either "/" or "," or "&" somewhere in the text, the cell should NOT be whited out, even though there's less than three words. Finally, some cells may contain a space in the end of the text, why that last character is best not checked at all (the important thing is that no unsure cells are whited out). The main problem is how I convert numbers and strings into the irritating type Range, which is how I try to check all cells from X to Y. Here's my failing code, beware of stupid variable names...

    VB Code:
    1. Sub Makro4()
    2. '
    3. ' Makro4 Makro
    4. ' Makrot inspelat 2005-12-30 av Joel
    5. '
    6.  
    7. '
    8.     addd = 26
    9.     byp = "B"
    10.     While (addd < 11230)
    11.    
    12.     gtt = byp + addd
    13.    
    14.     sirr = Range(gtt).Value
    15.     numm = 1
    16.     countt = 0
    17.     While (numm < Len(sirr))
    18.         charr = Mid(sirr, numm, 1)
    19.         If (charr = " ") Then
    20.             countt = countt + 1
    21.         End If
    22.         If (charr = "/" Or charr = "&" Or charr = ",") Then
    23.             numm = 99
    24.         End If
    25.         numm = numm + 1
    26.     Wend
    27.     If (countt < 2 And Not (numm = 100)) Then
    28.         Range("B" + addd).Select
    29.         Selection.Font.ColorIndex = 2
    30.     End If
    31.     addd = addd + 1
    32.    
    33.     Wend
    34. End Sub

    Variable declarations:

    VB Code:
    1. Dim gtt As Range
    2. Dim addd As Integer
    3. Dim sirr As String
    4. Dim charr As String
    5. Dim numm As Integer
    6. Dim countt As Integer
    7. Dim byp As Range

    Thanks for your help!

  2. #2
    Fanatic Member Comintern's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln, NE
    Posts
    826

    Re: Excel macro problem (Simple?)

    Try this:
    VB Code:
    1. Public Sub CheckCells()
    2.  
    3.     Dim oSheet As Excel.Worksheet, lRow As Long, sTest As String, sWords() As String, bSkip As Boolean
    4.    
    5.     Set oSheet = Application.ActiveSheet
    6.    
    7.     For lRow = 26 To 11230              'loop through the cells
    8.         sTest = oSheet.Cells(lRow, 2).Value
    9.         If InStr(1, sTest, "/") Then bSkip = True
    10.         If InStr(1, sTest, "&") Then bSkip = True
    11.         If InStr(1, sTest, ",") Then bSkip = True
    12.         If Not bSkip Then
    13.             sWords = Split(sTest, " ")
    14.             If UBound(sWords) > 2 Then
    15.                 oSheet.Cells(lRow, 2).Font.ColorIndex = 2
    16.             End If
    17.         End If
    18.         bSkip = False
    19.     Next lRow
    20.    
    21.     Set oSheet = Nothing
    22.    
    23. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    2

    Re: Excel macro problem (Simple?)

    Thanks! Though the macro was supposed to white out cells with less than two spaces I easily fixed it so it works perfectly. I shall learn more about macro writing when I have time. Thanks again !

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