2014年8月4日月曜日

EXCEL 仕事でつかえる小技 VBAで複数セル(文字列)の連結 EXCEL Tips at work-- VBA concatenating two cells

 覚えたいくつかの事柄の断片(スキル)が、ふとした瞬間につながることがある。
 EXCELでのセルの選択状態をVBAで読むFor Each in Selectionとコピペできるメッセージを出力できるInputBox
 小さな積み重ねだけど、できると案外、嬉しくなる。
One day, some pieces got at work connect each other and then become a tips.
"For Each in Selection" picks selected cells with the selected order and "InputBox" enables to copy and paste the message output by Inputbox.
Very tiny things but it make me happy when it come up as new idea.
 EXCELで選択している複数セル(文字列)を連結するVBA(マクロ)は、これ。
Here is the VBA which concatenates cells with the selected order.

Sub concatenate()
  For Each  r In Selection
    streach = r.Value
    If strselect = "" Then
      strselect = streach
    Else
      strselect = strselect & " " & streach
    EndIf
  Next
  InputBox Prompt:="Please Copy and Paste", Default:= strselect
End Sub
EXCELのSelectionを使うので、セルを選択した順番をキープして、連結が行われます。
規則性がない、離れたセルも連結できますよ。

Selection of EXCEL enables to pick up cells with the selected order for any cells. without any rules and even distant cells.

0 件のコメント:

コメントを投稿