使用VBA使用用户select的文件在单元格中inputvlookup函数

我正在尝试构build一个将公式input到单元格中的子对象,并将该对象的公式放入lastrow中,然后复制公式并pastespecial->values整个范围的pastespecial->values 。 我在vLookup使用的表格位于单独的文件中,并不总是存储在相同的位置。 表总是格式相同,但表大小并不总是相同的。

我必须在4个不同的工作表上做这个,我必须input这个公式的列有一个“Order Grade”的标题。 我使用.Find返回“Order Grade”的位置。 然后我想进入我find“Order Grade”下面的Vlookup 1行。

如果我在工作表上手动input公式,它看起来像这样:

 =VLOOKUP(C2,[newpipe.xlsx]Sheet1!$A$1:$B$376,2,FALSE) 

VBA中 ,我想要构build的公式看起来像这样:

 =vlookup(RC[-1],stringFileName\[newpipe.xlsx]Sheet1!$A$1:LastColumn & LastRow,2,False 

用户使用打开的文件对话框selectstringFileName。 所选工作表上的LastColumn和LastRow应该由macros来计算。

这是我迄今为止。

 Private Function UseFileDialogOpen() Dim myString As String ' Open the file dialog With Application.FileDialog(msoFileDialogFilePicker) .AllowMultiSelect = False .Show If .SelectedItems.Count = 1 Then myString = .SelectedItems(1) 'MsgBox myString UseFileDialogOpen = myString Else MsgBox ("Failed to properly open file") myString = "fail" UseFileDialogOpen = myString End If End With End Function Sub formatOrderColumn() Dim strSearch Dim foundColumn Dim foundRow Dim RowBelowSpotFound Dim fileLocation strSearch = "Order Grade" Set aCell = ActiveSheet.Rows(1).Find(what:=strSearch, LookIn:=xlValues, _ Lookat:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _ MatchCase:=True, SearchFormat:=False) If Not aCell Is Nothing Then foundColumn = aCell.Column foundRow = aCell.Row spotFound = ColumnLetter(foundColumn) & foundRow + 1 ' MsgBox "Value Found in Row " & foundRow & _ " and the Column Number is " & foundColumn Else Exit Sub End If fileLocation = UseFileDialogOpen() LastColumn = FindLastColumn(UserSelectedSheet) LastRow = FindLastRow(UserSelectedSheet) Range(RowBelowSpotFound).Formula = _ "=vlookup(RC[-1], [" & fileLocation & "]Sheet1!$A$1:" & LastColumn & lastrow & ",2,False" End Sub 

我不知道如何从用户select的文件中获得lastrow和lastColumn。 我有传递给他们的任何工作表的function。 我意识到我做了一个很糟糕的工作来解释我的情况,我一点也不确定我正在做这个最好的方法。 如果您有任何问题,请告诉我,我会尽力澄清。 我很快就要离开办公室了,直到早上才能回复。

这是新的公式。 当我尝试将偏移单元格公式设置为string值时,最后一行出现错误。 string值是正确的。 如果我尝试直接设置单元格值而不使用mystring持有者来首先构buildstring,则会得到相同的错误。 “应用程序或对象定义的错误”

 Sub vlookupOrderGrade() Dim strSearch Dim fileLocation Dim aCell As Range Dim aCellString Dim myString As String strSearch = "Order Grade" Set aCell = ActiveSheet.Rows(1).Find(what:=strSearch, LookIn:=xlValues, _ Lookat:=xlWhole, MatchCase:=True) If Not aCell Is Nothing Then fileLocation = UseFileDialogOpen() If fileLocation <> "fail" Then 'replace last "\" with a "[" fileLocation = StrReverse(fileLocation) fileLocation = Replace(fileLocation, "\", "[", 1, 1) fileLocation = StrReverse(fileLocation) 'build string myString = "=vlookup(" & _ ColumnLetter(aCell.Column - 1) & aCell.Row + 1 & _ ", '" & fileLocation & "]Sheet1'!$A:$B,2,False" MsgBox (myString) 'set cell to string aCell.Offset(1, 0).Formula = myString End If Else Exit Sub End If End Sub 

未经testing:

 Sub formatOrderColumn() Dim strSearch Dim fileLocation strSearch = "Order Grade" Set aCell = ActiveSheet.Rows(1).Find(what:=strSearch, LookIn:=xlValues, _ Lookat:=xlWhole, MatchCase:=True) If Not aCell Is Nothing Then fileLocation = UseFileDialogOpen() If fileLocation <> "fail" Then aCell.Offset(1, 0).Formula = "=vlookup(" & _ aCell.Offset(1, -1).Address(False, False) & _ ", '[" & fileLocation & "]Sheet1'!$A:$B,2,False" End If Else Exit Sub End If End Sub