使用 Vb.net Vlookup 从 Excel Sheet 填充特定列范围
Populating a specific column range from Excel Sheet using Vb.net Vlookup
我正在尝试使用 vb.Net w/vlookup 公式从我的 excel sheet 中填充一个特定的列范围,但它一直将 1 值返回到单个单元格,我我希望填充列上受影响的所有行。
**已解决,这是我的最终代码:
Dim App As New Excel.Application
Dim workbook As Excel.Workbook
Dim sheet As Excel.Worksheet
Dim exrange As Excel.Range
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Try
Dim fn As String = System.IO.Path.GetFileNameWithoutExtension(filedirectory1) 'Gets the filename
Dim convertedfn = fn.Substring(0, 31) 'Returns a specific string based on the character count from the filename
workbook = App.Workbooks.Open(filedirectory1)
sheet = workbook.Worksheets(convertedfn) '
exrange = sheet.UsedRange
exrange = CType(sheet.Columns("B:B"), Excel.Range) 'selects a specific column range in the excel file
exrange.EntireColumn.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, Excel.XlInsertFormatOrigin.xlFormatFromRightOrBelow) 'Inserts a column from the selected column range above
Dim lastrow As Long
lastrow = sheet.Range("A" & sheet.Rows.Count).End(Excel.XlDirection.xlUp).Row ' To get the row count that has value from a specific column
sheet.Range("B2:B" & lastrow).Formula = "=VLOOKUP(A:A,'[somefilename.xlsx]JULY 2021'!$C:$M,11,0)" 'Vlookup formula
App.DisplayAlerts = False
workbook.Save()
workbook.Close()
App.Quit()
MessageBox.Show("Done")
Catch ex As Exception
MessageBox.Show(ex.ToString)
workbook.Close()
App.Quit()
End Try
End Sub
旧结果:
最终结果:
编辑:最终通过添加此代码中的特定范围使其工作
之前:
'sheet.Range("B2").Formula = "=VLOOKUP(A:A,'[somefilename.xlsx]JULY 2021'!$C:$M,11,0)"'
Post代码:
'sheet.Range("B2:B4").Formula = vlookup formula etc..'
编辑 2:开始工作了。声明了一个变量,该变量计算具有特定列值的单元格的行。
Dim lastrow As Long
lastrow = sheet.Range("A" & sheet.Rows.Count).End(Excel.XlDirection.xlUp).Row ' To get the row count that has value from a specific column
参考:Excel Vlookup with Autofill VBA
已通过将这些添加到现有代码中解决此问题。
Dim lastrow As Long
lastrow = sheet.Range("A" & sheet.Rows.Count).End(Excel.XlDirection.xlUp).Row ' To get the row count that has value from a specific column
这是 fill/populate 具有 vlookup 公式的特定列范围。
sheet.Range("B2:B" & lastrow).Formula = "=VLOOKUP(A:A,'[somefilename.xlsx]JULY 2021'!$C:$M,11,0)" 'Vlookup formula
我正在尝试使用 vb.Net w/vlookup 公式从我的 excel sheet 中填充一个特定的列范围,但它一直将 1 值返回到单个单元格,我我希望填充列上受影响的所有行。
**已解决,这是我的最终代码:
Dim App As New Excel.Application
Dim workbook As Excel.Workbook
Dim sheet As Excel.Worksheet
Dim exrange As Excel.Range
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Try
Dim fn As String = System.IO.Path.GetFileNameWithoutExtension(filedirectory1) 'Gets the filename
Dim convertedfn = fn.Substring(0, 31) 'Returns a specific string based on the character count from the filename
workbook = App.Workbooks.Open(filedirectory1)
sheet = workbook.Worksheets(convertedfn) '
exrange = sheet.UsedRange
exrange = CType(sheet.Columns("B:B"), Excel.Range) 'selects a specific column range in the excel file
exrange.EntireColumn.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, Excel.XlInsertFormatOrigin.xlFormatFromRightOrBelow) 'Inserts a column from the selected column range above
Dim lastrow As Long
lastrow = sheet.Range("A" & sheet.Rows.Count).End(Excel.XlDirection.xlUp).Row ' To get the row count that has value from a specific column
sheet.Range("B2:B" & lastrow).Formula = "=VLOOKUP(A:A,'[somefilename.xlsx]JULY 2021'!$C:$M,11,0)" 'Vlookup formula
App.DisplayAlerts = False
workbook.Save()
workbook.Close()
App.Quit()
MessageBox.Show("Done")
Catch ex As Exception
MessageBox.Show(ex.ToString)
workbook.Close()
App.Quit()
End Try
End Sub
旧结果:
最终结果:
编辑:最终通过添加此代码中的特定范围使其工作 之前:
'sheet.Range("B2").Formula = "=VLOOKUP(A:A,'[somefilename.xlsx]JULY 2021'!$C:$M,11,0)"'
Post代码:
'sheet.Range("B2:B4").Formula = vlookup formula etc..'
编辑 2:开始工作了。声明了一个变量,该变量计算具有特定列值的单元格的行。
Dim lastrow As Long
lastrow = sheet.Range("A" & sheet.Rows.Count).End(Excel.XlDirection.xlUp).Row ' To get the row count that has value from a specific column
参考:Excel Vlookup with Autofill VBA
已通过将这些添加到现有代码中解决此问题。
Dim lastrow As Long
lastrow = sheet.Range("A" & sheet.Rows.Count).End(Excel.XlDirection.xlUp).Row ' To get the row count that has value from a specific column
这是 fill/populate 具有 vlookup 公式的特定列范围。
sheet.Range("B2:B" & lastrow).Formula = "=VLOOKUP(A:A,'[somefilename.xlsx]JULY 2021'!$C:$M,11,0)" 'Vlookup formula