To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > Visual Basic > Office Development

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Dec 30th, 2005, 03:56 PM   #1
danielzzz
New Member
 
Join Date: Dec 05
Posts: 2
danielzzz is an unknown quantity at this point (<10)
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.     addd = 26
  8.     byp = "B"
  9.     While (addd < 11230)
  10.    
  11.     gtt = byp + addd
  12.    
  13.     sirr = Range(gtt).Value
  14.     numm = 1
  15.     countt = 0
  16.     While (numm < Len(sirr))
  17.         charr = Mid(sirr, numm, 1)
  18.         If (charr = " ") Then
  19.             countt = countt + 1
  20.         End If
  21.         If (charr = "/" Or charr = "&" Or charr = ",") Then
  22.             numm = 99
  23.         End If
  24.         numm = numm + 1
  25.     Wend
  26.     If (countt < 2 And Not (numm = 100)) Then
  27.         Range("B" + addd).Select
  28.         Selection.Font.ColorIndex = 2
  29.     End If
  30.     addd = addd + 1
  31.    
  32.     Wend
  33. 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!
danielzzz is offline   Reply With Quote
Old Dec 30th, 2005, 07:20 PM   #2
Comintern
Fanatic Member
 
Comintern's Avatar
 
Join Date: Nov 04
Location: Lincoln, NE
Posts: 826
Comintern has a spectacular aura about (150+)Comintern has a spectacular aura about (150+)
Re: Excel macro problem (Simple?)

Try this:
VB Code:
  1. Public Sub CheckCells()
  2.     Dim oSheet As Excel.Worksheet, lRow As Long, sTest As String, sWords() As String, bSkip As Boolean
  3.    
  4.     Set oSheet = Application.ActiveSheet
  5.    
  6.     For lRow = 26 To 11230              'loop through the cells
  7.         sTest = oSheet.Cells(lRow, 2).Value
  8.         If InStr(1, sTest, "/") Then bSkip = True
  9.         If InStr(1, sTest, "&") Then bSkip = True
  10.         If InStr(1, sTest, ",") Then bSkip = True
  11.         If Not bSkip Then
  12.             sWords = Split(sTest, " ")
  13.             If UBound(sWords) > 2 Then
  14.                 oSheet.Cells(lRow, 2).Font.ColorIndex = 2
  15.             End If
  16.         End If
  17.         bSkip = False
  18.     Next lRow
  19.    
  20.     Set oSheet = Nothing
  21.    
  22. End Sub
Comintern is offline   Reply With Quote
Old Dec 31st, 2005, 11:24 AM   #3
danielzzz
New Member
 
Join Date: Dec 05
Posts: 2
danielzzz is an unknown quantity at this point (<10)
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 !
danielzzz is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Office Development


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 12:49 PM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.