MsgBox hvis
Udskrevet fra: Dansk Regneark Forum
Kategori: Hjælp til regneark.
Forum navn: Makro og VBA
Forum beskrivelse: Hjælp til Makroer og VBA-programmering
Web-adresse: https://forum.excel-regneark.dk/forum_posts.asp?TID=2344
Udskrevet den: 05.Dec.2026 kl. 18:07
Emne: MsgBox hvis
Besked fra: Mikkel
Emne: MsgBox hvis
Posteringsdato: 15.Dec.2015 kl. 16:13
Hej Forum,
Jeg kan ikke rigtigt få denne stump til at virke.
Meningen er den skal løbe gennem kolonne S og checke om der er tal over 8! Er der det skal den komme op med en boks, med en besked.
koden ser således ud:
Private Sub Worksheet_Calculate() If Intersect(Target, Range("S:S")) Is Nothing Then Exit Sub If Target.Value = ">8" Then MsgBox "You entered to many hrs. for travelling", vbExclamation End If End Sub
Private Sub Worksheet_Change(ByVal Target As Range) If Intersect(Target, Range("F1:F100")) Is Nothing Then Exit Sub If Target.Value = "Transport Hotel/Site - Site/Hotel" Then MsgBox "You have chosen Transport Hotel/Site - Site/Hotel, When working Offshore you are only entitled, to book 12,00 hours a day in total", vbExclamation If Target.Value = "Travelling Onshore (home/site - site/home - site/site)" Then MsgBox "You have chosen Travelling Onshore (home/site - site/home - site/site), When working Offshore you are only entitled, to book 8,00 hours per travel in total", vbExclamation If Target.Value = "Work time Onshore" Then MsgBox "You have chosen Work time Onshore, Please consider, to give detailed description and with an NCR number if applicable", vbExclamation If Target.Value = "Indirect time on site Offshore" Then MsgBox "You have chosen Indirect time on site Offshore, When Indirect time, check your garanteed hours for this day, your total ammount of hours are not to exceed the guranteed hours", vbExclamation If Target.Value = "Indirect time on site Onshore" Then MsgBox "You have chosen Indirect time on site Offshore, When Indirect time, check your garanteed hours for this day, your total ammount of hours are not to exceed the guranteed hours", vbExclamation If Target.Value = "Indirect time off site Onshore (abroad)" Then MsgBox "You have chosen Indirect time on site Onshore (abroad), When Indirect time, check your garanteed hours for this day, your total ammount of hours are not to exceed the guranteed hours", vbExclamation If Target.Value = "Indirect time off site Onshore (home)" Then MsgBox "You have chosen Indirect time on site Onshore (home), When Indirect time, check your garanteed hours while at Home, your total ammount of hours are not to exceed the guranteed hours", vbExclamation If Target.Value = "Transport Offshore" Then MsgBox "You have chosen Transport Offshore, Offshore Transport hours, are not to exceed 12 hours per day", vbExclamation
End Sub
|
Svar:
Besked fra: jespergn
Posteringsdato: 16.Dec.2015 kl. 12:51
|
Public Sub FindHours() Dim sht As Worksheet Dim LastRow As Long Dim i As Long Set sht = ThisWorkbook.Worksheets("Ark1") LastRow = sht.Cells(sht.Rows.Count, "S").End(xlUp).Row For i = 1 To LastRow If Cells(i, 19) > 8 Then MsgBox "You entered to many hrs. for travelling in row " & i End If Next i End Sub Skift Ark1 ud med arket på dit navn.
|
Besked fra: Mikkel
Posteringsdato: 16.Dec.2015 kl. 13:19
Hej,
Jespergn´s forsøg virker ikke helt efter hensigten, desværre kommer den op med MsgBox i samtlige celler, uden at der er nogen værdi.
Mikkel
|
Besked fra: excelent
Posteringsdato: 16.Dec.2015 kl. 20:19
|
Udskift dine 2 koder med denne : Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("F1:F100")) Is Nothing Then If Target.Value = "Transport Hotel/Site - Site/Hotel" Then MsgBox "You have chosen Transport Hotel/Site - Site/Hotel, When working Offshore you are only entitled, to book 12,00 hours a day in total", vbExclamation If Target.Value = "Travelling Onshore (home/site - site/home - site/site)" Then MsgBox "You have chosen Travelling Onshore (home/site - site/home - site/site), When working Offshore you are only entitled, to book 8,00 hours per travel in total", vbExclamation If Target.Value = "Work time Onshore" Then MsgBox "You have chosen Work time Onshore, Please consider, to give detailed description and with an NCR number if applicable", vbExclamation If Target.Value = "Indirect time on site Offshore" Then MsgBox "You have chosen Indirect time on site Offshore, When Indirect time, check your garanteed hours for this day, your total ammount of hours are not to exceed the guranteed hours", vbExclamation If Target.Value = "Indirect time on site Onshore" Then MsgBox "You have chosen Indirect time on site Offshore, When Indirect time, check your garanteed hours for this day, your total ammount of hours are not to exceed the guranteed hours", vbExclamation If Target.Value = "Indirect time off site Onshore (abroad)" Then MsgBox "You have chosen Indirect time on site Onshore (abroad), When Indirect time, check your garanteed hours for this day, your total ammount of hours are not to exceed the guranteed hours", vbExclamation If Target.Value = "Indirect time off site Onshore (home)" Then MsgBox "You have chosen Indirect time on site Onshore (home), When Indirect time, check your garanteed hours while at Home, your total ammount of hours are not to exceed the guranteed hours", vbExclamation If Target.Value = "Transport Offshore" Then MsgBox "You have chosen Transport Offshore, Offshore Transport hours, are not to exceed 12 hours per day", vbExclamation End If If Intersect(Target, Range("S1:S1000")) Is Nothing Then Exit Sub If Target.Value > 8 Then MsgBox "You entered to many hrs. for travelling", vbExclamation ActiveCell.Offset(-1, 0).Select Target = "" End If End Sub
------------- Jeg anvender Excel 2016 DK. Hvad anvender DU ? HUSK TILBAGEMELDING !!!
|
Besked fra: Mikkel
Posteringsdato: 17.Dec.2015 kl. 16:10
Hej,
Det virker desværre ikke som det skal.
Er der en venlig sjæl, som kan se hvad der er galt, med koden herunder, der er lavet lidt om på det så det er nemmere at rette i "beskederne" til brugeren, men det ser ikke ud til at virke desværre.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim feedback As String
Const msg As String = "You have chosen - "
Const msg_Guaranteed As String = "your total ammount of hours are not to exceed the guranteed hour"
Const msg1 As String = "When working Offshore you are only entitled, to book 12,00 hours a day in total"
Const msg2 As String = "When working Offshore you are only entitled, to book 8,00 hours per travel in total"
Const msg3 As String = "Please consider, to give detailed description and with an NCR number if applicable"
Const msg4 As String = "When Indirect time, check your garanteed hours for this day, " & msg_Guaranteed
Const msg5 As String = "When Indirect time, check your garanteed hours for this day, " & msg_Guaranteed
Const msg6 As String = "When Indirect time, check your garanteed hours for this day, " & msg_Guaranteed
Const msg7 As String = "When Indirect time, check your garanteed hours while at Home, " & msg_Guaranteed
Const msg8 As String = "Offshore Transport hours, are not to exceed 12,00 hours per day"
On Error GoTo wsc_exit
Application.EnableEvents = False
If Intersect(Target, Me.Range("F6:F123,M6:M123,N6:N123")) Is Nothing Then Exit Sub
If Me.Cells(Target.Row, "S").Value > 8 Then
Select Case Me.Cells(Target.Row, "F").Value
Case "Transport Hotel/Site - Site/Hotel": feedback = msg1
Case "Travelling Onshore (home/site - site/home - site/site)": feedback = msg2
Case "Work time Onshore": feedback = msg3
Case "Indirect time on site Offshore": feedback = msg4
Case "Indirect time on site Onshore": feedback = msg5
Case "Indirect time off site Onshore (abroad)": feedback = msg6
Case "Indirect time off site Onshore (home)": feedback = msg7
Case "Transport Offshore": feedback = msg8
End Select
If feedback <> "" Then
MsgBox msg & vbNewLine & Me.Cells(Target.Row, "F").Value & vbNewLine & vbNewLine & feedback, vbExclamation
Me.Cells(Target.Row, "M").Resize(, 2).Value = ""
End If
End If
wsc_exit:
Application.EnableEvents = True
End Sub
|
|