HLOOKUP增加行索引号

时间:2018-03-27 15:43:09

标签: excel excel-vba vlookup lookup lookup-tables vba

目前正致力于从一个程序导出数据并使用VBA将数据导入CRM。

使用Dispatcher,我可以为每行数据合并多个列到“联系人备注”部分。

HLOOKUP公式(如下)可用于合并信息:

HLOOKUP
  • =$AU$3&": "& HLOOKUP($AU$3,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$4&": "& HLOOKUP($AU$4,listing_info,2+AS2,FALSE )& CHAR(10)&$AU$5&": "& TEXT(HLOOKUP($AU$5,listing_info,2+AS2,FALSE ),"mmmm dd, yyyy")& CHAR(10)&$AU$6&": "& TEXT(HLOOKUP($AU$6,listing_info,2+AS2,FALSE),"$0,000")& CHAR(10)&$AU$7&": "& HLOOKUP($AU$7,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$8&": "& TEXT(HLOOKUP($AU$8,listing_info,2+AS2,FALSE ),"mmmm dd, yyyy")& CHAR(10)&$AU$9&": "& TEXT(HLOOKUP($AU$9,listing_info,2+AS2,FALSE ),"mmmm dd, yyyy")& CHAR(10)&$AU$10&": "& TEXT(HLOOKUP($AU$10,listing_info,2+AS2,FALSE),"mmmm dd, yyyy")& CHAR(10)&$AU$11&": "& TEXT(HLOOKUP($AU$11,listing_info,2+AS2,FALSE ),"mmmm dd, yyyy")& CHAR(10)&$AU$12&": "& HLOOKUP($AU$12,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$13&": "&HLOOKUP($AU$13,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$14&": "&HLOOKUP($AU$14,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$15&": "&HLOOKUP($AU$15,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$16&": "&HLOOKUP($AU$16,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$17&": "&HLOOKUP($AU$17,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$18&": "&HLOOKUP($AU$18,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$19&": "&HLOOKUP($AU$19,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$20&": "&HLOOKUP($AU$20,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$21&": "&HLOOKUP($AU$21,listing_info,2+AS2,FALSE)& CHAR(10)&$AU$22&": "&HLOOKUP($AU$22,listing_info,2+AS2,FALSE) 的索引行增加1并从零开始。
  • Column AS是存储Column AT公式的地方。
  • HLOOKUP是存储查找值的位置。

记录Column AU的宏会产生以下代码:

HLOOKUP

如何使用 ActiveCell.FormulaR1C1 = "=HLOOKUP(RC[-2],listing_info,RC[-4],FALSE)" 代码填充列增加行索引号?

1 个答案:

答案 0 :(得分:0)

我现在警告你,这很丑陋而且有效。 现在我看看这个,我不相信我需要将查找值添加到电子表格中。我本可以在代码中完成这个。

我还将我的工作范围命名为" listing_info"。 此步骤非常重要。

我仍在努力寻找超过50条记录的数据解决方案,但现在这样做。

vector<vector<int > >selectedEdgesLocal; 
int edgeCount=0;                            
for(int i = 0; i < rows; ++i)
    for(int j = 0; j < nVertex; ++j)
        if (some conditions)
        {
            vector<int> tempEdge;
            tempEdge.push_back(displs[my_rank]+i);
            tempEdge.push_back(j);
            selectedEdgesLocal.push_back(tempEdge);
            edgeCount++;
         }
int NumdgesToAdd;
MPI_Reduce(&edgeCount, &NumdgesToAdd, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);

vector<vector<int > > allSelectedEdges(NumdgesToAdd);

int rcounts[comm_size];
int rdisp[comm_size];
int sumE=0;
for(int i=0; i<comm_size; ++i) {
    rcounts[i] = edgeCount;
    rdisp[i]=sumE;
    sumE+=edgeCount;
}

MPI_Gatherv(&selectedEdgesLocal.front(), rcounts[my_rank], MPI_INT, &allSelectedEdges.front(), rcounts, rdisp, MPI_INT, 0, MPI_COMM_WORLD);

更新1:我可以通过将字符串传递给公式而不是让代码类型为公式来缩短此代码。这将减少处理该项目代码的时间,并且可以节省大量数据传输的时间。

此问题显示如何将字符串传递给`Sub HLookup_Process_Data_To_New_CRM() 'This is where I Input the increasing row index numbers Range("AS2").Select ActiveCell.FormulaR1C1 = "0" Range("AS3").Select ActiveCell.FormulaR1C1 = "1" Range("AS4").Select ActiveCell.FormulaR1C1 = "2" Range("AS2:AS4").Select Selection.AutoFill Destination:=Range("AS2:AS51"), Type:=xlFillDefault 'Adding my Lookup Values Range("AS2:AS51").Select Range("AU2").Select ActiveCell.FormulaR1C1 = "Address" Range("AU3").Select ActiveCell.FormulaR1C1 = "Address 2" Range("AU4").Select ActiveCell.FormulaR1C1 = "List Date" Range("AU5").Select ActiveCell.FormulaR1C1 = "List Price" Range("AU6").Select ActiveCell.FormulaR1C1 = "Days On Market" Range("AU7").Select ActiveCell.FormulaR1C1 = "Lead Date" Range("AU8").Select ActiveCell.FormulaR1C1 = "Expired Date" Range("AU9").Select ActiveCell.FormulaR1C1 = "Withdrawn Date" Range("AU10").Select ActiveCell.FormulaR1C1 = "Status Date" Range("AU11").Select ActiveCell.FormulaR1C1 = "Listing Agent" Range("AU12").Select ActiveCell.FormulaR1C1 = "Listing Broker" Range("AU13").Select ActiveCell.FormulaR1C1 = "MLS/FSBO ID" Range("AU14").Select ActiveCell.FormulaR1C1 = "Bedrooms" Range("AU15").Select ActiveCell.FormulaR1C1 = "Bathrooms" Range("AU16").Select ActiveCell.FormulaR1C1 = "Type" Range("AU17").Select ActiveCell.FormulaR1C1 = "Square Footage" Range("AU18").Select ActiveCell.FormulaR1C1 = "Year Built" Range("AU19").Select ActiveCell.FormulaR1C1 = "Remarks" Range("AU20").Select ActiveCell.FormulaR1C1 = "Notes" Range("AU21").Select ActiveCell.FormulaR1C1 = "Folders" 'My original formula exceeded the VBA length and I divide it into six pieces and use Concat to glue the pieces back together. Range("AW2").Select ActiveCell.FormulaR1C1 = _ "=R2C47&"": ""&HLOOKUP(R2C47,listing_info,2+RC[-4],FALSE )&CHAR(10)&R3C47&"": ""&HLOOKUP(R3C47,listing_info,2+RC[-4],FALSE )&CHAR(10)&R4C47&"": ""&TEXT(HLOOKUP(R4C47,listing_info,2+RC[-4],FALSE ),""mmmm dd, yyyy"")" Range("AX2").Select ActiveCell.FormulaR1C1 = _ "=CHAR(10)&R5C47&"": ""&TEXT(HLOOKUP(R5C47,listing_info,2+RC[-5],FALSE ),""$0,000"")&CHAR(10)&R6C47&"": ""&HLOOKUP(R6C47,listing_info,2+RC[-5],FALSE )&CHAR(10)&R7C47&"": ""&TEXT(HLOOKUP(R7C47,listing_info,2+RC[-5],FALSE ),""mmmm dd, yyyy"")" Range("AY2").Select ActiveCell.FormulaR1C1 = _ "=CHAR(10)&R8C47&"": ""&TEXT(HLOOKUP(R8C47,listing_info,2+RC[-6],FALSE ),""mmmm dd, yyyy"")&CHAR(10)&R9C47&"": ""&TEXT(HLOOKUP(R9C47,listing_info,2+RC[-6],FALSE ),""mmmm dd, yyyy"")&CHAR(10)&R10C47&"": ""&TEXT(HLOOKUP(R10C47,listing_info,2+RC[-6],FALSE ),""mmmm dd, yyyy"")" Range("AZ2").Select ActiveCell.FormulaR1C1 = _ "=CHAR(10)&R11C47&"": ""&HLOOKUP(R11C47,listing_info,2+RC[-7],FALSE )&CHAR(10)&R12C47&"": ""&HLOOKUP(R12C47,listing_info,2+RC[-7],FALSE )&CHAR(10)&R13C47&"": ""&HLOOKUP(R13C47,listing_info,2+RC[-7],FALSE )&CHAR(10)&R14C47&"": ""&HLOOKUP(R14C47,listing_info,2+RC[-7],FALSE )" Range("BA2").Select ActiveCell.FormulaR1C1 = _ "=CHAR(10)&R15C47&"": ""&HLOOKUP(R15C47,listing_info,2+RC[-8],FALSE )&CHAR(10)&R16C47&"": ""&HLOOKUP(R16C47,listing_info,2+RC[-8],FALSE )&CHAR(10)&R17C47&"": ""&HLOOKUP(R17C47,listing_info,2+RC[-8],FALSE )&CHAR(10)&R18C47&"": ""&HLOOKUP(R18C47,listing_info,2+RC[-8],FALSE )" Range("BB2").Select ActiveCell.FormulaR1C1 = _ "=CHAR(10)&R19C47&"": ""&HLOOKUP(R19C47,listing_info,2+RC[-8],FALSE )&CHAR(10)&R20C47&"": ""&HLOOKUP(R20C47,listing_info,2+RC[-8],FALSE )&CHAR(10)&R21C47&"": ""&HLOOKUP(R21C47,listing_info,2+RC[-8],FALSE)" Range("BC1").FormulaR1C1 = "Contact Notes" Range("BC2").Select ActiveCell.FormulaR1C1 = "=CONCAT(RC[-6]:RC[-1])" 'Auto fill down a predetermined distance. I will only add 50 records at a time. I still need to figure out how to do this with an unlimited number of records. Range("AW2:BC2").Select Selection.AutoFill Destination:=Range("AW2:BC51"), Type:=xlFillDefault 'Copying and Pasting only the values Range("BC2:BC51").Select Selection.Copy Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone 'Deleting everything I just did Columns("AS:BB").Select Selection.Delete Shift:=xlToLeft End Sub

Pass variables into VLOOKUP in VBA

由于此代码需要HLOOKUP作为命名范围,因此我使用以下链接创建以下代码:

  1. 命名"listing_info"(Andrew Poulsom&#39;解决方案) https://www.mrexcel.com/forum/excel-questions/51686-vba-code-name-worksheet.html

  2. 查找最后一行和一列(Siddharth Rout&#39;答案) How can I find last row that contains data in the Excel sheet with a macro?

  3. 使用活动区域命名范围(我修改了示例#4) https://powerspreadsheets.com/vba-create-named-range/