VBA Copy column paste to sheet based on Month header

Glytch

New Member
Joined
Jan 14, 2014
Messages
12
Hello all,
I need some help, i went through several threads and didnt find a solution.

I have a file as example composed of 3 sheets
Sheet 1 "Month selection" - were user will pick the month he is working on
Sheet 2 "a) PL+BS+RE" - P&L statement were numbers will be finalized on collumn O
Sheet 3 - "A) P&L+BS" - Archive sheet were i want to paste collumn O to the correct month collumn

O2 cell will have the month name.
But i cant get the code to copy over the collumn to the correct month.

Any help would be much appreciated.

File example here: File Example Gofile
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,622
Office Version
  1. 2007
Platform
  1. Windows
Try this

VBA Code:
Sub CopyMonth()
  Dim f As Range, sMon As String, lr As Long
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
  
  Set sh1 = Sheets("Month selection")
  Set sh2 = Sheets("a) PL+BS+RE")
  Set sh3 = Sheets("A) P&L+BS")
  
  sMon = sh1.Range("B1").Value
  If sMon = "" Then
    MsgBox "Select month"
    Exit Sub
  End If
  
  Set f = sh3.Rows(3).Find(sMon, , xlValues, xlWhole)
  If Not f Is Nothing Then
    lr = sh2.Range("C" & Rows.Count).End(3).Row
    sh3.Range(sh3.Cells(5, f.Column), sh3.Cells(lr, f.Column)).Value = sh2.Range("O5:O" & lr).Value
  Else
    MsgBox "Month does not exists"
  End If
End Sub
 

Glytch

New Member
Joined
Jan 14, 2014
Messages
12
Try this

VBA Code:
Sub CopyMonth()
  Dim f As Range, sMon As String, lr As Long
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
 
  Set sh1 = Sheets("Month selection")
  Set sh2 = Sheets("a) PL+BS+RE")
  Set sh3 = Sheets("A) P&L+BS")
 
  sMon = sh1.Range("B1").Value
  If sMon = "" Then
    MsgBox "Select month"
    Exit Sub
  End If
 
  Set f = sh3.Rows(3).Find(sMon, , xlValues, xlWhole)
  If Not f Is Nothing Then
    lr = sh2.Range("C" & Rows.Count).End(3).Row
    sh3.Range(sh3.Cells(5, f.Column), sh3.Cells(lr, f.Column)).Value = sh2.Range("O5:O" & lr).Value
  Else
    MsgBox "Month does not exists"
  End If
End Sub


This is perfect. Its working exactly as needed.
Thank you for the time and support on this.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,622
Office Version
  1. 2007
Platform
  1. Windows
I'm glad to help you. Thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,685
Messages
5,637,808
Members
416,983
Latest member
LessThanAverageUser

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
Top