Avatar billede Michael_H007 Juniormester
07. august 2015 - 13:14 Der er 10 kommentarer og
1 løsning

VBA teksboks

Har nedenstående kode, som laver en inputboks når en bestemt celle aktiveres - problemet er, at boksen overskriver teksten i cellen. Så jeg har behov for tjek om der er indhold i cellen, hvis ikke så skal boksen komme frem ellers ikke.

userInput = InputBox("Skriv dit input her:", "Skriv her")
userInput = Format(Date, "dd.mm.yy") & " " & Application.UserName & " " & userInput
Avatar billede store-morten Ekspert
07. august 2015 - 13:29 #1
Prøv:

Sub SkrivICelle2()

Dim userInput As String

On Error GoTo Fejl:

If Not ActiveCell = "" Then Exit Sub
    userInput = InputBox("Skriv dit input her:", "Skriv her")
    userInput = Format(Date, "dd.mm.yy") & " " & Application.UserName & " " & userInput

    ActiveCell.Value = userInput

Columns("D:D").EntireColumn.AutoFit

Exit Sub

Fejl:
MsgBox "Fejl"

End Sub
Avatar billede store-morten Ekspert
07. august 2015 - 14:23 #2
Hvis "Cancel" vælges:
Sub SkrivICelle2()

Dim userInput As String

On Error GoTo Fejl:

If Not ActiveCell = "" Then Exit Sub
    userInput = InputBox("Skriv dit input her:", "Skriv her")
If userInput = vbNullString Then Exit Sub

    userInput = Format(Date, "dd.mm.yy") & " " & Application.UserName & " " & userInput

    ActiveCell.Value = userInput

Columns("D:D").EntireColumn.AutoFit

Exit Sub

Fejl:
MsgBox "Fejl"

End Sub


Prøv evt. denne, hvor man kan tilføje tekst:
Sub SkrivICelle3()

Dim userInput As String

On Error GoTo Fejl:

If Not ActiveCell = "" Then
userInput = InputBox("Skriv dit input her:" & vbLf & _
        vbLf & "Teksten vil blive tilføjet efter:" & vbLf & _
        vbLf & ActiveCell, "Skriv her")
If userInput = vbNullString Then Exit Sub
    userInput = ActiveCell & " " & userInput
    ActiveCell.Value = userInput
GoTo Indsæt:
End If


    userInput = InputBox("Skriv dit input her:", "Skriv her")
If userInput = vbNullString Then Exit Sub
    userInput = Format(Date, "dd.mm.yy") & " " & Application.UserName & " " & userInput

Indsæt:
ActiveCell.Value = userInput

Columns("D:D").EntireColumn.AutoFit

Exit Sub

Fejl:
MsgBox "Fejl"

End Sub
Avatar billede Michael_H007 Juniormester
07. august 2015 - 14:33 #3
Tak virker fint - send svar så kommer der point
Avatar billede store-morten Ekspert
07. august 2015 - 14:44 #4
Den er god ;-)

Velbekomme
Avatar billede Michael_H007 Juniormester
07. august 2015 - 15:01 #5
Var lige for hurtig med sidste løsning - den giver en fejl. Har kopieret din kode ind.

Compile Error
Invalid outside procedure

On Error GoTo Fejl:
Avatar billede store-morten Ekspert
07. august 2015 - 16:17 #6
Bemærk:

Sub SkrivICelle3()

Ret til 2

eller på Ark1: Call SkrivICelle3
Avatar billede Michael_H007 Juniormester
10. august 2015 - 08:00 #7
@store-morten så virker det tak. Et lille supplerende spørgsmål - hvordan får jeg lavet linjeskift i cellen hvis jeg tilføjer tekst til en eksisterende tekst.
Avatar billede store-morten Ekspert
10. august 2015 - 11:45 #8
Med:  & Chr(10) &
Avatar billede store-morten Ekspert
10. august 2015 - 12:13 #9
Så tilføjes: linjeskift, ny dato, brugernavn og ny tekst:
Sub SkrivICelle3()

Dim userInput As String

On Error GoTo Fejl:

If Not ActiveCell = "" Then
userInput = InputBox("Skriv dit input her:" & vbLf & _
        vbLf & "Teksten vil blive tilføjet under:" & vbLf & _
        vbLf & ActiveCell, "Skriv her")
If userInput = vbNullString Then Exit Sub
    userInput = ActiveCell & Chr(10) & _
    Format(Date, "dd.mm.yy") & " " & Application.UserName & " " & userInput

    ActiveCell.Value = userInput
GoTo Indsæt:
End If

    userInput = InputBox("Skriv dit input her:", "Skriv her")
If userInput = vbNullString Then Exit Sub
    userInput = Format(Date, "dd.mm.yy") & " " & Application.UserName & " " & userInput

Indsæt:
ActiveCell.Value = userInput

Columns("D:D").EntireColumn.AutoFit

Exit Sub

Fejl:
MsgBox "Fejl"

End Sub
Avatar billede Michael_H007 Juniormester
10. august 2015 - 12:07 #10
Kan ikke lige finde ud af hvorhenne jeg skal tilføje det
Avatar billede Michael_H007 Juniormester
10. august 2015 - 12:57 #11
Perfekt - det fungerer
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester