Help with some code!

SteveG

New Member
Joined
Aug 1, 2002
Messages
36
I am a VBA newbie and need some help combining 2 diferent pieces of code together.

Below is code to delete rows that contain "invalid" in particular cells.

Private Sub Worksheet_Activate()
Dim MyRange As Range
Dim ThisCell As Range
Dim SearchVal As Boolean
Dim LastRow As Long
Dim r As Long
Set MyRange = Range("I1:I" & Range("I65536").End(xlUp).Row & ",M1:M" & Range("M65536").End(xlUp).Row)
For Each ThisCell In MyRange
If UCase(ThisCell.Value) = "INVALID" Then
If MsgBox("Invalid Blanket Wash data has been found. (ie. crawl washes, washes on shutdown) Would you like to delete these entries?", vbQuestion + vbYesNo, "Invalid Data Found") = vbYes Then
GoTo DeleteThem
Else
Exit Sub
End If
End If
Next ThisCell
Exit Sub
DeleteThem:
If Range("I65536").End(xlUp).Row > Range("M65536").End(xlUp).Row Then
LastRow = Range("I65536").End(xlUp).Row
Else
LastRow = Range("M65536").End(xlUp).Row
End If
For r = LastRow To 1 Step -1
If UCase(Range("I" & r).Value) = "INVALID" Or UCase(Range("M" & r).Value) = "INVALID" Then
Rows(r).EntireRow.Delete

End If
Next r
End Sub


********************************************************

Now I want to use a progress bar with that procedure and the code is as follows.

Sub ShowUserForm()
UserForm1.Show
End Sub

Sub Main()
Dim Counter As Integer
Dim RowMax As Integer, ColMax As Integer
Dim r As Integer, c As Integer
Dim PctDone As Single

Application.ScreenUpdating = False
' Initialize variables.
Counter = 1
RowMax = 100
ColMax = 25

' Loop through cells.
For r = 1 To RowMax
For c = 1 To ColMax
'Put a random number in a cell
Cells(r, c) = Int(Rnd * 1000)
Counter = Counter + 1
Next c

' Update the percentage completed.
PctDone = Counter / (RowMax * ColMax)

' Call subroutine that updates the progress bar.
UpdateProgressBar PctDone
Next r
' The task is finished, so unload the UserForm.
Unload UserForm1
End Sub

Sub UpdateProgressBar(PctDone As Single)
With UserForm1

' Update the Caption property of the Frame control.
.FrameProgress.Caption = Format(PctDone, "0%")

' Widen the Label control.
.LabelProgress.Width = PctDone * _
(.FrameProgress.Width - 10)
End With

' The DoEvents allows the UserForm to update.
DoEvents
End Sub

******************************************************

Can someone help me mesh these 2 together?

Thanks, Stevegr
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hello Steve,

I'm sure someone can, but it won't be me today.

How long does your procedure take?

With all due respect, my recommondation is focus on your first procedure and make it as efficient as possible, before mucking around with advanced bells and whistles to provide a gui for a non-optimal approach.

Check out the following procedure:

<font face=Courier New><SPAN style="color:darkblue">Option</SPAN> <SPAN style="color:darkblue">Compare</SPAN> <SPAN style="color:darkblue">Text</SPAN>

<SPAN style="color:darkblue">Sub</SPAN> DelFilt()
<SPAN style="color:darkblue">Dim</SPAN> Invld <SPAN style="color:darkblue">As</SPAN> Range, i <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Boolean</SPAN>, m <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Boolean</SPAN>
Application.ScreenUpdating = 0
<SPAN style="color:darkblue">Set</SPAN> Invld = [i:i].Find("invalid", MatchCase:=False)
<SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> Invld <SPAN style="color:darkblue">Is</SPAN> <SPAN style="color:darkblue">Nothing</SPAN> <SPAN style="color:darkblue">Then</SPAN> i = <SPAN style="color:darkblue">True</SPAN>
<SPAN style="color:darkblue">Set</SPAN> Invld = [m:m].Find("invalid", MatchCase:=False)
<SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> Invld <SPAN style="color:darkblue">Is</SPAN> <SPAN style="color:darkblue">Nothing</SPAN> <SPAN style="color:darkblue">Then</SPAN>
    m = <SPAN style="color:darkblue">True</SPAN>
Else: <SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> i <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">If</SPAN> i <SPAN style="color:darkblue">Then</SPAN>
    [i:i].AutoFilter Field:=1, Criteria1:="Invalid"
    [a2:a65536].SpecialCells(xlVisible).EntireRow.Delete
    ActiveSheet.AutoFilterMode = <SPAN style="color:darkblue">False</SPAN>
    <SPAN style="color:darkblue">If</SPAN> [i1] = "invalid" <SPAN style="color:darkblue">Then</SPAN> [a1].EntireRow.Delete
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">If</SPAN> m <SPAN style="color:darkblue">Then</SPAN>
    [m:m].AutoFilter Field:=1, Criteria1:="Invalid"
    [a2:a65536].SpecialCells(xlVisible).EntireRow.Delete
    ActiveSheet.AutoFilterMode = <SPAN style="color:darkblue">False</SPAN>
    <SPAN style="color:darkblue">If</SPAN> [m1] = "invalid" <SPAN style="color:darkblue">Then</SPAN> [a1].EntireRow.Delete
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
Application.ScreenUpdating = -1
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>

This runs fast enough, tested on 10,000+ rows of data, where there is no need for a progress indicater, it takes a second or two.

Sometimes procedures can't be sped up too much more and they're just going to take a while, but this is not one of those cases, and the progress bar slows down the procedure to an even greater extent.

You can call this from an event if you like. Hope this helps. :)
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,568
Members
449,237
Latest member
Chase S

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top