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

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,138
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
 

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,480
Office Version
2013
Platform
Windows
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
 

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
8,010
Office Version
2019
Platform
Windows
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
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,138
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
 

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
8,010
Office Version
2019
Platform
Windows
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?
 

dmt32

Well-known Member
Joined
Jul 3, 2012
Messages
5,480
Office Version
2013
Platform
Windows
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
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,138
@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
 

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
8,010
Office Version
2019
Platform
Windows
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.
 

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
3,138
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
 

Forum statistics

Threads
1,089,515
Messages
5,408,740
Members
403,224
Latest member
rholmesa

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top