VBA to find Correct Column and Copy/Paste Values

moony

New Member
Joined
Jul 22, 2019
Messages
20
Hello,

I am working on a sheet called Monthly that automatically pulls the relevant data from the ABR Drop In sheet. The cells confirm the month referenced above in cell H5 matches the date inputted in the ABR Drop In sheet in cell H5. The Monthly sheet has the dates listed in Monthly!$F$5:$EH$5. Only the blue cells are inputs, and the goal is to have a button on the ABR Drop In sheet that will look at the date in cell 'ABR Drop In'!H5, find that column in the Monthly sheet, and copy/paste all the blue input cells as values.

Appreciate any help I can get!

Thanks!
Hotel AM Tracker v1 - test.xlsx
BCDEFGH
5Input Month Ending Date of Statements Below:12/31/2021
ABR Drop In
Hotel AM Tracker v1 - test.xlsx
H
5Dec 2021
62
71
8
949
10
11
12
131,519
14458
15351
16107
1730.2%
18990.66
191,240.21
20396.58
21298.70
22
23--
24--
25
26
27--
28--
29
30
31--
32
33--
34--
35
36
37--
38--
39
40
41--
42
431,519
44458
45351
46107
4730.2%
48990.66
49
50
51298.70
52
53
54453,721
55
56
57215,575
58116,669
598,941
6025,887
61971
6263,107
63103,983
6499,743
652,153
661,800
67287
68
6915,602
7012,600
71
7246,337
731
74--
7536,316
76424
779,596
78
79847,818
80
81
82242,656
83
84
85422,221
8628,207
87
88
8981,249
90--
91--
9273,960
937,289
940
95
96774,333
97
9873,485
99
100
101196,141
10244,272
103166,620
10466,466
10528,330
106
107501,830
108
109(428,345)
110
11121,195
112
113
114(449,540)
115
116
117(78,884)
11812,617
1195,070
120
121
122
123
124(61,197)
125
126(388,343)
127
12816,927
129
130(405,270)
Monthly
 
Last edited by a moderator:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

For some reason your workbook is not readable under my Excel version but someone else may help you …​
 
Upvote 0
Can anyone else help with the below? I'm not able to set the rng correctly as it returns Nothing.

VBA Code:
Sub PasteMonthlyPL()
 
 Application.ScreenUpdating = False
 Dim wb As Workbook
 Dim s1 As Worksheet
 Dim s2 As Worksheet
 Dim rng As Range
 Dim MonthlyPL As Date
 Dim colnumber As Long
 
 Set wb = ThisWorkbook
 Set s1 = wb.Worksheets("ABR Drop In")
 Set s2 = wb.Worksheets("Monthly")
 
 MonthlyPL = s1.Cells(5, 8)
 
 With s2
    Set rng = .Rows(5).Find(What:=MonthlyPL, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
 End With
 
 If rng Is Nothing Then
    MsgBox "Date Not Found"
 Else
    colnumber = rng.Column
    
    s2.Cells(9, colnumber).Copy
    s2.Cells(9, colnumber).PasteSpecial Paste:=xlPasteValues
 
 End If
  

Application.ScreenUpdating = True
End Sub
 
Upvote 0
made more changes and i think i got it to work. any chance it could be more efficient?

VBA Code:
Sub PasteMonthlyPL()
 
 Application.ScreenUpdating = False
 Dim wb As Workbook
 Dim s1 As Worksheet
 Dim s2 As Worksheet
 Dim rng As Range
 Dim MonthlyPL As Double
 Dim colnumber As Long
 Dim LastRow As Integer
 Dim LastColumn As Integer
 Dim LastCell
 
 Set wb = ThisWorkbook
 Set s1 = wb.Worksheets("ABR Drop In")
 Set s2 = wb.Worksheets("Monthly")
 Set LastCell = s2.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious)
 LastRow = LastCell.Row
 LastColumn = LastCell.Column
 
 MonthlyPL = s1.Cells(5, 8).Value

 For Each cell In s2.Range("F5", Cells(5, LastColumn)).Cells
    If cell = MonthlyPL Then
        colnumber = cell.Column
        Exit For
    End If
 Next
 
 If colnumber = 0 Then
    MsgBox "Date Not Found"
 Else
    
    For Each cell In s2.Range(Cells(1, colnumber), Cells(LastRow, colnumber)).Cells
        If cell.HasFormula = True And cell.Font.Color = RGB(0, 0, 255) Then
            cell.Value = cell.Value
        End If
    Next
    
 End If

Application.ScreenUpdating = True
End Sub
 
Upvote 0
any chance it could be more efficient?
Yes obviously removing the useless and using elements of my previous demonstration !​
According to the Macro Recorder just with a filter Excel basics my VBA demonstration revamped :​
VBA Code:
Sub Demo1r()
     Dim V
    With Sheets("Monthly").UsedRange.Columns
         V = Application.Match(['ABR Drop In'!H5], .Rows(5 - .Row + 1), 0):  If IsError(V) Then Beep: Exit Sub
        .Item(V).AutoFilter 1, 16711680, 9
         If Application.Subtotal(103, .Item(V)) > 1 Then With .Item(V).Offset(1).SpecialCells(12): .Formula = .Value2: End With
        .AutoFilter
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,861
Messages
6,121,971
Members
449,059
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