简体   繁体   English

如何从 VLookup 返回值中保留日期格式?

[英]How can I keep the date format from a VLookup returned value?

I have a sheet whit mixed format data (dates, double, strings, etc.).我有一张混合格式数据(日期、双精度、字符串等)的工作表。 I search the value (my_index) in the lookup_range and I want to retrieve the data to change with it a cell in other sheet.我在 lookup_range 中搜索值 (my_index),我想检索数据以更改其他工作表中的单元格。 It works fine, but when the value returned by VLookup is a date and I set it to the other sheet it looses its date format.它工作正常,但是当 VLookup 返回的值是一个日期并且我将它设置为另一个工作表时,它会丢失其日期格式。

Dim lookup_range As Range
Dim my_index, my_value As Variant

my_value = Application.VLookup(my_index, lookup_range, num_col, False)
Sheets(3).Cells(num_row, last_col_s1 + num_col - 1).Value = my_value

So, when the data in the lookup_range is 02/05/2014 the data showed at sheet-3 looks like 41761 .因此,当 lookup_range 中的数据为02/05/2014 , sheet-3 中显示的数据看起来像41761

I need to keep the original data format of the data in the lookup_range.我需要在lookup_range中保留数据的原始数据格式。

VLOOKUP doesn't care about the formatting of the data it's returning - it returns data , not formats . VLOOKUP不关心它返回的数据的格式——它返回数据,而不是格式

The only way to ensure the same format as the source, is to copy/paste that format, either manually or programmatically.确保与源格式相同的唯一方法是手动或以编程方式复制/粘贴该格式。

用这个

Sheets(3).Cells(num_row, last_col_s1 + num_col - 1).Value = cdate(my_value)

I had the same problem, and simply reformatted the VLOOKUP cell to also be in date-time format.我遇到了同样的问题,只是将 VLOOKUP 单元格重新格式化为日期时间格式。 That fixed my issue.那解决了我的问题。 I am not sure how date-time gets translated into a number, but it does.我不确定日期时间如何转换为数字,但确实如此。

i faced the same issue.我遇到了同样的问题。 After vlookup, change the format to "ShortDate" format. vlookup 后,将格式更改为“ShortDate”格式。 This will give you what you are looking for.这会给你你正在寻找的东西。

Excel Formula : Excel公式:

=TEXT(VLOOKUP(Lookup_value,table_array,col_index_num,0),"dd-mm-yyyy")

VBA : VBA :

Sub vlookup_code()

    Dim table_rng As Range

    'FOR THE TIME BEING GIVING A LOOKUP VALUE
    look_up_value = "sekar"

    'SETTING THE TABLE ARRAY RANGE
    Set table_rng = Sheet1.Range("C3").CurrentRegion

    'SINCE VLOOKUP WILL GIVE AN ERROR, IF THE LOOKUP VALUE IS NOT PRESENT
    'TO HANDLE THIS ERROR, WE USE THE ISERROR STATEMENT
    If IsError(Application.vlookup(look_up_value, table_rng, 2, 0)) = False Then

        vlook_value = WorksheetFunction.vlookup(look_up_value, table_rng, 2, False)

        'CHANGING THE VLOOKUP VALUE FORMAT TO DATE FORMAT
        date_format = WorksheetFunction.Text(vlook_value, "dd-mm-yyyy")

    End If

End Sub

In Microsoft Office 2016.在 Microsoft Office 2016 中。

Once data copied to the cell.一旦数据复制到单元格。 Select the column which needs to be in date format.选择需要为日期格式的列。 Right click->format cells, go to Number tab and select date from the drop-down and choose the required date format, data will be converted to date format.右键单击-> 格式化单元格,转到数字选项卡并从下拉列表中选择日期并选择所需的日期格式,数据将转换为日期格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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