VBA to copy column based on a header matching a cell value (date). The paste formulas into another column. Finally, hard code values after copying.

vekdas

New Member
Joined
Jan 26, 2022
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I am not great with VBA and I am trying to automate a forecasting spreadsheet. Any help would be much appreciated.

In reality the spreadsheet is more complicated but in simplified terms:

There are 3 sheets used to forecast Market Shares:
Sheet 1: Lookup sheet which shows the latest month market share for all the markets. Another column shows the lates month of the market share (same for all markets - this is duplicated because this is how the data is extracted):

1643295981525.png


Sheet 2 - Completed by Territory Managers for forecasting:
The column for the data's latest month will have formulas looking up the market share in each market in rows 4,6,8,10... and calculating product sales in rows 5,7,9,11... based on multiplying the market share in the cell above the market sales in column B.
The territory manager will then add their forecast market shares for each market for all months after the latest month (non green headers)
All historical months data need to be hard coded to remove formulas and only show values.

1643296195297.png


Sheet 3 - same as sheet 2 but for a different territory

So, I need a VBA to:
1. On Sheet 2, find the column whose header (on row 3) matches the Latest Month in Column B of Sheet 1 (the lookup sheet). In the example, that would be column L (October 2021)
2. Copy all the cells below the header in that column. The formulas (not values) from this column should be copied and pasted into the column to the right. In the example, that would be column M (November 2021). The formula needs to be copied in a way that it will be updated to lookup the new header.
3. Copy the same column identified in step 1 again and paste as values over the top of itself to hard code the data and remove all formulas.
4. Repeat this will Sheet 3 (territory 2). In reality this would need to be repeated for several territories.

Thank you for any help!!

Mini Sheet:

TEST.xlsm
ABCDEFGHIJKLMNOPQRS
1Actual m/s
2
3MARKETMARKET SALESJan-21Feb-21Mar-21Apr-21May-21Jun-21Jul-21Aug-21Sep-21Oct-21Nov-21Dec-21Jan-22Feb-22Mar-22Apr-22May-22
4MARKET 1£87,6617.7%8.7%9.7%10.7%11.7%12.7%13.7%14.7%15.7%16.7%5.0%5.0%10.0%20.0%25.0%25.0%30.0%
5MARKET 1£87,661£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 1,217£ 365£ 365£ 731£ 1,461£ 1,826£ 1,826£ 2,192
6MARKET 2£122,7630.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%5.0%7.0%
7MARKET 2£122,763£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 512£ 716
8MARKET 3£208,4140.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%2.0%5.0%10.0%
9MARKET 3£208,414£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 347£ 868£ 1,737
10MARKET 4£141,5500.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.2%0.0%0.0%0.0%0.0%0.0%0.0%0.0%
11MARKET 4£141,550£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 25£ -£ -£ -£ -£ -£ -£ -
12MARKET 5£151,6240.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%5.0%
13MARKET 5£151,624£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 632
14MARKET 6£126,2540.0%0.0%0.0%0.0%0.0%0.2%1.2%2.2%3.2%4.2%0.0%0.0%0.0%0.0%0.0%0.0%0.0%
15MARKET 6£126,254£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 441£ -£ -£ -£ -£ -£ -£ -
16MARKET 7£116,83743.4%44.4%45.4%46.4%47.4%48.4%49.4%50.4%51.4%52.4%3.0%3.0%3.0%3.0%3.0%3.0%3.0%
17MARKET 7£116,837£ 292£ 97£ 97£ 97£ 97£ 97£ 195£ 97£ 195£ 5,105£ 292£ 292£ 292£ 292£ 292£ 292£ 292
18MARKET 8£76,4130.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.9%0.0%0.0%0.0%0.0%0.0%0.0%0.0%
19MARKET 8£76,413£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 60£ -£ -£ -£ -£ -£ -£ -
20MARKET 9£42,4400.0%0.0%0.0%0.0%0.4%1.4%2.4%3.4%4.4%5.4%0.0%0.0%0.0%0.0%0.0%0.0%0.0%
21MARKET 9£42,440£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 190£ -£ -£ -£ -£ -£ -£ -
22MARKET 10£113,6770.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.6%0.0%0.0%0.0%0.0%0.0%0.0%0.0%
23MARKET 10£113,677£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 57£ -£ -£ -£ -£ -£ -£ -
24MARKET 11£68,1660.0%0.0%0.0%0.0%0.0%0.0%0.0%1.0%2.0%3.0%0.0%0.0%0.0%0.0%2.0%4.0%6.0%
25MARKET 11£68,166£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 170£ -£ -£ -£ -£ 114£ 227£ 341
26MARKET 12£23,5780.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.1%0.0%0.0%0.0%0.0%0.0%0.0%0.0%
27MARKET 12£23,578£ -£ -£ -£ -£ -£ -£ -£ -£ -£ 2£ -£ -£ -£ -£ -£ -£ -
28MARKET 13£234,5040.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.0%0.3%0.0%0.0%0.0%0.0%0.0%0.0%0.0%
29MARKET 13£234,504£ 391£ -£ -£ -£ -£ -£ -£ -£ -£ 53£ -£ -£ -£ -£ -£ -£ -
Territory 1
Cell Formulas
RangeFormula
A5,A29,A27,A25,A23,A21,A19,A17,A15,A13,A11,A9,A7A5=A4
L29:S29,L5:S5,L27:S27,L25:S25,L23:S23,L21:S21,L19:S19,L17:S17,L15:S15,L13:S13,L11:S11,L9:S9,L7:S7M5=($B5/12)*M4
L4,L6,L8,L10,L12,L14,L16,L18,L20,L22,L24,L26,L28L4=XLOOKUP(A4,'Latest MS'!A:A,'Latest MS'!C:C)
Named Ranges
NameRefers ToCells
'Latest MS'!_FilterDatabase='Latest MS'!$A$1:$C$43L4, L6, L8, L10, L12, L14, L16, L18, L20, L22, L24, L26, L28
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A121,C3:Z3Expression=A3<='Latest MS'!$B$2textNO
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
The following VBA finds Column L on Sheet 2 that is linked to the latest month on Sheet 1 (October 2021).
The code below then copies the formulas in that column and pastes them into the next column as required.

The problem is that it is also copying the header. I just want it to copy the cells beneath the header in that column.

I also need to copy the column again and paste as values over itself after copying the formulas to the adjacent column.

Any help would be much appreciated!


Sub Demo()
Dim vDate As Variant
Dim lr As Long
Dim Col As Variant
Dim NextCol As Long


Set vDate = Sheets("Latest MS").Range("B2")

With ThisWorkbook.Worksheets("Territory 1")

Col = Application.Match(vDate, .Rows(3), 0)

NextCol = Col + 1

lr = .Cells(.Rows.Count, 1).End(xlUp).Row

.Cells(1, Col).Resize(lr, 1).Copy Destination:=Cells(1, NextCol)


End With
End Sub
 
Upvote 0
So I have figured out how to paste as values over the original column using the code below.

The only thing I cannot figure out is how to not copy the header across to the adjacent column in the first copy and paste section of the code?

Sub Demo()
Dim vDate As Variant
Dim lr As Long
Dim Col As Variant
Dim LastCol As Long

' Sets the vDate
Set vDate = Sheets("Latest MS").Range("B2")

With ThisWorkbook.Worksheets("Territory 1")

Col = Application.Match(vDate, .Rows(3), 0)

LastCol = Col + 1

'Copy and paste formulas to adjacent column

lr = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(1, Col).Resize(lr, 1).Copy Destination:=Cells(1, LastCol)


'Copy and paste values over the top of column

lr = .Cells(.Rows.Count, 1).End(xlUp).Row
.Cells(1, Col).Resize(lr, 1).Copy
Cells(1, Col).PasteSpecial Paste:=xlPasteValues

End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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