使用VBA查找内容:=检索工作表中的值

时间:2017-08-01 13:37:38

标签: excel vba excel-vba

首先感谢大家,你们通过我真正天真的VBA代码帮助我成功。随着我的进步,我遇到了新的问题。

我的挑战是我正在使用此代码在所有打开的工作簿中查找值。如果找到,则执行剩余的代码。我希望我的最终用户可以轻松更改找到的值,而无需他们进入代码。因此,我已向Data Specs添加了一个名为A2的新工作表。我希望当我的代码开始找到某些东西时,它应该在单元格Sheets("Add File Here").Select If IsEmpty(Range("A1")) Then Worksheets("Master Mapper").Activate Dim answer003 As Integer answer003 = MsgBox("Please check the Data Sheet. No value found in first row! Do you wish to find Cvent003 file in open workbooks and start process?", vbYesNo + vbQuestion, "Review & Proceed") If answer003 = vbYes Then 'Starts here Dim wSheet As Worksheet Dim wBook As Workbook Dim rFound As Range Dim bFound As Boolean Dim lngLastRow2 As Long Dim Cvent003Val As Long Cvent003Val = ThisWorkbook.Sheets("Data Specs").Cells("A2").Value On Error Resume Next For Each wBook In Application.Workbooks For Each wSheet In wBook.Worksheets Set rFound = Nothing Set rFound = wSheet.Range("D1:D2").Find(What:=Cvent003Val, SearchFormat:=True, After:=wSheet.Range("D1"), _ LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, MatchCase:=True) 'rFound.Cells.Select If Not rFound Is Nothing Then bFound = True Application.Goto rFound, True 'Rows(1, 2).EntireRow.Hidden = True lngLastRow2 = Cells(Cells.Rows.Count, "B").End(xlUp).Row Range("A1:G" & lngLastRow2).Copy ThisWorkbook.Worksheets("Add File Here").Activate Range("A1").Select ActiveSheet.Paste Application.CutCopyMode = False Exit For End If Next wSheet If bFound = True Then Exit For Next wBook If rFound Is Nothing Then MsgBox "No open file for Cvent003 Meetings Found. Make sure the most recent Cvent003 Excel WB is open!", vbCritical + vbOKOnly Exit Sub End If 中寻找此工作表中的值。

@Component({
  selector: 'my-app',
  template: `<h1>Hello {{name}}</h1>
  <my-child #child ></my-child>
  <button (click)="child.refresh()" >Refresh Child</button>
  `
})
export class AppComponent { 
  name = 'World'; 
}

@Component({
  selector: 'my-child',
  template: `<h1>Date : {{date}}</h1>`
})
export class ChildComponent { 
  date = new Date();

  refresh(){
    this.date = new Date();
  }
}

1 个答案:

答案 0 :(得分:0)

这应该适合你:

After:=wSheet.Range(ThisWorkbook.Sheets("Data Specs").Range("B2").Value)

您需要将Range对象传递给After参数。这就是为什么来自单元格B2的值必须用作Range方法的参数。

编辑:

尝试将此范围设置为变量,然后在After中使用它:

Set afterRange = wSheet.Range(ThisWorkbook.Sheets("Data Specs").Range("B2").Value)

Find方法中:

After:=afterRange

您甚至可以尝试将var = ThisWorkbook.Sheets("Data Specs").Range("B2").Value分配给变量并在Set afterRange = wSheet.Range(var)

中使用它

EDIT2:

我刚用两个工作簿对你进行了全面检查,一个用于代码,另一个用于搜索值。一切都对我很好。我用了这段代码:

Sub test()
Set wsheet = Workbooks("test2").Sheets("Sheet1")
Set rfound = wsheet.Range("D1:D2").Find(What:=ThisWorkbook.Sheets("Data Specs").Range("A2"), SearchFormat:=True, _
                After:=wsheet.Range(ThisWorkbook.Sheets("Data Specs").Range("B2")), _
                LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
                SearchDirection:=xlPrevious, MatchCase:=True)

rfound.Select
End Sub

结果如下:result

代码从master.xslm运行,Data Specs在master工作簿中。搜索的值在Sheet1的test2中。您可以在即时窗口中看到正确找到值。