早上好,
我想在新单元格(I23)中创建一个句子,如果它们不为空,则所有单元格值都在J2:S2范围内。
这是我的董事会:
它应该标记一句话:“在今天早上我有oui,aaaaaah,nooooo,yeeees,225,也许,y,y”
但是pb是值的数量可以改变(你在第二行看到它)
这是我现在的代码:
y = Sum("J2:S2")
For Z = 1 To Z = y
If Range("J2:S2") <> " " Then
Cells(Z, 2) = Range("J2:S2")
End If
Next
答案 0 :(得分:1)
下午好。可能有用的东西:
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
范围内的所有单元格值打印到即时窗口。与空间分开。我在最后添加了一个句号,就你提到的那样,输出应该是一个句子。
如果你想让它成为一种情况,这是一个很好的例子: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