Csibész
New Member
- Joined
- Nov 17, 2005
- Messages
- 10
Hi,
First and foremost I'm a pre-novice when it comes to code writing so my problem likely be not more than a light finger exercise for all/most of you...
So... I receive xl files at regular intervals where data may be split into more than one cells (not more than 5). The goal is to combine the information in these successive cells into one and to eliminate the empty lines left behind. To my utter surprise my feeble attempt - listed below - almost works: sadly the last instance is ignored.
Could someone help me by showing where the problem is and what the solution might be?
For the purpose of illustration a simplified example (columns A&B) and the result after "treatment" (columns C&D) are shown below. Rows 9&10 are causing the grief...
Thanks!
First and foremost I'm a pre-novice when it comes to code writing so my problem likely be not more than a light finger exercise for all/most of you...
So... I receive xl files at regular intervals where data may be split into more than one cells (not more than 5). The goal is to combine the information in these successive cells into one and to eliminate the empty lines left behind. To my utter surprise my feeble attempt - listed below - almost works: sadly the last instance is ignored.
VBA Code:
Sub test()
Dim c, i, k As Integer
k = ActiveSheet.Range("a" & Rows.count).End(xlUp).Row
c = 0
For i = 1 To k
If IsEmpty(Cells(i, 1)) = True Then
' cell empty - increment counter
c = c + 1
Else
' MsgBox " Number of empty cells detected : " & c
'
' cell non-empty - if first and only non-empty occurence: do nothing, otherwise - do the processing
If c > 0 Then
Cells(i - c, 1).Activate
ActiveCell.Value = ActiveCell.Offset(-1, 1).Value + " " + ActiveCell.Offset(0, 1).Value
Selection.Cut
ActiveCell.Offset(-1, 1).Range("A1").Select
ActiveSheet.Paste
ActiveCell.Offset(1, -1).Range("A1").Select
Selection.EntireRow.Delete
ActiveCell.Offset(-1, 0).Range("A1").Select
c = 0
End If
End If
Next
' debug
' MsgBox "original last row: " & k & vbNewLine & _
"current row : " & i
End Sub
Could someone help me by showing where the problem is and what the solution might be?
For the purpose of illustration a simplified example (columns A&B) and the result after "treatment" (columns C&D) are shown below. Rows 9&10 are causing the grief...
Thanks!