返回列表 上一主題 發帖

[發問] 如何利用VBA回傳文字?

回復 1# leehom


    不知道你所謂的特定文字是什麼?
不過你可以利用工作表事件來製作
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2. If Target.Address = "$B$1" Then
  3.     If Target = "" Then
  4.         Target.Offset(, -1) = ""
  5.     Else
  6.         Target.Offset(, -1) = Target & " DEF"
  7.     End If
  8. End If
  9. End Sub
複製代碼

TOP

本帖最後由 lpk187 於 2015-9-15 22:34 編輯

回復 3# leehom


    依照你傳回數字,可以利用Target的列號(Row)回傳
觸發則改為第二欄Column = 2 ,也就是B欄
  1. Private Sub Worksheet_Change(ByVal Target As Range)

  2. If Target.Column = 2 Then
  3.     If Target = "" Then
  4.         Target.Offset(, -1) = ""
  5.     Else
  6.         Target.Offset(, -1) = Target & " " & Target.Row
  7.     End If
  8. End If
  9. End Sub
複製代碼

TOP

回復 7# leehom


    整個走向錯誤了,不應該這樣去寫
一個問題是要考慮後來使用者該如何輸入,你第一個問題到後面這一個問題,恩考的方向完全不同了。
  1. Sub worksheet_change(ByVal Target As Range)
  2.      If Target.Address = "$B$2" Then
  3.      Application.EnableEvents = False
  4.      i = Application.InputBox("How many numbers you want to add?", "Prompt", , , , , , 1) - 1
  5.      For j = Target.Row + 1 To Target.Row + i
  6.          Cells(j, Target.Column) = Left(Cells(j - 1, Target.Column), 5) & Right(Cells(j - 1, Target.Column), 7) + 1
  7.          Cells(j, Target.Column - 1) = Cells(j, Target.Column - 1).Row
  8.      Next
  9.      Application.EnableEvents = True
  10.      End If
  11. End Sub
複製代碼

TOP

回復 9# leehom
  1. Sub worksheet_change(ByVal Target As Range)
  2.      If Target.Address = "$B$2" Then
  3.      Application.EnableEvents = False
  4.         Cells(2, Target.Column - 1) = 1
  5.      i = Application.InputBox("How many numbers you want to add?", "Prompt", , , , , , 1) - 1
  6.      For j = Target.Row + 1 To Target.Row + i
  7.          Cells(j, Target.Column) = Left(Cells(j - 1, Target.Column), 5) & Right(Cells(j - 1, Target.Column), 7) + 1
  8.          Cells(j, Target.Column - 1) = Cells(j, Target.Column - 1).Row - 1
  9.      Next
  10.      Application.EnableEvents = True
  11.      End If
  12. End Sub
複製代碼

TOP

回復 13# leehom

把Cells(2, "A") = 1 把這句改成    Cells(2, "A") = 1 & ")"

TOP

        靜思自在 : 知識要用心體會,才能變成自己的智慧。
返回列表 上一主題