VBA Copying and pasting certain columns to another worksheet

VBAExpertWannabe

New Member
Joined
Jan 8, 2020
Messages
10
Office Version
  1. 2019
Platform
  1. Windows
I'm trying to do a macro where I will copy data for a column that will be dynamic in Sheet 2, to the current worksheet, Column D. The dynamic column are months, January, February, March...and I want a macro so that if I input the month of "12", that, it will pick that column and then copy the entire column over to the first tab. So I guess I need a mechanism for tying the month to a column name/number?

Right now, my code is static but would like to make it dynamic.

VBA Code:
Sub CopyPaste()

Windows("book1.xlsm").Activate
Worksheets("Actual Budget").Activate
Range("M4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Worksheets("DECEMBER Summary").Activate
Range("D3").Select
ActiveSheet.Paste

End Sub

Please see screenshots below.
 

Attachments

  • Tab 2.PNG
    Tab 2.PNG
    49.9 KB · Views: 3
  • Tab1.PNG
    Tab1.PNG
    36.5 KB · Views: 4

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi and welcome to the board!

Try this code, Enter the month in cell H1 of the "DECEMBER Summary" sheet

VBA Code:
Sub Copying_pasting_certain_columns()
  Dim sh1 As Worksheet, sh2 As Worksheet, m As Variant, lr As Long
  
  Set sh1 = Sheets("DECEMBER Summary")
  Set sh2 = Sheets("Actual Budget")
  
  m = sh1.Range("H1").Value
  If m = "" Or m < 1 Or m > 12 Or Not IsNumeric(m) Then
    MsgBox "Enter number of month in H1"
    Exit Sub
  End If
  lr = sh2.Cells(Rows.Count, m + 1).End(xlUp).Row
  sh2.Range(sh2.Cells(4, m + 1), sh2.Cells(lr, m + 1)).Copy
  sh1.Range("D3").PasteSpecial xlPasteValues
  Application.CutCopyMode = False
End Sub
 
Upvote 0
Thank you sooo much! It worked PERFECTLY!

I'm relatively new to VBA but could you please explain the following..

lr = sh2.Cells(Rows.Count, m + 1).End(xlUp).Row
sh2.Range(sh2.Cells(4, m + 1), sh2.Cells(lr, m + 1)).Copy
 
Upvote 0
Thank you sooo much! It worked PERFECTLY!

I'm relatively new to VBA but could you please explain the following..

Get the last row with data from the captured month column
lr = sh2.Cells(Rows.Count, m + 1).End(xlUp).Row
m = month number + 1, if you put 1 then the column is 2, if you put 12 then the column is 13 (column M)

copy from cell (4, column m + 1) to column (last row with data, column m + 1)
sh2.Range(sh2.Cells(4, m + 1), sh2.Cells(lr, m + 1)).Copy
ex:
sh2.Range(sh2.Cells(4, 13), sh2.Cells(lr, 13)).Copy

I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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