PDA

View Full Version : VBA coding for Excel problem



McGabbarah
02-11-2020, 01:59 AM
Hi

I am trying to read a column of cells and have the content of the cell read into a variable as text.

I am using a for next loop.



Sub SplitCellBelowTotal()

Dim i As Long
Dim x As Long
Dim CurrentRecord As String


Range("A1").Select
x = Cells(Rows.Count, 1).End(xlUp).Row


For i = 1 To x


CurrentRecord = Right("A" & i, 5) ' want cell text characters


MsgBox Chr(CurrentRecord)


If CurrentRecord = "Total" Then ActiveCell.EntireRow.Insert


Next i


End Sub


Hope you understand what I am trying to do

snb
02-11-2020, 02:27 AM
Please use code tags.

See: https://www.snb-vba.eu/VBA_Arrays_en.html#L_5.4.1

p45cal
02-11-2020, 02:43 AM
CurrentRecord = Right("A" & i, 5)
Becomes:
CurrentRecord = Right(Range("A" & i), 5)

McGabbarah
02-12-2020, 02:45 PM
CurrentRecord = Right("A" & i, 5)
Becomes:
CurrentRecord = Right(Range("A" & i), 5)

SamT
02-12-2020, 04:09 PM
For i = 1 To x
should be
For i = x to 1 Step - 1

Reason: when you insert rows. the last row becomes Row(x + 1)