I am currently using this code to display the number of "RCA Pending" found in a column. The message box does show the correct number of times it is found in the column, however, it creates a box for each instance (i.e. if there are 2 instances in the column, when the workbook is open it will display "Found 2 RCA Pending(s)", then when the user clicks OK, a second popup saying the same thing appears. If there are 5, you will get 5 consecutive popups).
Sub Auto_Open()
Dim row As Range
For Each row In Worksheets("Swivel").UsedRange.Rows
If row.Cells(1, "AB").Value = "RCA Pending" Then
MsgBox "Found " & WorksheetFunction.CountIf(Columns("AB"), "RCA Pending") & " RCA Pending(s)", vbInformation, "RCA Pending Found"
End If
Next row
End Sub
How can this be altered to show the total number of instances and not get multiple popups?
As a side note, I am using UsedRange because the range is always growing. The module that this code resides in has Option Explicit at the top.
Is this what you are trying?
Sub Auto_Open()
Dim instances As Long
instances = WorksheetFunction.CountIf(Columns("AB"), "RCA Pending")
If instances <> 0 Then _
MsgBox "Found " & instances & " RCA Pending(s)", vbInformation, "RCA Pending Found"
End Sub
OR
Sub Auto_Open()
Dim instances As Long
instances = WorksheetFunction.CountIf(Columns("AB"), "RCA Pending")
MsgBox "We Found " & instances & " instances of RCA Pending(s)", _
vbInformation, "RCA Pending Found"
End Sub
Related
I am trying to make a row in my Excel table mandatory before users close the document, then display a pop-up message stating "cells require input".
I am running into an issue where users are still getting the pop-up message even if they have filled out all the mandatory cells.
This is a screenshot of what all I typed out. I have this in the workbook area
I am typing what's in the screenshot, in the workbook area, and have it to run beforeclose.
This is the what I used below. My required fields is the row A3-O3
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Cells(3, 1)(3, 2)(3, 3)(3, 4)(3, 5)(3, 6)(3, 7)(3, 8)(3, 9)(3, 10)(3, 11)(3, 12)(3, 13)(3, 14)(3, 15).Value = "" Then
MsgBox "Cell(s) require input", vbInformation, "Kutools for Excel"
Cancel = True
End If
End Sub
view of my spreadsheet
A plus would be a pop-up message letting the user know which cells are empty & for it to highlight the cells that are empty also
Use WorksheetFunction.CountBlank:
If Worksheetfunction.CountBlank(ActiveSheet.Range("A3:O3")) > 0 Then
MsgBox "Cell(s) require input", vbInformation
End If
Or SpecialCells(xlCellTypeBlanks):
On Error Resume Next
Dim rng As Range
Set rng = ActiveSheet.Range("A3:O3").SpecialCells(xlCellTypeBlanks)
On Error GoTo 0
If Not rng Is Nothing Then
MsgBox "Cell(s) " & rng.Address(False, False) & " require input", vbInformation
End If
Note that Cells(3, 1)(3, 2)(3, 3)(3, 4)(3, 5)(3, 6)(3, 7)(3, 8)(3, 9)(3, 10)(3, 11)(3, 12)(3, 13)(3, 14)(3, 15) does not refer to A3:O3.
In the Immediate Window, put:
? Cells(3, 1)(3, 2)(3, 3)(3, 4)(3, 5)(3, 6)(3, 7)(3, 8)(3, 9)(3, 10)(3, 11)(3, 12)(3, 13)(3, 14)(3, 15).Address
The result is
$DB$31
I have a template that is being used to look up a list of accounts. Its kind of like a workflow tool, where tab 1 shows a list of active accounts. Tab 2 is where data formatting takes place and tab 3 is where the completed accounts are listed. This is being used by multiple people. So as they work through the list I wanted to apply some error handling that applies a vlookup to the account the user has selected just to see if its already in the completed tab by another user.
So essentially I really want it to Vlookup against the completed list. If a match then msgbox to say "This account has already been completed", else carry on formatting the data.
The code I was trying to use was:
Set WsInput = Sheets("Account Search")
Set PolLookup = WsInput.Range("U3")
Set PolRange = Sheets("Completed Accounts").Range("B5:B15000")
With WsInput
.Range("U3").Value = Application.VLookup(PolLookup, PolRange, 1, False)
If IsError(.Range("U3").Value) Then
*Format Data
Else
MsgBox "This account has already been completed", vbExclamation
Sheets("Completed Accounts").Select
Exit Sub
End If
End With
However I can't seem to get it to work. If I put a matching account number in it goes to the msgbox, which is what I want, but if I put a new number in, it also goes to the msgbox?
Any help or advice would be much appreciated.
Thanks
Sub Check()
Dim sPol As String, rngPol As Range
sPol = Sheets("Account Search").Range("U3").Value
Set rngPol = Sheets("Completed Accounts").Range("B5:B15000")
If IsError(Application.VLookup(sPol, rngPol, 1, False)) Then
' do something
MsgBox sPol & " not completed"
Else
MsgBox "Account " & sPol & " has already been completed", vbExclamation
Sheets("Completed Accounts").Select
Exit Sub
End If
End Sub
I have a number of things I want to achieve using VBA on a particular sheet.
1) Have a 3 button message box pop up when a certain condition is met.
2) Display the active cell address in a specific cell.
3) When hitting Enter after inputting data only to empty cells in a particular column, make the cursor jump to another column on the same row.
(Codes for each of these are at the end of the post)
I have the code to do all three of these things and they all work fine on their own, indeed the codes for items 1 & 2 also both work fine together, but when I add the code for item 3, a message box appears with
"Compile Error:
Ambiguous name detected: Worksheet_SelectionChange"
and the offending article in the code window is also highlighted.
I've noticed that the code for items 2 & 3 have the heading "Private Sub Worksheet_SelectionChange(ByVal Target As Range)"
If I remove the code for item 2, im then faced with a different message box
"Compile Error: Only comments may appear after End Sub, End Function, or End Property"
and highlights the words "Option Explicit" in the code window.
This also seems to break the code for item 1 as well.
I suspect the fact that I have two sets of code in the same window with the same "heading" might be the issue here. Is there a way for me to "blend" them so that they'll all play nicely with each other?
**---CODE FOR ITEM 1---**
Private Sub Worksheet_Calculate()
Dim r As Range
For Each r In Range("L:L")
If r.Value < 0 Then
result = MsgBox("You do not have enough stock to fulfil this request" & vbNewLine & vbNewLine & vbNewLine & _
"Please click: -" & vbNewLine & vbNewLine & _
" -Abort to order more stock" & vbNewLine & _
" -Retry to enter a different value" & vbNewLine & _
" -Ignore to receive stock", _
_
vbAbortRetryIgnore + vbDefaultButton2 + vbExclamation, "Negative Stock Level Warning")
End If
Next r
If result = vbAbort Then
MsgBox "Opening web browser", vbOKOnly + vbInformation, "New program warning!"
ActiveWorkbook.FollowHyperlink _
Address:="https://uk.rs-online.com/login", _
NewWindow:=True
End If
If result = vbRetry Then
MsgBox "Please enter a smaller parts count value", vbOKOnly + vbInformation, "Parts Count Input"
ActiveCell.Offset(-1, 0).Select
End If
If result = vbIgnore Then
MsgBox "You will now be directed to the Goods In window", vbOKOnly + vbInformation, "Receive Stock"
Sheets("Goods In").Activate
End If
End Sub
---CODE FOR ITEM 2---
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range("A1048575") = ActiveCell.Address
End Sub
---CODE FOR ITEM 3---
Option Explicit
Dim emptyCell As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub ' don't bother with multicell selections
If Cells(1, Target.Column).Value <> "S.I.#" Then Exit Sub ' don't bother with selections outside "S.I.#" column
If emptyCell And Not IsEmpty(Target.Value) Then Cells(Target.Row, Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Find(what:="Count", _
LookIn:=xlValues, lookat:=xlWhole).Column).Select ' if current cell was empty and now it is not then skip to "Count" column same row
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
emptyCell = IsEmpty(Target.Value) ' store the info about current selection being empty
End Sub
I'm attempting to write hyperlinks to cells via one button click from the ribbon in Excel 2007. These hyperlinks must, in turn, execute another routine that does stuff to the row on which they're located. However, the standard method of Worksheet_FollowHyperlink() will not work, as I need to execute this via an external xlam addin, and as far as I understand, this event triggers from hyperlink clicks located only in the same worksheet.
As a result, I have found that using the =HYPERLINK("#funName()","Click me") method works, but a problem exists that I am still experiencing.
The following is a trimmed down version of my already-working code:
Sub InsertLink() ' This is run directly by the callback for an IRibbonControl button click
With Sheet1
lastrow = Range("A" & Rows.count).End(xlUp).Row
While i <= lastrow
Range("E" & i).Value = "=HYPERLINK(""#consolidateDuplicate(A" & i & ",C" & i & ")"",""Copy this row to next"")"
Wend
Call MsgBox ("Finished inserting hyperlinks on each row.", vbOKOnly)
End With
End Sub
Sub consolidateDuplicate(PN, Quantity)
Dim thisRow, prevQuant As Long
thisRow = Selection.Row
MsgBox("You clicked the hyperlink for " & thisRow & "Q: " & Quantity & ", PN: " & PN)
End Sub
Clicking the ribbon button successfully writes all hyperlinks into their respective rows as expected. However, when I try to click any of these hyperlinks, thus executing consolidateDuplicate() in the same add-in module, I immediately receive a "Reference is not valid" warning followed by the MsgBox message.
I have tried removing all code from the routine to no avail. I also tried the standard brute-force bypass method of adding On Error Resume Next ... On Error Goto 0 as well as Application.DisplayAlerts = False ... Application.DisplayAlerts = True. Neither helps. I still receive the warning.
So while my script works fine and does what I want it to in the end, I feel like there should be a way to suppress the warning, or fix whatever is causing it, which I have a feeling, like Worksheet_FollowHyperlink(), is from storing the routine in the scope of an add-in module (required for my application), rather than keeping it totally contained in the calling workbook/sheet itself.
Any ideas?
Your consolidateDuplicate subroutine should be a Function, returning the address that the hyperlink should take you to. The lack of a return value is what is causing the "Reference is not valid" error.
The following code might work, effectively telling the hyperlink to take you to the cell in which the hyperlink was located:
Function consolidateDuplicate(PN, Quantity) As Range
Dim thisRow, prevQuant As Long
thisRow = Selection.Row
MsgBox ("You clicked the hyperlink for " & thisRow & "Q: " & Quantity & ", PN: " & PN)
Set consolidateDuplicate = Selection
End Function
I have been trying to check if two cells are empty. I know how to check for one cell but I can't get to figure out how to check two separate cells. This is one of the ways I have tried. When I run it, it will only check for F17 but not for F12, if I switch the numbers it will check only for F12. I appreciate any help. Thank you
Sub Button11VerifyFilePathPresent_Click()
Dim rCell As Range
On Error GoTo ErrorHandle
Set rCell = Range("F17", "F12")
If IsEmpty(rCell) Then
MsgBox "Please select the files" ' "Cell " & rCell.Address & " is empty."
End If
BeforeExit:
Set rCell = Nothing
Exit Sub
ErrorHandle:
MsgBox Err.Description & " Error in procedure CellCheck."
Resume BeforeExit
End Sub
Consider:
If Range("F17") & Range("F12") = "" Then
If the concatenation is null, then they both must be null.