I have an excel sheet, with column both A、B、C、D.
Both C & D number changes all the time (they have different criteria), since it calculates by stock data that fetches in real-time.
I need message box to pop up both when C & D matches my target value, and showing the the ticker in column A, the name in column B, and the number in C/D.
With the help I know the code when there is only column C:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.column = 3 And Target.value >= -4 And Target.value <= 4 Then
Call MsgBoxMacro(Target.value, Target.column, Target.row)
End If
End Sub
Sub MsgBoxMacro(value, column, row)
MsgBox "Ticker: " & Cells(row, column - 2) & vbNewLine & "Stock Name: " & Cells(row, column - 1) & vbNewLine & "Variable Value: " & value
End Sub
I don't know what to do, when I want to add column D data into the code. (so i can have message box pop up when D number reaches the criteria) please help.
Thank you!
By passing another parameter to the function MsgBoxMacro will solve your problem:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.column = 32 And Target.value >= -4 And Target.value <= 4 Then
Call MsgBoxMacro(Target.value, Target.column, Target.row, 0)
End If
If Target.column = 33 And Target.value >= -4 And Target.value <= 4 Then
Call MsgBoxMacro(Target.value, Target.column, Target.row, 1)
End If
End Sub
Sub MsgBoxMacro(value, column, row, counter)
MsgBox "Ticker: " & Cells(row, column - 31 - counter) & vbNewLine & "Stock Name: " & Cells(row, column - 30 - counter) & vbNewLine & "Variable Value: " & value
End Sub
Hope this helps.
Something like this, not far from what you had. This will go in the worksheet where the changes are to be made.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 4 Then
If ((Target.Offset(0, -1).Value > -4 And Target.Offset(0, -1).Value < 4) And _
(Target.Value > -4 And Target.Value < 4)) Then
' Msgbox here
Else
End If
End If
End Sub
Related
I'm trying to copy some data from one Sheet to another using a vba script, it works fine but it doesn't appear to gather all the results, the data i have is split up over multiple tables so i assume it's seeing a blank space and stepping out but i'm not sure the solution! (the results i'm after are all letters i.e A-f and are all located on column C)
code below:
Sub copytoprint()
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Application.ScreenUpdating = False
On Error GoTo Err_Execute
LSearchRow = 2
LCopyToRow = 2
While Len(Range("C" & CStr(LSearchRow)).value) > 0
If InStr(1, Range("C" & CStr(LSearchRow)).value, "A") > 0 Then
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
Sheets("dest").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
LCopyToRow = LCopyToRow + 1
Sheets("source").Select
End If
LSearchRow = LSearchRow + 1
Wend
Application.CutCopyMode = False
Range("A3").Select
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
Input would just be a basic line of details i.e
ID person ref itemid itemname shape
Alphas1 bob A As01 Alphaselects1 circle
Alphas2 Stuart B As02 Alphaselects2 circle
Basically they are split up with many records I'd like it to grab all the A reference put them in one table then folow on with B C etc
Hope that makes a little sense?
Looks like you want to search from ActiveSheet using certain reference (A,B,C,,etc) and copy matching rows into unique destination sheets.
Below code will help you accomplish this, it separates the copying sub-procedure out into its own sub (called copyToSheet) and you can keep calling it from copytoprint() each time giving a reference and the destination sheet you desire.
Option Explicit
Private Sub copyToSheet(reference As String, shtSource As Worksheet, shtDest As Worksheet)
Dim x As Integer
Dim y As Integer
shtDest.Range("A2:Z" & shtDest.UsedRange.Rows.Count + 2).ClearContents
x = 2
y = 2
'loop until 20 consequtive rows have column C blank
While (Not shtSource.Range("C" & x).Value = "") _
And (Not shtSource.Range("C" & (x + 1)).Value = "") _
And (Not shtSource.Range("C" & (x + 5)).Value = "") _
And (Not shtSource.Range("C" & (x + 10)).Value = "") _
And (Not shtSource.Range("C" & (x + 20)).Value = "")
'If shtSource.Range("C" & x).Value, reference) > 0 Then
If shtSource.Range("C" & x).Value = reference Then
shtDest.Range("A" & y & ":Z" & y).Value = shtSource.Range("A" & x & ":Z" & x).Value
y = y + 1
End If
x = x + 1
Wend
End Sub
Public Sub copytoprint()
copyToSheet "A", ActiveSheet, Sheets("A")
copyToSheet "B", ActiveSheet, Sheets("B")
MsgBox "All matching data has been copied."
End Sub
So if I understood your problem correctly then you want to sort the data in sheet source first and then paste all of that data in another sheet.
If that's the case try this code.
Sub copytoprint()
Dim lastrow As Double
With Sheets("source")
lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("A2:F" & lastrow).Sort key1:=Range("C3:C" & lastrow), order1:=xlAscending, Header:=xlNo
End With
Sheets("dest").Range("A2:F" & lastrow).Value = Sheets("source").Range("A2:F" & lastrow).Value
End Sub
I need to change a value of a cell. For example, Range("B1").value is "Cake" and i do
Range("B1").value = "Bubblegum"
But, I have a Worksheet_Change event and when it starts, if I MsgBox "Target.value" it will be "Cake" and not "Bubblegum".
Here is my Event :
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cellAdress As String
cellAdress = Target.Column
If cellAdress = 5 Then
If Target.Value = "X" Then
Range("F" & Target.Row).Value = "0,0€"
ElseIf Target.Value = "O" Then
If Range("A" & Target.Row).Interior.ColorIndex = 2 Then
Range("F" & Target.Row).Value = Worksheets("Astreinte").Range("B10").Value
Else
Range("F" & Target.Row).Value = Worksheets("Astreinte").Range("B11").Value
End If
Else
Range("F" & Target.Row).Value = "Valeur Incorrecte"
End If
End If
End Sub
And Here is my code
For Each myDate In DateMonthCorrespondante
Range("E" & (2 + Day(myDate))).Value = "O"
Next
How can I change that?
Thanks for your help
You can try using the SelectionChange and Change events in tandem, viz
Dim v
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox v
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
v = Target.Value
End Sub
All this goes in the sheet module. Then you can add conditions to restrict the situations in which you want the box to appear and so on.
Disable the events before and re-enable after.
Application.EnableEvents = False
Range("B1").value = "Bubblegum"
Application.EnableEvents = True
Well, with your help i finally solved my problem.
I had an error in the line
Range("E" & (2 + Day(myDate))).Value = "O"
And because of the event, it didn't say it to me.
I have been trying to create a notification system for Excel VBA, however I've hit a brick wall that I can't seem to be able to solve. I keep getting Error 404 - Object Required for the code I created. Hope you all can help.
Public price_col As Range
Public vol_col As Range
Public Sub setVars()
Set price_col = Range("E2:E90")
Set vol_col = Range("J2:J90")
End Sub
Private Sub Worksheet_Calculate()
checkPrice price_col
checkVol vol_col
End Sub
Private Sub Worksheet_Change(ByVal target As Range)
setVars
If Not Intersect(target, price_col) Is Nothing Then
checkPrice target
End If
If Not Intersect(target, vol_col) Is Nothing Then
checkVol target
End If
End Sub
Public Sub checkPrice(target As Range)
**For Each cell In target**
Dim row As Long
row = Range(cell.Address).row
If cell.Value > 0.025 Then
If ThisWorkbook.getPriceState(row) <> 1 Then
MsgBox "Price " & Application.WorksheetFunction.RoundDown(cell.Value * 100 / 1, 0) * 1 & "% rise: " & Range(cell.Address).Offset(0, -2).Value
ThisWorkbook.setPriceState row, 1
End If
ElseIf cell.Value < -0.025 Then
If ThisWorkbook.getPriceState(row) <> -1 Then
MsgBox "Price " & Application.WorksheetFunction.RoundDown(cell.Value * 100 / 1, 0) * 1 & "% fall: " & Range(cell.Address).Offset(0, -7).Value
ThisWorkbook.setPriceState row, -1
End If
ElseIf cell.Value <> "" Then
If ThisWorkbook.getPriceState(row) <> 0 Then
ThisWorkbook.setPriceState row, 0
End If
End If
Next cell
End Sub
Public Sub checkVol(vol_col As Range)
For Each cell In vol_col
Dim row As Long
row = Range(cell.Address).row
If cell.Value >= 2.5 Then
If ThisWorkbook.getVolState(row) <> 3 Then
MsgBox "Volume Change above 250%" & Range(cell.Address).Offset(0, -7).Value
ThisWorkbook.setVolState row, 3
End If
ElseIf cell.Value >= 2 Then
If ThisWorkbook.getVolState(row) <> 2 Then
MsgBox "Volume Change above 200%" & Range(cell.Address).Offset(0, -7).Value
ThisWorkbook.setVolState row, 2
End If
ElseIf cell.Value >= 1.5 Then
If ThisWorkbook.getVolState(row) <> 1 Then
MsgBox "Volume Change above 150%" & Range(cell.Address).Offset(0, -7).Value
ThisWorkbook.setVolState row, 1
End If
ElseIf cell.Value <> "" Then
If ThisWorkbook.getVolState(row) <> 0 Then
ThisWorkbook.setVolState row, 0
End If
End If
Next cell
End Sub
I got the error on the code "for each cell in target"; which was bolded. Thanks for helping!
If your calculate event fires, and the variables aren't set, then you'll get the error. At the very least, you should add a call to setVars at the start of Worksheet_Calculate
I have value in Sheet1 "B" column which is to be vlookup in Sheet2 from Column "A to K" and copy the corresponding C column value of Sheet2 and paste it in Sheet1's E column.
I have tried with below code but it shows error as
Run-time error '1004':
Unable to get the Vlookup Property of the worksheetfunction class.
Sub vlook_up()
For i = 2 To 11
Cells("D" & i).Value = WorksheetFunction.VLookup(Sheets("Sheet1").Range("B" & i), Sheets("Sheet2").Range("A1:K500"), 3, 0)
Next i
End Sub
This works for me, however I feel you are passing the function an inappropriate var type.
Sub vlook_up()
For i = 2 To 11
Range("D" & i).Value = WorksheetFunction.IfError(Application.WorksheetFunction.VLookup(Sheets("Sheet1").Range("B" & i), Sheets("Sheet2").Range("A1:K500"), 3, False), "error")
Next i
End Sub
Try with this
Sub vlook_up()
For i = 2 To 11
Range("D" & i).Value = Application.WorksheetFunction.VLookup(Sheets("Sheet1").Range("B" & i), Sheets("Sheet2").Range("A1:K500"), 3, False);
Next i
End Sub
or
Sub vlook_up()
For i = 2 To 11
Range("D" & i).Value = Application.VLookup(Sheets("Sheet1").Range("B" & i), Sheets("Sheet2").Range("A1:K500"), 3, False);
Next i
End Sub
I am trying to run a code to validate a column "K". If any cells in column "K" is Null then an error message should pop up and the cell should turn to red. I tried the following code and it is working. Following is my issue.
I run the macro.
Macro detects the Null cell and pop up error msg.
I deleted the row with Null cell.
Run macro again.
Error msg pop up again. Last cell of column K turn into red eventhough that row doesnt have any data.
This is the code I am using
Sub Errormsg ()
count2 = Range("B:B").SpecialCells(xlLastCell).Row
For n = 2 To count2
If Range("K" & n).Value = vbNullString Then
Range("K" & n).Interior.ColorIndex = 3
MsgBox "Error ! Null value "
Exit Sub
End If
Next n
End Sub
Use another column (like an ID or something that is never going to blank) and use that in the IF statement too
Sub Errormsg ()
count2 = Range("B:B").SpecialCells(xlLastCell).Row
For n = 2 To count2
If Range("K" & n).Value = vbNullString AND Range("A" & n).Value <> "" Then
Range("K" & n).Interior.ColorIndex = 3
MsgBox "Error ! Null value "
Exit Sub
End If
Next n
End Sub
Actually, your code is also work the last row. Just remove last row from loop. It will OK.
Sub Errormsg ()
count2 = Range("B:B").SpecialCells(xlLastCell).Row
For n = 2 To count2 - 1 'Just modify it
If Range("C" & n).Value = vbNullString Then
Range("C" & n).Interior.ColorIndex = 3
MsgBox ("Error ! Null value ")
Exit Sub
End If
Next n
End Sub