VBA to change horizontal range to vertical with conditions

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello experts

I need your expertise to help me write a code to arrange the data from horizontal data to vertical with some conditions. Sheet1 contains the horizontal data. In sheet1 cell G1 contains the name of the bank. The expected result is in the sheet2. The Cr Amt and Dr Amt are posted depending on the column “Type”. The amounts in sheet2 column Cr. Amt should be posted always positive and column Dr. Amt negative.
If, in sheet1, the Type is Receipt, then Name should be posted to Credit in sheet2 & amount must be posted positive and Bank Name should be in Debit column and amount should be negative.
If, in sheet1, the Type is Payment, then Name should be posted to Debit in sheet2 & amount must be posted positive and Bank Name should be in Credit column and amount should be negative.
Please note that the original data contains rows from 300 to 4000.
Query code 28.12.21.xlsx
ABCDEFGHI
1DateTypeNo.xxxxNameDebitCreditICICI Bank
228-12-2021Receipt1April500.00
328-12-2021Payment2October1000.00
428-12-2021Contra3Cash1500.00
528-12-2021Contra4Cash2000.00
6
Raw


Query code 28.12.21.xlsx
ABCDEFGH
1DateTypeNo.xxxxCredit Cr Amt Debit Dr Amt
228-12-2021Receipt1April500ICICI-500
328-12-2021Payment2ICICI1000October-1000
428-12-2021Contra3Cash1500ICICI-1500
528-12-2021Contra4ICICI2000Cash-2000
Result
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try this:

VBA Code:
Sub change_horizontal_to_vertical()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long
  Dim bnk As String
  
  With Sheets("Raw")
    bnk = .Range("I1").Value
    a = .Range("A2:G" & .Range("A" & Rows.Count).End(3).Row).Value
  End With
  
  ReDim b(1 To UBound(a, 1), 1 To 9)
  For i = 1 To UBound(a, 1)
    For j = 1 To 4
      b(i, j) = a(i, j)
    Next
    If a(i, 6) = "" Then
      b(i, 5) = bnk
      b(i, 6) = a(i, 7)
      b(i, 7) = a(i, 5)
      b(i, 8) = a(i, 7) * -1
    Else
      b(i, 5) = a(i, 5)
      b(i, 6) = a(i, 6)
      b(i, 7) = bnk
      b(i, 8) = a(i, 6) * -1
    End If
  Next
  Sheets("Result").Range("A2").Resize(UBound(b, 1), 8).Value = b
End Sub
 
Upvote 0
Solution
Try this:

VBA Code:
Sub change_horizontal_to_vertical()
  Dim a As Variant, b As Variant
  Dim i As Long, j As Long
  Dim bnk As String
 
  With Sheets("Raw")
    bnk = .Range("I1").Value
    a = .Range("A2:G" & .Range("A" & Rows.Count).End(3).Row).Value
  End With
 
  ReDim b(1 To UBound(a, 1), 1 To 9)
  For i = 1 To UBound(a, 1)
    For j = 1 To 4
      b(i, j) = a(i, j)
    Next
    If a(i, 6) = "" Then
      b(i, 5) = bnk
      b(i, 6) = a(i, 7)
      b(i, 7) = a(i, 5)
      b(i, 8) = a(i, 7) * -1
    Else
      b(i, 5) = a(i, 5)
      b(i, 6) = a(i, 6)
      b(i, 7) = bnk
      b(i, 8) = a(i, 6) * -1
    End If
  Next
  Sheets("Result").Range("A2").Resize(UBound(b, 1), 8).Value = b
End Sub
That was really fast DanteAmor. It's PERFECT. Thank you very much.
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,036
Members
449,205
Latest member
Eggy66

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