[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Count ColB and set as range for code

Posted on 2011-10-14
5
Medium Priority
?
288 Views
Last Modified: 2012-05-12
Hi,

I have a piece of code below that works well, but it goes looking through the whole spreadsheet, i need it to count column B and use that as its range to work off, can anyone amend the code so it does this?

Thanks
Seamus
Public Sub Sample()
    Set ws = Sheets("SophisTreats")
    
    ClearText ws, 13, ""
    ClearText ws, 13, "FI Trade Support"
    ClearText ws, 13, ""
    ClearText ws, 13, "REFERRED TO"

    
       Application.CutCopyMode = False
End Sub

Public Sub ClearText(osht As Worksheet, Colm As Long, srchText As String)
    ExitLoop = False
    Set oRange = osht.Columns(Colm)

    SearchString = srchText
    
    Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
    
    If Not aCell Is Nothing Then
        strbCell = aCell.Address
        aCell.EntireRow.Delete
        Do While ExitLoop = False
            Set aCell = oRange.FindNext(After:=Range(strbCell))
            If Not aCell Is Nothing Then
                aCell.EntireRow.Delete
            Else
                ExitLoop = True
            End If
        Loop

    End If



End Sub

Open in new window

0
Comment
Question by:Seamus2626
  • 2
  • 2
5 Comments
 
LVL 24

Expert Comment

by:StephenJR
ID: 36967637
Seamus - it looks as though you can specify a column in the middle argument of ClearText so if you try 2 instead of 13 does that do what you want?
0
 

Author Comment

by:Seamus2626
ID: 36967651
I shouldnt have ClearText ws, 13, "" in twice, thats a typo.
13 is Col M which is the column i need to work off, the problem is because i am specifying blank, once it goes past my end data point (normally row 500) it is going to look at the whole of ColumnM and they alll have "" in after that.

So i need to some how set it to only look as far as the last piece of data

Thanks
Seamus
0
 
LVL 2

Accepted Solution

by:
jan24 earned 1000 total points
ID: 36967676
Try this - it uses UsedRange to make sure you're not going through the whole sheet, only the bit where there is data:

Public Sub Sample()
    Set ws = Sheets("SophisTreats")
    
    ClearText ws, 13, ""
    ClearText ws, 13, "FI Trade Support"
    ClearText ws, 13, ""
    ClearText ws, 13, "REFERRED TO"

    
       Application.CutCopyMode = False
End Sub

Public Sub ClearText(osht As Worksheet, Colm As Long, srchText As String)
    ExitLoop = False
    Set oRange = Intersect(osht.Columns(Colm), osht.UsedRange)

    SearchString = srchText
    
    Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
    
    If Not aCell Is Nothing Then
        strbCell = aCell.Address
        aCell.EntireRow.Delete
        Do While ExitLoop = False
            Set aCell = oRange.FindNext(After:=Range(strbCell))
            If Not aCell Is Nothing Then
                aCell.EntireRow.Delete
            Else
                ExitLoop = True
            End If
        Loop

    End If



End Sub

Open in new window

0
 
LVL 24

Assisted Solution

by:StephenJR
StephenJR earned 1000 total points
ID: 36967682
I see, try this:
Public Sub ClearText(osht As Worksheet, Colm As Long, srchText As String)
    
    Dim r As Long
    
    ExitLoop = False
    
    With osht
        r = .Range("B" & Rows.Count).End(xlUp).Row
        Set oRange = .Range(.Cells(1, Colm), .Cells(r, Colm))
    End With

    SearchString = srchText
    
    Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
                LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False)
    
    If Not aCell Is Nothing Then
        strbCell = aCell.Address
        aCell.EntireRow.Delete
        Do While ExitLoop = False
            Set aCell = oRange.FindNext(After:=Range(strbCell))
            If Not aCell Is Nothing Then
                aCell.EntireRow.Delete
            Else
                ExitLoop = True
            End If
        Loop

    End If



End Sub

Open in new window

0
 

Author Closing Comment

by:Seamus2626
ID: 36967708
Cheers guys, spot on

Thanks
Seamus
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question