简体   繁体   English

Excel VBA - =在宏中查找

[英]Excel VBA - =lookup in macro

I'm using this formula to get info from another sheet. 我正在使用这个公式从另一张纸上获取信息。 I would like to make it in a macro. 我想在宏观中做到这一点。

=LOOKUP(Sheet1!F2,{"FL01","FL12","FL21","FL31","FL34"},{"ORLANDO PICKLIST","TAMPA PICKLIST","JAX PICKLIST","NAPLES PICKLIST","MIAMI PICKLIST"})

Also the answer to be in sheet2 B2 center font size 24 . 答案是在sheet2 B2中心字体大小24。

Thank you 谢谢

By recording it 通过录制

在此输入图像描述

you will get something like this: 你会得到这样的东西:

Option Explicit

Sub Macro3()
'
' Macro3 Macro
'

'
    Selection.FormulaArray = _
        "=LOOKUP(Sheet1!RC[4],{""FL01"";""FL12"";""FL21"";""FL31"";""FL34""},{""ORLANDO PICKLIST"";""TAMPA PICKLIST"";""JAX PICKLIST"";""NAPLES PICKLIST"";""MIAMI PICKLIST""})"
    With Selection.Font
        .Name = "Calibri"
        .Size = 24
    End With
    With Selection
        .HorizontalAlignment = xlGeneral
    End With
End Sub

Where selection is which cell I selected when I started recording (" B2 "). 选择哪个是我开始录制时选择的单元格(“ B2 ”)。 From here you can play around with the code. 从这里你可以玩代码。 You will get a feeling for how Excel "Translate" your formulas and your format options into code. 您将了解Excel如何将您的公式和格式选项“翻译”到代码中。

By edit the selection part ( Selection. ) we can be more explicit which worksheet and which range we want to apply the new code into... 通过编辑选择部分( Selection. ),我们可以更明确哪个工作表以及我们想要将新代码应用到哪个范围...

Sub Macro3_Changes()
'
' Macro3 Macro
'

'
    Worksheets("Sheet2").Range("B2").FormulaArray = _
        "=LOOKUP(Sheet1!RC[4],{""FL01"";""FL12"";""FL21"";""FL31"";""FL34""},{""ORLANDO PICKLIST"";""TAMPA PICKLIST"";""JAX PICKLIST"";""NAPLES PICKLIST"";""MIAMI PICKLIST""})"
    With Selection.Font
        .Name = "Calibri"
        .Size = 24
    End With
    With Selection
        .HorizontalAlignment = xlGeneral
    End With
End Sub

Here is some interesting note, I assume that you had an Array formula. 这是一些有趣的注释,我假设你有一个数组公式。 I enter your formula with Ctrl + Shift + Enter. 我用Ctrl + Shift + Enter输入你的公式。 The macro records that too and translate it as FormulaArray . 宏也记录了它并将其翻译为FormulaArray If I have just enter the formula it would have been FormulaR1C1 which is a relative formula from the section you selected. 如果我刚刚输入公式,那么它将是FormulaR1C1 ,它是您选择的部分的相对公式。

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

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