VBA Loop for inserting data

excelproblem101

New Member
Joined
Jul 7, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I have raw data that I want to convert to be read easier.
There are lines open between each statement.

1. I would need the payment date(M67) to be pasted in every row in column K(In photo - K75 and K76) for the transactions
2. I would need the Internal code(C68) and code(C69) to be pasted next to the payment date (L75,L76 and M75, M76) for all the transactions

Now the tricky part is that there should be a loop to do this over and over for all the statements following from A1 until there is no data left.
Each statement has different amount of transactions inside but the format and position within the sheet remain the same.
 

Attachments

  • EXcel Problem.png
    EXcel Problem.png
    33.5 KB · Views: 13

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi,
As you say the format and position within the sheet remain the same, so this code would get data using an Offset based on "Bank Name" in column B. Since it's just using a loop, it would be very slow depending on the raw data size. Anyway, give this a try then.

VBA Code:
Sub CustomConvert()
    Dim lLastRow As Long    'last data row
    Dim sInternalCode As String
    Dim sCode As String
    Dim sPaymentDate As String
    Dim i As Long, j As Long  'counter for looping

    'Get last row
    lLastRow = Cells(Rows.Count, "A").End(xlUp).Row

    For i = 1 To lLastRow
        With Cells(i, "B")
            If .Value = "Bank Name" Then
                'Getting data
                sInternalCode = .Offset(2, 1).Value
                sCode = .Offset(3, 1).Value
                sPaymentDate = .Offset(1, 11).Value

                'Pasting data
                Do While .Offset(9 + j, -1) <> ""
                    .Offset(9 + j, 9).Value = sPaymentDate
                    .Offset(9 + j, 10).Value = sInternalCode
                    .Offset(9 + j, 11).Value = sCode
                    j = j + 1
                Loop
                j = 0
            End If
        End With
    Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,190
Members
452,616
Latest member
intern444

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top