How to move a column from a sheet to another depending if the last row contain of a column contains a specific value?

Miks89

New Member
Joined
Jan 23, 2018
Messages
4
Hello All,

I am actually working on a reporting file. I wish to have a VBA code which moves the columns from sheet1 to sheet2 if the last cell of the column contains a specific word. Can someone help me please? For example the values are from column B, first row is 3 and last row is 84 and if it contains completed or inactive, the entire column is moved to Sheet2 and deleted in Sheet1.

That would be really nice if someone could help me :)
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
where in sheet2 is the column to be moved to?

The column is to be moved as from column A row 2 in Sheet2. I wish to keep the history of the completed/inactive projects in sheet2 and add other columns once there are other projects from sheet1 which have been completed /inactive.
 
Upvote 0
Hi & welcome to the board
How about this
Code:
Sub MoveColumns()

   Dim Col As Long
   
   With Sheets("Sheet1")
      For Col = .Cells(3, Columns.Count).End(xlToLeft).Column To 2 Step -1
         If LCase(.Cells(84, Col).Value) = "completed" Or LCase(.Cells(84, Col).Value) = "inactive" Then
            .Columns(Col).Copy Sheets("Sheet2").Cells(3, Columns.Count).End(xlToLeft).Offset(-2, 1)
            .Columns(Col).Delete
         End If
      Next Col
   End With
         
End Sub
 
Upvote 0
Hi & welcome to the board
How about this
Code:
Sub MoveColumns()

   Dim Col As Long
   
   With Sheets("Sheet1")
      For Col = .Cells(3, Columns.Count).End(xlToLeft).Column To 2 Step -1
         If LCase(.Cells(84, Col).Value) = "completed" Or LCase(.Cells(84, Col).Value) = "inactive" Then
            .Columns(Col).Copy Sheets("Sheet2").Cells(3, Columns.Count).End(xlToLeft).Offset(-2, 1)
            .Columns(Col).Delete
         End If
      Next Col
   End With
         
End Sub
Hello Fluff,

Thank you for the reply. I adapted the code to what I wanted and it is working fine :)
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,864
Messages
6,121,981
Members
449,058
Latest member
oculus

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