动态范围在两个工作簿中

时间:2015-09-08 21:09:11

标签: excel vba excel-vba

我正在尝试将主工作表中的列与通过请求框打开的工作表中的列进行比较。我希望范围与我目前在代码中的动态相比是动态的。在主表单中,我希望它在单元格A2中查看列A并转到最后一个条目,在打开的表单中,它应该从单元格C2开始查看列E并转到最后一个条目。我目前使用的代码如下:

Sub InspectionCheck()

 Dim colI_Cell As Range
 Dim colI_Range As Range
 Dim rngLookupRange As Range
 Dim rngFound As Range
 Dim rngInspected As Range
 Dim FileName As Variant
 Dim wb As Workbook

Set colI_Range = ActiveSheet.Range("A2:A350").Cells
FileName = Application.GetOpenFilename(filefilter:="Excel Files(*.xlsx),*.xlsx")
Set wb = Workbooks.Open(FileName)

Set rngLookupRange = wb.Worksheets("owssvr").Range("E2:E350")
ThisWorkbook.Activate

For Each colI_Cell In colI_Range
With rngLookupRange
  Set rngFound = .Find(What:=colI_Cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
  If Not rngFound Is Nothing Then
   colI_Cell.Offset(0, 1) = "Yes"
  Else: colI_Cell.Offset(0, 1) = "No"

  End If
End With
Next


Set rngLookupRange = Nothing
wb.Close False
Set wb = Nothing
Set colI_Range = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

如果您使用以下模块代码查找非空白的最后一行,则可以轻松完成此操作:

Sub InspectionCheck()

Dim colI_Cell As Range
Dim colI_Range As Range
Dim rngLookupRange As Range
Dim rngFound As Range
Dim rngInspected As Range
Dim FileName As Variant
Dim wb As Workbook
Dim i as Long, rngStr as String

i = FindLastRowNotBlank(ActiveSheet, 2,1) 'I have added this
rngStr = "A2:A" & i 'I have added this
Set colI_Range = ActiveSheet.Range(rngStr).Cells 'I have modified this
FileName = Application.GetOpenFilename(filefilter:="Excel Files(*.xlsx),*.xlsx")

Set wb = Workbooks.Open(FileName)

i = FindLastRowNotBlank(wb.Worksheets("yoursheetnamehere"), 2,5) 'I have added this you need to fill in the worksheet name
rngStr = "E2:E" & i 'I have added this
Set rngLookupRange = wb.Worksheets("owssvr").Range(rngStr) ' I have modified this
ThisWorkbook.Activate

For Each colI_Cell In colI_Range
With rngLookupRange
  Set rngFound = .Find(What:=colI_Cell.Value, LookIn:=xlValues, LookAt:=xlWhole)
If Not rngFound Is Nothing Then
    colI_Cell.Offset(0, 1) = "Yes"
Else: colI_Cell.Offset(0, 1) = "No"

End If
End With
Next


Set rngLookupRange = Nothing
wb.Close False
Set wb = Nothing
Set colI_Range = Nothing

End Sub

然后您可以使用代码中获得的数字,如下所示:

C=mmx('mul',A,B);

让我知道它是如何运作的。谢谢, 奇