VBA Cutting/Pasting from one column to another depending on variable

firesale

New Member
Joined
Jul 17, 2019
Messages
3
Good Evening, I'm still fairly new to VBA and my googling hasn't gotten me results. I'm trying to move data from 3 columns to a different 3 columns, same workbook and sheet, even the same row. The column data is being moved to depends on what is in the first column, from what I can tell this requires a Case statement. I've entered 3 of the values in my code so far and when I run the macro nothing actually happens. See code below.

Code:
Sub MoveTax()


Select Case Column1
Case "CA1"
Range("A:C").Cut Range("D:F")


Case "PA12"
Range("A:C").Cut Range("G:I")


Case "PA10"
Range("A:C").Cut Range("G:I")
End Select


End Sub



This is the desired outcome:

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Column 1[/TD]
[TD]Column 2[/TD]
[TD]Column 3[/TD]
[TD]Column 4[/TD]
[TD]Column 5[/TD]
[TD]Column 6[/TD]
[TD]Column 7[/TD]
[TD]Column 8[/TD]
[TD]Column 9[/TD]
[/TR]
[TR]
[TD]CA1[/TD]
[TD]$3[/TD]
[TD]$8[/TD]
[TD]here[/TD]
[TD]here[/TD]
[TD]here[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]CA1[/TD]
[TD]$5[/TD]
[TD]$7[/TD]
[TD]here[/TD]
[TD]here[/TD]
[TD]here[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]PA10[/TD]
[TD]$8[/TD]
[TD]$9[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]here[/TD]
[TD]here[/TD]
[TD]here[/TD]
[/TR]
[TR]
[TD]PA12[/TD]
[TD]$13[/TD]
[TD]$19[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]here[/TD]
[TD]here[/TD]
[TD]here[/TD]
[/TR]
</tbody>[/TABLE]










This is where I was getting my information from and I'm sure it's lacking context that I'm not able to determine: https://www.techonthenet.com/excel/formulas/case.php
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Code:
Dim rng As Range, cel As Range
Set rng = Range("A2:A" & Cells(Rows.Count, "A").End(3).Row)
For Each cel In rng
    Select Case cel
        Case "CA1": cel.Resize(, 3).Cut cel(1, 4)
        Case "PA10", "PA12": cel.Resize(, 3).Cut cel(1, 7)
    End Select
Next
 
Upvote 0
Code:
Dim rng As Range, cel As Range
Set rng = Range("A2:A" & Cells(Rows.Count, "A").End(3).Row)
For Each cel In rng
    Select Case cel
        Case "CA1": cel.Resize(, 3).Cut cel(1, 4)
        Case "PA10", "PA12": cel.Resize(, 3).Cut cel(1, 7)
    End Select
Next

You're awesome, this does what I need. Can you by chance explain to me what is going on with each Case statement and why they are written different? Reason I ask is I have up to 61 of these codes that will need shuffled in to columns depending on which code it is, so PA10 and 12 would go in those columns but PA15 would go in a different set of 3. I was hoping to replicate this script with the others and essentially update the "CA/PA/1/10/12" part of the string.

From what I'm understanding in the CA1 statement it's cutting 3 cells beginning with column 1 and pasting them in to 3 cells beginning with column 4? If my understanding of this is correct then I shouldn't have any problem duplicating your code. Again, thank you for your help!
 
Upvote 0
Your understanding of the code is correct.
The Case statements could also be written like this :
Code:
Case "CA1": cel.Resize(, 3).Cut cel.Offset(0, 3)
Case "PA10", "PA12": cel.Resize(, 3).Cut cel.Offset(0, 6)
It is not necessary to put PA10 and PA12 on separate lines since the action based these values is the same.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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