简体   繁体   中英

How to use VBA to apply Auto Filter per month/year and add a value in another range based on the month/ Year filter

How can I use VBA to apply a filter in a column (per month/Yer) so the range corresponding to that date will receive value as Month-Yes in another column?

Or in this case, would be better to use if the value in the column named CreationDate is = Jan/2020 then range on column Month_Yr will be 01_2020.

Basic the VBA need to check dates on column A and if it writes only the month and year on column B.

Should I use the auto filter or Vlook? Any ideas how can I get the result I need?

Following your comments, you can use

With Sheet1.Range("B2:B" & last_row)
    .Formula = "=TEXT(A2,""m\_yyyy"")"
    ' I suggest you then delete the formulae
    .Value = .Value
End with

Note the way to add double quotes inside a string is to use 2 of them (ie escape it with another double quote), therefore the string "=TEXT(A2,""m\_yyyy"")" will result in the formula =TEXT(A2,"m\_yyyy").

Also note the underscore character has a special meaning in excel number formatting and therefore, if you want it displayed in the final string then you need to escape it with a backslash. (I know it looked like a typo in my comment above, but it is important)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM