Can you advise a different way of this code to work please

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,194
Office Version
  1. 2007
Platform
  1. Windows
Morning,
Please see working code shown below.

I have a worksheet where i enter data and in column G, the word POSTED is shown & also RED.
Once the parcel has been delivered on my userform i select the customer and press the command button.

Now looking on my worksheet i see what was POSTED "red highlite" is now yellow and 16/02/2020 etc etc

So looking at my code each time i press the command button i see a msgbox "do you want to open the userform"
No closes the msgbox & i work on the worksheet.
Yes then runs the code to see if there are any POSTED in column G
Depending on the outcome the form opens OR i am told all parcels delivered and userfrom doen not open.

WOW.
What can we do so i dont see the question everytime DO YOU WISH TO OPEN THE USERFORM ?

I just seem to be pressing YES all the time






VBA Code:
Private Sub Openuserform_Click()
Dim answer As Integer
answer = MsgBox("DO YOU WISH TO OPEN THE USERFORM", vbQuestion + vbYesNo + vbDefaultButton2, "POSTAGE OPEN USERFORM MESSAGE")
If answer = vbNo Then
  Exit Sub
Else
End If

Dim ws As Worksheet
Set ws = Sheets("POSTAGE")
Dim i As Integer
i = 1
Do Until i = 5000 ' <-- change number rows to check here
    If ws.Range("G" & i).Interior.Color = RGB(255, 0, 0) And ws.Range("G" & i).Value = "POSTED" Then
        PostageTransferSheet.Show
        Exit Sub
    End If
i = i + 1
Loop
MsgBox "NO NAMES TO SHOW AS ALL PARCELS HAVE NOW BEEN DELIVERED", vbInformation, "POSTAGE DATE TRANSFER SHEET MESSAGE"

End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
If you don't want the msgbox just delete all code shown in BOLD

Rich (BB code):
Private Sub Openuserform_Click()
Dim answer As Integer
answer = MsgBox("DO YOU WISH TO OPEN THE USERFORM", vbQuestion + vbYesNo + vbDefaultButton2, "POSTAGE OPEN USERFORM MESSAGE")
If answer = vbNo Then
  Exit Sub
Else
End If

Dim ws As Worksheet
Set ws = Sheets("POSTAGE")
Dim i As Integer
i = 1
Do Until i = 5000 ' <-- change number rows to check here
    If ws.Range("G" & i).Interior.Color = RGB(255, 0, 0) And ws.Range("G" & i).Value = "POSTED" Then
        PostageTransferSheet.Show
        Exit Sub
    End If
i = i + 1
Loop
MsgBox "NO NAMES TO SHOW AS ALL PARCELS HAVE NOW BEEN DELIVERED", vbInformation, "POSTAGE DATE TRANSFER SHEET MESSAGE"

End Sub
 
Upvote 0
If I'm following correctly, then this should show it once when you click the button, but not every time the form shows when the loop starts.
VBA Code:
Private Sub Openuserform_Click()
Dim answer As Integer
Static RunOnce As Boolean
If RunOnce = True Then
    answer = MsgBox("DO YOU WISH TO OPEN THE USERFORM", vbQuestion + vbYesNo + vbDefaultButton2, "POSTAGE OPEN USERFORM MESSAGE")
If answer = vbNo Then
  Exit Sub
Else
    RunOnce = True
End If

Dim ws As Worksheet
Set ws = Sheets("POSTAGE")
Dim i As Integer
i = 1
Do Until i = 5000 ' <-- change number rows to check here
    If ws.Range("G" & i).Interior.Color = RGB(255, 0, 0) And ws.Range("G" & i).Value = "POSTED" Then
        PostageTransferSheet.Show
        Exit Sub
    End If
i = i + 1
Loop
MsgBox "NO NAMES TO SHOW AS ALL PARCELS HAVE NOW BEEN DELIVERED", vbInformation, "POSTAGE DATE TRANSFER SHEET MESSAGE"
RunOnce = False
End Sub
 
Upvote 0
Hi,
Ive applied @dmt32 code & it works fine for me.

Also a question about the 5000 row code.
Remember i ship the parcel.
POSTED is shown on the worksheet.
Once delivered the text POSTED is changed to the dat in question.

So with regards the 5000 part in the code does it check EVERY time from row 1 to 5000 ?

The reason i ask is because i would assume the last cell with POSTED in would only be at the max say 2 weeks from the date at the time.
So it seems pointless that the code starts from row 1 say 14/10/2017 as the last POSTED in the cell is say 04/01/2020
Understand where im coming from.

Say next year im going to have to changed that 5000 to say 9000 but why ??? its checking rows that are way out of date so no need.

Can we alter the code so it only checks from say 3 weeks from the dat on the day.
Example.

Todays date 25/02/2020
Just run the check from 3 weeks today so 04/02/2020

Then lets say 28/02/2020 the code will run it check from 07/02/2020

Get where im coming from ?

Thanks

So the code i now have i use is.

VBA Code:
Private Sub Openuserform_Click()
Dim ws As Worksheet
Set ws = Sheets("POSTAGE")
Dim i As Integer
i = 1
Do Until i = 5000 ' <-- change number rows to check here
    If ws.Range("G" & i).Interior.Color = RGB(255, 0, 0) And ws.Range("G" & i).Value = "POSTED" Then
        PostageTransferSheet.Show
        Exit Sub
    End If
i = i + 1
Loop
MsgBox "NO NAMES TO SHOW AS ALL PARCELS HAVE NOW BEEN DELIVERED", vbInformation, "POSTAGE DATE TRANSFER SHEET MESSAGE"

End Sub
 
Upvote 0
Will the 'POSTED' cells always be the last on the list, or could they be in a mixed order?

My first thought would be to start at the bottom and work backwards until you find a cell that is not (255, 0, 0) and "POSTED"

Do you think that would work with your data?
 
Upvote 0
try replacing hard coded range 5000 with line shown in bold & see if this does what you want

Rich (BB code):
Dim ws As Worksheet
Set ws = Sheets("POSTAGE")
Dim i As Integer
i = 1
Do Until i = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
    If ws.Range("G" & i).Interior.Color = RGB(255, 0, 0) And ws.Range("G" & i).Value = "POSTED" Then
        PostageTransferSheet.Show
        Exit Sub
    End If
i = i + 1
Loop

Dave
 
Upvote 0
@dmt32
When i now press the command button with that line of code in place excel just hangs for a long time before the userform opens
 
Upvote 0
I think that I misread the code first time, give this a try.
VBA Code:
Private Sub Openuserform_Click()
Dim ws As Worksheet
Set ws = Sheets("POSTAGE")
Dim FirstRow As Long, LastRow As Long, fCell As Range
Set fCell = ws.Range("G:G").Find("POSTED", ws.Range("G1"), xlValues, xlWhole)
If Not Rng Is Nothing Then
    FirstRow = fCell.Row
    LastRow = ws.Cells(Rows.Count, 7).End(xlUp).Row
    Do Until FirstRow = LastRow
        If ws.Range("G" & i).Interior.Color = RGB(255, 0, 0) And ws.Range("G" & i).Value = "POSTED" Then
            PostageTransferSheet.Show
            Exit Sub
        End If
    LastRow = LastRow + 1
    Loop
Else
    MsgBox "NO NAMES TO SHOW AS ALL PARCELS HAVE NOW BEEN DELIVERED", vbInformation, "POSTAGE DATE TRANSFER SHEET MESSAGE"
End If
End Sub
Note that the Find method tends to be slower than a loop with small ranges, but there might be ways to improve it if it works.
 
Upvote 0
With that code i get variable not defind.
This is in yellow when i debug.

Rich (BB code):
Private Sub Openuserform_Click()
Dim ws As Worksheet
Set ws = Sheets("POSTAGE")
Dim FirstRow As Long, LastRow As Long, fCell As Range
Set fCell = ws.Range("G:G").Find("POSTED", ws.Range("G1"), xlValues, xlWhole)
If Not rng Is Nothing Then
    FirstRow = fCell.Row
    LastRow = ws.Cells(Rows.Count, 7).End(xlUp).Row
    Do Until FirstRow = LastRow
        If ws.Range("G" & i).Interior.Color = RGB(255, 0, 0) And ws.Range("G" & i).Value = "POSTED" Then
            PostageTransferSheet.Show
            Exit Sub
        End If
    LastRow = LastRow + 1
    Loop
Else
    MsgBox "NO NAMES TO SHOW AS ALL PARCELS HAVE NOW BEEN DELIVERED", vbInformation, "POSTAGE DATE TRANSFER SHEET MESSAGE"
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,733
Members
448,294
Latest member
jmjmjmjmjmjm

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