VBA-EXCEL使用变量

时间:2017-06-09 15:08:13

标签: excel vba excel-vba

问题:我无法使用变量(i)和特定单元格行(cell.Row)定义范围。

当前代码:

Sub TaskSearch()
'Dim wb As Workbook
Dim oSht As Worksheet
Dim lastRow As Long, i As Long
Dim strSearch As String
Dim aCell As Range


ThisWorkbook.Sheets("Interface").Range("D19:D33").ClearContents

'Set wb = Workbooks.Open("H:\Kevin.Boots\Database.xlsx")
Set oSht = Sheets("TaskMaster")
lastRow = oSht.Range("A" & Rows.Count).End(xlUp).Row
strSearch = Sheets("Interface").Range("F5")

Set aCell = oSht.Range("B2:B" & lastRow).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)

  Sheets("Interface").Range("D19").Value = Sheets("TaskMaster").Range("C" & aCell.Row).Value
  Sheets("Interface").Range("D20").Value = Sheets("TaskMaster").Range("D" & aCell.Row).Value
  Sheets("Interface").Range("D21").Value = Sheets("TaskMaster").Range("E" & aCell.Row).Value
  Sheets("Interface").Range("D22").Value = Sheets("TaskMaster").Range("F" & aCell.Row).Value
  Sheets("Interface").Range("D23").Value = Sheets("TaskMaster").Range("G" & aCell.Row).Value
  Sheets("Interface").Range("D24").Value = Sheets("TaskMaster").Range("H" & aCell.Row).Value
  Sheets("Interface").Range("D25").Value = Sheets("TaskMaster").Range("I" & aCell.Row).Value
  Sheets("Interface").Range("D26").Value = Sheets("TaskMaster").Range("J" & aCell.Row).Value
  Sheets("Interface").Range("D27").Value = Sheets("TaskMaster").Range("K" & aCell.Row).Value
  Sheets("Interface").Range("D28").Value = Sheets("TaskMaster").Range("L" & aCell.Row).Value
  Sheets("Interface").Range("D29").Value = Sheets("TaskMaster").Range("M" & aCell.Row).Value
  Sheets("Interface").Range("D30").Value = Sheets("TaskMaster").Range("N" & aCell.Row).Value
  Sheets("Interface").Range("D31").Value = Sheets("TaskMaster").Range("O" & aCell.Row).Value
  Sheets("Interface").Range("D32").Value = Sheets("TaskMaster").Range("P" & aCell.Row).Value
  Sheets("Interface").Range("D33").Value = Sheets("TaskMaster").Range("Q" & aCell.Row).Value

Exit Sub

End Sub

目标:我正在尝试使此代码更加健壮。推理的一部分是让我能够跳过空白。当试图调整细胞时,这是一场噩梦。

我尝试了两种不同的方法来实现这一目标:

方法A:

wb.Sheets("Interface").Range("D19:D33").Copy
  wb.Sheets("TaskMaster").Range("C" & aCell.Row & ":Q" & aCell.Row).PastSpecial Paste:=xlPasteValues, SkipBlanks:=True

失败:运行时错误438:对象不支持此属性或方法。

方法B:

   For j = 3 To 16
    If Not IsEmpty(j, aCell.Row) Then
        For i = 19 To 33

            iWb.oSht.Range(j, aCell.Row).Value = _
           iWb.iSht.Range(4, i).Value

        Next i
    End If
Next j

Exit Sub

失败:(我无法让这个旧版本再次编译)我认为错误是由定义范围的问题引起的。

总结一下,我正在尝试找到使用.find将信息从一个工作表传输到另一个工作表的最快方法。我也试图在转移时不复制空白单元格。

我目前认为这种方法最适合我的应用。

Sub TSearch()
Dim dWb As Workbook, Wb As Workbook
Dim oSht As Worksheet, Sht As Worksheet
Dim lastRow As Long, i As Long, j As Long
Dim strSearch As String
Dim aCell As Variant
Dim cell As Variant
'On Error GoTo Err

ThisWorkbook.Sheets("Interface").Range("D19:D33").ClearContents

'Set dWb = Workbooks.Open("H:\Kevin.Boots\Database.xlsx")
Set Wb = ThisWorkbook
Set Sht = Sheets("TaskMaster") ' Reference Worksheet
Set oSht = Sheets("Interface") ' User Interface Worksheet

lastRow = Sht.Range("A" & Rows.Count).End(xlUp).Row 'Obtain Last row of reference Worksheet
strSearch = oSht.Range("F5") 'Obtain User Selected Search Criteria

Set aCell = Sht.Range("B2:B" & lastRow).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)

For j = 3 To 16 'Columns from Reference Worksheet to be transfered
    If Not IsEmpty(Wb.Sht.Cells(aCell.Row, j)) Then ' Verify If cell has value before transfering
        For i = 19 To 33 ' Rows of User Interface where values are to be transfered
            Wb.Sht.Cells(aCell.Row, j).Value = _
            Wb.oSht.Cells(i, 4).Value

        Next i
    End If
  Next j
 Exit Sub

 'Err: 'MsgBox " Generic Task not found" & vbCrLf

 End Sub

IEmpty函数仍然导致错误438:对象不支持此属性方法。如果我删除了IsEmpty 'Wb.Sht.Cells(aCell.Row,j).Value = Wb.oSht.Cells(i,4).Value'给了我同样的错误。

2 个答案:

答案 0 :(得分:0)

由于IsEmpty需要检查单个单元格或变量,但是你给它两个数字,你的循环将无效。以下应该有效,但有些事情不合格,所以你可能仍会遇到问题。此外,Range()要求提供两个范围,或者范围字符串。我认为您正在寻找Cells(),它接受​​一行(作为数字)作为第一个参数,一列(作为数字)作为第二个参数。

For j = 3 To 16
    If Not IsEmpty(cells(aCell.Row, j)) Then
        For i = 19 To 33

            iWb.oSht.Cells(aCell.Row, j).Value = _
           iWb.iSht.Cells(4, i).Value

        Next i
    End If
Next j

End Sub

答案 1 :(得分:0)

似乎错误是尝试定义已定义的内容的结果。

Sub TSearch()
Dim dWb As Workbook, Wb As Workbook
Dim oSht As Worksheet, Sht As Worksheet
Dim lastRow As Long, i As Long, j As Long
Dim strSearch As String
Dim aCell As Variant
Dim cell As Variant
'On Error GoTo Err

ThisWorkbook.Sheets("Interface").Range("D19:D33").ClearContents

'Set dWb = Workbooks.Open("H:\Kevin.Boots\Database.xlsx")
Set Wb = ThisWorkbook
Set Sht = Sheets("TaskMaster")                      ' Reference Worksheet
Set oSht = Sheets("Interface")                      ' User Interface Worksheet

lastRow = Sht.Range("A" & Rows.Count).End(xlUp).Row 'Obtain Last row of reference Worksheet
strSearch = oSht.Range("F5")                        'Obtain User Selected Search Criteria

 'Find Row in Reference Worksheet that Matches Search Criteria
Set aCell = Sht.Range("B2:B" & lastRow).Find(What:=strSearch, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)

For j = 3 To 16
    If Not IsEmpty(Cells(aCell.Row, j)) Then
        i = j + 16
            oSht.Cells(i, 4).Value = Cells(aCell.Row, j).Value
    End If
Next j

 Exit Sub

 'Err:
'MsgBox " Generic Task not found" & vbCrLf

 End Sub

感谢@Jordan和@Kyle帮助解决这个问题。