VBA copy values to month column

jocker_boy

Board Regular
Joined
Feb 5, 2015
Messages
83
Hi,

It may be easy, but i'm a noob in VBA.

I have my month in cell K1.
In row 3 i have all months (jan/21, fev/21. mar/21, etc...)
I have my values in column (K6: lastrow).

I need to copy my values in (K6: last row)
Check the month in K1
and copy those values to the column where is the same month, starting in row 6.
In the example below, i need to copy the values (5,9,4) and copy to the column (O) where the month is a match to my month in K1.

KLMNOP
abr/22
Valuesjan/22fev/22mar/22abr/22mai/22
55
99
44
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
You have:
abr/22
In Range("K1")

But then it looks like you have months in Row 2 not row 3
 
Upvote 0
You have:
abr/22
In Range("K1")

But then it looks like you have months in Row 2 not row 3
It's a mistake. they are in row 3.
I finally did it :)

VBA Code:
Sub CopyDate()

Dim rng1 As Range
Dim strSearch As Date
Dim lr As Long

strSearch = Range("K1").Value

Set rng1 = Rows(3).Find(CDate(strSearch), , xlFormulas, xlWhole)

If Not rng1 Is Nothing Then

'MsgBox ("Contains date")
lr = Range("K" & Rows.Count).End(xlUp).Row
Range(Cells(4, rng1.Column), Cells(lr, rng1.Column)).Value = Range("K4:A" & lr).Value

Else

MsgBox ("Does not contain date")

End If

End Sub
 
Upvote 0
Solution
It's a mistake. they are in row 3.
I finally did it :)

VBA Code:
Sub CopyDate()

Dim rng1 As Range
Dim strSearch As Date
Dim lr As Long

strSearch = Range("K1").Value

Set rng1 = Rows(3).Find(CDate(strSearch), , xlFormulas, xlWhole)

If Not rng1 Is Nothing Then

'MsgBox ("Contains date")
lr = Range("K" & Rows.Count).End(xlUp).Row
Range(Cells(4, rng1.Column), Cells(lr, rng1.Column)).Value = Range("K4:A" & lr).Value

Else

MsgBox ("Does not contain date")

End If

End Sub
Glad to see you have things working.
If you wrote all this code yourself glad to see your learning Vba
 
Upvote 0

Forum statistics

Threads
1,215,453
Messages
6,124,918
Members
449,195
Latest member
Stevenciu

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