vba 使用 vlookup 保持文本格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25359312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:16:42  来源:igfitidea点击:

Keep textformatting with vlookup

excelexcel-vbavba

提问by Gmorken

Is there a way to use vlookup with excel and keep formatting of text?

有没有办法在 excel 中使用 vlookup 并保持文本格式?

In this case some rows that I want to copy with vlookup to another sheet is formatted with different colors. I want to keep these colors.

在这种情况下,我想用 vlookup 复制到另一个工作表的一些行被格式化为不同的颜色。我想保留这些颜色。

采纳答案by lowak

As Rory pointed in comment, it is not possible to achieve it with VLOOKUP. However you can use .Findand .Copymethod and probably you will receive the effect you need. Below I posted sample code. You can also use copy/paste depending on your needs.

正如 Rory 在评论中指出的那样,使用 VLOOKUP 无法实现它。但是,您可以使用.Find.Copy方法,并且可能会收到您需要的效果。下面我发布了示例代码。您还可以根据需要使用复制/粘贴。

Sub paste_formats()

Dim Find As String

On Error GoTo blad:
Find = Range("C3:C19").Find(Range("a1").Value).Address

Range(Find).Copy Destination:=Range("B2")

Exit Sub
blad:
If Err.Number = 91 Then: MsgBox ("Value was not found.") 'if value was not found it will pop up a message.

End Sub

If you want to copy rows then you just need to change a code a bit:

如果要复制行,则只需要稍微更改代码即可:

Find = Range("C3:C19").Find(Range("a1").Value).Row

Rows(Find & ":" & Find).Copy Destination:=Rows(23)

In case you want to only copy formatting you should use copy / pastespecial xlpasteformats.

如果您只想复制格式,您应该使用 copy / pastespecial xlpasteformats。