Copy cell range to another sheet into a specific column based on a criteria

mst3kr

New Member
Joined
Apr 15, 2013
Messages
46
Office Version
  1. 2019
  2. 2010
  3. 2007
Platform
  1. Windows
Hello all.

I have a file updated each day with two tabs: Sheet 1 has data for daily performance for some plants I am tracking.

Date1/17/22
Plant 18.4%
Plant 22.6%
Plant 33.7%
Plant 412.4%
Plant 56.0%

The second sheet has a recap that is needed to be captured each day and kept for historical & graphing purposes.

1-Jan2-Jan3-Jan4-Jan5-Jan6-Jan7-Jan8-Jan9-Jan10-Jan11-Jan12-Jan13-Jan14-Jan15-Jan16-Jan17-Jan18-Jan19-Jan20-Jan21-Jan22-Jan23-Jan24-Jan25-Jan26-Jan27-Jan28-Jan29-Jan30-Jan31-Jan
Plant 18.40%
Plant 22.60%
Plant 33.70%
Plant 412.40%
Plant 56.00%

What I'm trying to do is create some VBA that, upon saving, would take the percentage values from Sheet 1 and automatically copy & paste those values in Sheet 2 into the corresponding column based on the date in Sheet 1.

I already have the VBA to save a macro upon saving but I haven't been able to figure out how to copy & paste with the condition on the date. I could copy & paste the values manually each day, but in case I forget, I can make the VBA upon saving make sure this is done for me.

Any help would be appreciated!

Thanks!
-Greg (mst3kr)
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
i used 3 named ranges (see below)
VBA Code:
Sub Recapture()
     Set c1 = Sheets("sheet1").Range("MySource")                'range in sheet1 with your new data
     Set c2 = Sheets("sheet2").Range("MyDates")                 'range in sheet2 with your dates
     Set c3 = Sheets("sheet2").Range("MyPlants")                'range in sheet2 with your plants

     k = Application.Match(c1.Cells(1, 2).Value2, c2.Value2, 0)     'find the relative position of your date in that range of dates (use value2, to be independent of numberformat) !
     If Not IsNumeric(k) Then MsgBox "date isn't found", vbCritical: Exit Sub     'that position isn't found = end of story

     For i = 2 To c1.Rows.Count                                 'loop through the data of the different plants
          r = Application.Match(c1.Cells(i, 1), c3.Value, 0)    'find the relative position of your plant in the range of plants
          If Not IsNumeric(r) Then                              'position isn't found
               MsgBox "row for " & c1.Cells(i, 1).Value & " not found"     'msgbox to tell
          Else
               Sheets("sheet2").Cells(c3.Row + r - 1, c2.Column + k - 1).Value = c1.Cells(i, 2)     'write your value to that cell in sheet2 with that rownumber and columnnumber
          End If
     Next
End Sub

Map1
BC
11Date01/17/22
12Plant 18,40%
13Plant 22,60%
14Plant 33,70%
15Plant 412,40%
16Plant 56,00%
Sheet1

Map1
EFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJ
1301/01/2201/02/2201/03/2201/04/2201/05/2201/06/2201/07/2201/08/2201/09/2201/10/2201/11/2201/12/2201/13/2201/14/2201/15/2201/16/2201/17/2201/18/2201/19/2201/20/2201/21/2201/22/2201/23/2201/24/2201/25/2201/26/2201/27/2201/28/2201/29/2201/30/2201/31/22
14Plant 10,084
15Plant 20,026
16Plant 30,037
17Plant 40,124
sheet2

defined names used
Map1
FG
13MyDates=sheet2!$F$13:$AL$13
14MyPlants=sheet2!$E$14:$E$18
15MySource=Sheet1!$B$11:$C$16
Sheet1
 
Upvote 0
Solution
amateurish no doubt, but since I just finished playing I might as well post it.
VBA Code:
Sub InsertValues()
Dim strDate As String
Dim i As Integer

Worksheets("Sheet9").Select
strDate = Range("B1")

With Worksheets("Sheet10")
   .Activate
   For i = 1 To .Cells(1, Columns.Count).End(xlToLeft).Column
      If .Cells(1, i) = strDate Then
         Worksheets("Sheet9").Range("B2:B6").Copy .Cells(2, i)
         Exit For
      End If
   Next
End With

End Sub
Assumes data is located and needs to be copied as was shown.
 
Upvote 0
Thanks BSLAV! Your code worked perfectly!

I also want to add that, as a bit of a VBA noob, you explaining the why's & how's behind your code really makes it easier to understand the workings of the code, and helps me learn just that little bit more so I can get to be more independent in writing code. Very much appreciated!
 
Upvote 0
You will need a copy of the code, or a named range for every month sheet? Not all months have 31 days.
 
Upvote 0
Hi Micron. No. The way I ultimately need to report performance is by month. So, each file will be its own month. So, I won't need to keep multiple tabs for more than one month within the same file. Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,885
Messages
6,122,085
Members
449,064
Latest member
MattDRT

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