Excel MsgBox with VBA for multiple linked range - excel

I need some help with an excel problem.
It is a combination of the two problems below:
1) Excel - Popup message with sound alert when cell value meets certain criteria
2) VBA code to show Message Box popup if the formula in the target cell exceeds a certain value
In Sheet1, I have a range of products and sales figure.
Example: Sheet1
In Sheet2, I have multiple columns of sumif() functions. Example: Sheet2.
It contains a column of names in (A:A) and data in (B:B) & (C:C) which are linked to cells in other sheets. I would like a pop up notification saying ("Text in column A" sold is > 20) when the value of any cell in column B exceeds 20 or column C exceeds 40.
For example: If one of the cell value in column "B" gets updated to 33 (which is >20) and the corresponding cell value in column "A" contains text as "Charlie", excel sheet should popup message saying "Charlie sold is > 20".
The below VBA code accomplishes this IF it was raw data. However, it does not work when the cells are linked to data from other sheets, as in the case of this workbook.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.column = 2 and target.value > 1 Then
MsgBox target.offset(0,-1).text & " sold is > 20"
End If
End Sub
This alternative code works with data linked from other sheets, however it only applies to a specific cell, not an entire column.
Private Sub Worksheet_Calculate()
If Sheets("Sheet2").Range("B2").Value > 20 Then
MsgBox "B2 is >20", vbOKOnly
End If
End Sub
What I'm trying to achieve is this: As I input my raw data in Sheet1, the figures in Sheet2 column(B:B) and column(C:C) get updated. Once any of the cells in Sheet2 column(B:B) exceed 20 or column(C:C) exceed 40, there will be a popup notification that links back to column A text such as MsgBox target.offset(0,-1).text & " sold is > 20". Is there a way to combine the above two codes to achieve this?
Any alternative solutions are welcome too, thank you!

Compare all the Sums in Summary table
Private Sub Worksheet_Calculate()
Dim RangeToCheck As Range
Dim Cell As Range
Set RangeToCheck = Sheets("Sheet2").Range("B2:B5") 'See comment #1
For Each Cell In RangeToCheck.Cells
With Cell
If .Value2 > 20 Then
MsgBox "Product: " & .Offset(columnoffset:=-1).Value2 & _
" in cell: " & .Address & " is " & .Value2 & ">20", vbOKOnly
End If
End With
Next
End Sub
Comments
I recommend turning the range on Sheet2 to Excel table and using ListObject and Listcolumns instead.
Off topic hint: You can also use some counter, put the range into an VBA array and loop through array, it will be faster, than refering to sheet cell one after another. See Writing efficient VBA UDFs (Part 1).

Related

Limit Range Text Length

I'm running into an issue where I've set up Data Validation for characters that = 10 in a specific column in Excel. My issue is, if a user pastes over the cell, the data validation gets overwritten. Is there a way to accomplish a text limit by using VBA, that'll alert the user to only have 10 characters in a cell within a defined range? One that won't get overwritten if a user pastes over a cell?
Paste this into sheet's module:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In [E14:E1000]
If Len(cell) <> 10 Then
MsgBox "Text length in cell """ & cell.Address(0, 0) & """ does not equal to 10.", vbExclamation
Application.Undo
Exit For
End If
Next
End Sub
Explanation
When you paste text onto sheet, the procedure scans each cell in a range. As soon as it finds text with length more than 10, it notifies user which cell breaks the limit and undoes all the changes with Application.Undo. Then it immediately exits.

Concatenate text in VBA Excel with certain paramaters

What method would be best if I had the following.
3 checkboxes that if selected will populate 3 cells A1, A2 & A3. I need code that will check the 3 cells for data and create a singular string of info. I can use this with formula but need VBA to do the following to the cells as per below.
So my end result will be "cell1" (if only 1 checkbox is selected) or "cell1 & cell2" (if 2 checkboxes are selected) but if all 3 are selected I want the string to read "cell1, cell2 & cell3". There will only ever be 3 cells in this section.
I want the Concatenate Text to then go into a single cell. AA13 where it can be copied at a later stage.
I have another scenario where I have 6 checkboxes and I need the same thing as above, the 6 checkboxes populate 6 cells B1:B6. but, I need some code that makes sure a minimum of 2 check box's are selected. I can't work out how to get VBA to check for the number of tick boxes and report an error if it doesn't meet the criteria of 2 minimum.
I will have an active x button to run the code as per above to concatenate but when a check box is selected the cell will auto populate with the string i need.
Help would be much appreciated. I'm struggling to find code online to help me for these two scenarios
Consider:
Sub konkat()
Dim N As Long
With Application.WorksheetFunction
N = .CountA(Range("A1:A3"))
Select Case N
Case 0
Exit Sub
Case 1
Range("AA13").Value = Range("A1") & Range("A2") & Range("A3")
Case 2
Range("AA13").Value = .TextJoin(" & ", True, Range("A1:A3"))
Case 3
Range("AA13").Value = Range("A1").Value & " , " & Range("A2").Value & " & " & Range("A3").Value
End Select
End With
End Sub

Macro to select one cell if two other cells match

I need a Macro (must be a macro) that will select a cell if - and only if - two cells match. =IF(a3=k8) select k9. A3 is static, but the matching data can be anywhere along a row range. Therefore, I need to look for the data in a3 in the range k8:bz8, and in every case, follow by selecting the cell directly below it. Basically HLOOKUP. Once that cell is selected i will call another macro to populate that cell.
I am using Office 2016 for Mac (which sux)
Thanks
Don
Presumably you shouldn't have any trouble with the WorksheetFunction object using HLOOKUP function with a wildcard search.
dim val as variant
with activesheet
on error goto no_match
val = worksheetfunction.hlookup(chr(42) & .range("a3").value & chr(42), .range("k8:bz9"), 2, false)
on error goto 0
end with
debug.print val
no_match:
if err.number = 1004 Then _
debug.print "no match"
I think I would do a Do Until Loop. Something like:
Range("A8").Select
Do Until Selection = Range("A3").Value
ActiveCell.Offset(0, 1).Select
Loop
ActiveCell.Offset(0, 1).Select
To test it I put the value 14 in cell A3. Then I put a bunch of different values in each cell along row 8. I put 14 in k8. The macro will look at each cell and see if it equals 14 and then move on. You'll need an exit condition if you run out of data but that should get you started.

excel VBA print row based on date value

I am trying to print a report from a sheet called Patrol based on the date of the data.
Range A1 contains different dates but I only want to print the data in rows with first row = to (date + 1) ie tomorrows date.(To print delivery details for the next day)
For each row there is date and 16 other cells. The code below is only printing the table headers
Private Sub Comprintpatrol_Click()
Range("A1").AutoFilter Field:=1, Criteria1:=Format(Date + 1, Range("A2").NumberFormatLocal)
ActiveSheet.PrintOut
' or
Range("A1").CurrentRegion.PrintOut
Range("A1").AutoFilter
End Sub
Any Ideas?
I think you're off in saying "Range A1 contains different dates". I think you mean "Column A contains different dates". (Range A1 is a single cell, the upper-left cell on the sheet)
You can get column a from the Range function by passing the string "A:A": Range("A:A")
You could also try the UsedRange instead of CurrentRegion. The latter can be tricky because it depends a lot on how your data are set (especially if you have empty cells).
Something like:
Private Sub Comprintpatrol_Click()
Range("A1").AutoFilter Field:=1, Criteria1:=Format(Date + 1, Range("A2").NumberFormatLocal)
ActiveSheet.PrintOut
' or
ActiveSheet.UsedRange.PrintOut
Range("A1").AutoFilter
End Sub

Transfer values from one sheet of a workbook to another by clicking on the cell

I have 5,000 part numbers contained on one sheet of an Excel workbook (Part Numbers), ranging from cell A1:A5000. I want to find a way to click on any one of those part numbers and have it automatically populate into cell D7 on another sheet of the same workbook (Price Sheet). What is the easiest way to accomplish this task?
To do it that way, you will have to write VBA code to catch every SheetSelectionChange event to see if the new selection range is in your cells A1:A5000. And then if it is, execute the VBA code to fill OtherSheet!D7.
If I recall correctly, the VBA code to do this would look something like this:
Private Sub WorkSheet_SelectionChange(ByVal Target As Range)
'Check if it is in the range A1:A5000
If Target.Column = 1 And Target.Row <= 5000 Then
'get the target worksheet
Dim TargetWS As Worksheet
Set TargetWS = Sheets("OtherSheetsName")
'copy the value of the just selected cell to D7 on the target WS
TargetWS.Cells(7, 4).Value = Target.Value
End If
End Sub
(Oops, forgot about the need for "SET" in VBA.)
You can do this without VBA:
Select the partnumbers A1:A5000 and type PartNumbers in the Name Box (to the left of the formula bar) and press carriage return (OartNumbers should now be visible in the Name Box whenever you select a1:a5000
Now go to cell D7 on Price Sheet, and use Data Validation-->List and enter =PartNumbers in the Source box
Now you can select any of the 5000 part numbers from the dropdown in cell D7

Resources