VBA Code to move Data

knaimi

New Member
Joined
Jun 23, 2013
Messages
49
Please see excel table below:



DateDescriptionDebitCreditMember Name
11/12/2023​
Item 1$ 36.72John Smith
11/12/2023​
Item 2$ 23.40John Smith
11/11/2023​
Item 3$ 96.35John Smith
11/7/2023​
Item 4$(1,283.96)Clara Smith
11/3/2023​
Item 5$ 401.55Clara Smith
11/3/2023​
Item 6$ 67.00Clara Smith
10/31/2023​
Item 7$ 43.70Clara Smith
10/30/2023​
Item 8$ 26.68John Smith
10/29/2023​
Item 9$ (15.89)John Smith
10/29/2023​
Item 10$ (327.98)John Smith
10/29/2023​
Item 11$ 155.07John Smith
10/29/2023​
Item 12$ 44.77John Smith


I’m trying to create a VBA code to move any numbers in column D (Credit) to the left cell in column C (Debit). Sometimes there are no numbers in column D, other times there is at least one. Basically I want all numbers in column C in with the same order



Thank you,



knaimi
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Hello,

This must do the job:
VBA Code:
Sub test()
  Dim columnC As Variant, columnD As Variant, i As Long
  columnC = Intersect(UsedRange, Columns("C"))
  columnD = Intersect(UsedRange, Columns("D"))
  For i = 2 To UBound(columnD, 1)
    If columnD(i, 1) <> "" Then
      columnC(i, 1) = columnD(i, 1)
    End If
  Next
  Range("C1").Resize(UBound(columnC, 1), 1).Value = columnC
End Sub
 
Last edited by a moderator:
Upvote 0
Hi ...
One more variant with minor changes:
VBA Code:
Sub test()
  Dim arr As Variant, i As Long
     arr = [a1].CurrentRegion.Value
        For i = 2 To UBound(arr)
             If arr(i, 4) <> "" Then
              arr(i, 3) = arr(i, 4)
              arr(i, 4) = ""
            End If
        Next
  [a1].CurrentRegion.Value = arr
End Sub
 
Upvote 1
Solution
Hello,

This must do the job:
VBA Code:
Sub test()
  Dim columnC As Variant, columnD As Variant, i As Long
  columnC = Intersect(UsedRange, Columns("C"))
  columnD = Intersect(UsedRange, Columns("D"))
  For i = 2 To UBound(columnD, 1)
    If columnD(i, 1) <> "" Then
      columnC(i, 1) = columnD(i, 1)
    End If
  Next
  Range("C1").Resize(UBound(columnC, 1), 1).Value = columnC
End Sub
Thank you Flashbond. I ran the code and it game with the following error

Run-time error ‘424’:

Object required

When I click on Debug, I got the following line highlighted:

columnC = Intersect(UsedRange, Columns("C"))
 
Upvote 0
Hi ...
One more variant with minor changes:
VBA Code:
Sub test()
  Dim arr As Variant, i As Long
     arr = [a1].CurrentRegion.Value
        For i = 2 To UBound(arr)
             If arr(i, 4) <> "" Then
              arr(i, 3) = arr(i, 4)
              arr(i, 4) = ""
            End If
        Next
  [a1].CurrentRegion.Value = arr
End Sub
Thank you, RustmaMika. The code worked perfectly
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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