大家下午好。東西可能會工作:
Option Explicit
Public Sub J2S2()
Dim rngCell As Range
Dim strResult As String
For Each rngCell In Range("J2:S2")
If Len(rngCell) Then
strResult = strResult & " " & rngCell
End If
Next rngCell
Debug.Print Trim(strResult & ".")
End Sub
它打印到即時窗口範圍J2:S2
細胞的所有值。與空間分開。我在最後添加了一個句號,就你所說的輸出應該是一個句子。
如果你想讓它sentece情況下,這裏是一個很好的例子:Converting to sentence case using VBA
編輯: 另外,如果你想通過列循環首先,試試這個:
Option Explicit
Public Sub ColumnsFirstJ2S4()
Dim rngCell As Range
Dim strResult As String
Dim lngRow As Long
Dim lngCol As Long
Dim rngTarget As Range
Set rngTarget = ActiveSheet.Range("J2:S4")
With rngTarget
For lngCol = .Column To .Columns.Count + .Column
For lngRow = .Row To .Rows.Count + .Row
If Len(ActiveSheet.Cells(lngRow, lngCol)) Then
strResult = strResult & " " & ActiveSheet.Cells(lngRow, lngCol)
End If
Next lngRow
Next lngCol
End With
Debug.Print "ADDING TEXT here " & Trim(strResult & ".")
ActiveSheet.Range("I23") = "ADDING TEXT here " & Trim(strResult & ".")
End Sub
謝謝你爲你的答案Vityata!你的代碼很棒,他幫了我很多!但我仍然有一些問題,你的代碼一行一行地寫,是否可以逐列寫?如果我想寫一個單元格,我不能做Range(「I23」)。value = Debug.Print Trim(strResult&「。」)。如果我想在前面添加文字? – babou
@babou - 看到編輯:) – Vityata
它寫在一個單元格中,但仍然行:( – babou