我是VBA的新手,正在尝试确定如何在一个单元格中存储多个值。例如,我先:
我需要一些帮助来搞清楚:
感谢您提供任何帮助!
Sub EmptyCells()
Dim Cell As Range
Dim lrow As Long, i As Integer
Dim lcol As Long
Dim rw As Range
Dim reString As String
Dim ResultRng As Range
'Find the last non-blank cell in Column "School"
lrow = Cells(Rows.Count, 3).End(xlUp).Row
lcol = Cells(1, Columns.Count).End(xlToLeft).Column
MsgBox "Last Row: " & lrow
Set ResultRng = Range("G2:G3")
For Each rw In Sheets(1).Range("A1:F3").Rows
For Each Cell In rw.Cells
If IsEmpty(Cell.Value) Then
'MsgBox Cell.Address & " is empty. " & "The cell row number is " & Cell.Row & "." & vbNewLine & "The column header is " & Cell.Offset((1 - Cell.Row), 0)
ResultRng = Cell.Offset((1 - Cell.Row), 0)
End If
Next
Next
MsgBox "Complete"
End Sub
答案 0 :(得分:2)
我已经在这里更广泛地使用了你的lrow和lcol。
Sub EmptyCells()
Dim lrow As Long, lcol As Long
Dim i As Integer, r As Long, c As Long
Dim reString As String
With Worksheets("sheet1")
'Find the last non-blank cell in Column "School"
lrow = .Cells(.Rows.Count, 3).End(xlUp).Row
lcol = .Cells(1, .Columns.Count).End(xlToLeft).Column
MsgBox "Last Row: " & lrow
For r = 2 To lrow
reString = vbnullstring
For c = 1 To lcol
If IsEmpty(.Cells(r, c)) Then
'MsgBox .Cells(r, c).Address(0,0) & " is empty. " & _
"The cell row number is " & r & "." & vblf & _
"The column header is " & .Cells(1, c).value
reString = reString & ", " & .Cells(1, c).Value
End If
Next c
.Cells(r, c) = Mid(reString, 3)
Next r
End With
MsgBox "Complete"
End Sub