【问题标题】:vba to search cell values in another workbook's columnvba 在另一个工作簿的列中搜索单元格值
【发布时间】:2018-04-17 21:11:08
【问题描述】:

我在 workbook1 中有一个“F”列,其中包含一些值(在使用一些 excel 公式从其​​他列中提取和连接后获得),例如 废话-rd1 废话-rd5 废话rd6 blah-rd48do 我想这样做 废话-rd100 等等

我在 workbook2 中有另一列“D”,其中包含如下值 rndm-blah-rd1_sgjgs hjdf-blah-rd5_cnnv sdfhjdf-blah-rd100_cfdnnv 等等

基本上 "Blah-rdxx" 总是与 workbook2 的 D 列中的其他字符串一起出现

现在,我想做的是—— 如果 workbook2 的 D 列中的值包含 workbook1 的 F 列的值 那么 将workbook2的S列对应的值复制到workbook1的H列(第5列)

这是我到目前为止所达到的地方,但它没有复制任何东西,可能因为存在一些问题并且外循环没有迭代,我尝试遵循解决方案 Nested For Next Loops: Outer loop not iterating 并添加了 n 计数器,但外循环仍然没有迭代 -

Sub findandcopy()
Dim r As Range
Dim f As Range

Dim i As Long
Dim j As Long
Dim w1 As Worksheet
Dim w2 As Worksheet
Dim n As Integer

Set w1 = Workbooks("Book1.xlsm").Worksheets("sheet1")
Set w2 = Workbooks("Book2.xlsx").Worksheets("sheet1")


n = 0
For i = 1 To w2.Cells(Rows.Count, 1).End(xlUp).Row
For j = 1 To w1.Cells(Rows.Count, 1).End(xlUp).Row + n

If InStr(1, w2.Cells(i, 1).Value, w1.Cells(j, 3).Value) > 0 Then

w2.Cells(i, 2).Copy (w2.Cells(j, 5))
Exit For
n = n + 1
End If

Next j
Next i

End Sub

【问题讨论】:

  • 尝试定义工作簿而不是工作表,例如 Dim wb1, wb2 作为工作簿而不是 w1, w2 作为工作表;然后将其引用为 wb1.sheets(“sheet 1”).etc 和 wb2.sheets(“sheet 1”).etc

标签: vba excel


【解决方案1】:

试试这个


Option Explicit

Public Sub FindAndCopy()

    Const F = "F"
    Const D = "D"
    Const H = 2
    Const S = 15

    Dim ws1 As Worksheet:   Set ws1 = Workbooks("Book1.xlsm").Worksheets("Sheet1")
    Dim ws2 As Worksheet:   Set ws2 = Workbooks("Book2.xlsm").Worksheets("Sheet1")
    Dim lr1 As Long:        lr1 = ws1.Cells(ws1.Rows.Count, F).End(xlUp).Row
    Dim lr2 As Long:        lr2 = ws2.Cells(ws2.Rows.Count, D).End(xlUp).Row

    Dim itm1 As Range, itm2 As Range

    Application.ScreenUpdating = False
    For Each itm2 In ws2.Range(ws2.Cells(1, D), ws2.Cells(lr2, D))      'Book2
        For Each itm1 In ws1.Range(ws1.Cells(1, F), ws1.Cells(lr1, F))  'Book1
            If Not IsError(itm1) And Not IsError(itm2) Then
                If InStr(1, itm2.Value2, itm1.Value2) > 0 Then
                    itm1.Offset(, H).Formula = itm2.Offset(, S).Formula 'Book1.H = Book2.S
                    Exit For
                End If
            End If
        Next
    Next
    Application.ScreenUpdating = True
End Sub

原始代码,对功能问题的解释:


Sub findandcopy()
 Dim w1 As Worksheet, w2 As Worksheet
 Dim i As Long, j As Long, n As Integer

 Set w1 = Workbooks("Book1.xlsm").Worksheets("sheet1")
 Set w2 = Workbooks("Book2.xlsx").Worksheets("sheet1")

 n = 0
 For i = 1 To w2.Cells(Rows.Count, 1).End(xlUp).Row       'for each used cell in w2.colA
   For j = 1 To w1.Cells(Rows.Count, 1).End(xlUp).Row + n 'for each used cell in w1.colA

    'Find the text from w1.colC (current w1 row), within cell in w2.colA (current w2 row)
     If InStr(1, w2.Cells(i, 1).Value, w1.Cells(j, 3).Value) > 0 Then

      'If found then copy cell in w2.colB into cell in w2.colE (current w2 row)
       w2.Cells(i, 2).Copy (w2.Cells(i, 5))

       Exit For    'this exits the inner For loop

       n = n + 1   'this would jump over the next cell(s) in w1, but never executes
     End If
   Next j
 Next i
End Sub

  • 缺少缩进让人难以理解
  • 有未使用的变量(r、f),w1 / w2 名称可以表示 Workbook 或 Worksheet
  • 应在每个模块的顶部使用“Option Explicit”
  • 代码不处理有错误的单元格
    • #N/A, #VALUE!, #REF!, #DIV/0!, #NUM!, #NAME?, or #NULL!

如果您想更详细地查看代码,一旦修复,您可以将其发布到 Code Review

【讨论】:

  • 谢谢,但你能解释一下我的代码有什么问题吗?
  • 我添加了你的代码,cmets 解释了它在做什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-23
相关资源
最近更新 更多