我正在为自己的代码而苦苦挣扎,并承认我是VBA的新手。 是的-我已经审查了很多帖子,但似乎无法修复我的代码!
我正在尝试提取满足特定值(“优先级”)的行,该行位于15列中的H列中。该代码需要从所有15张工作表中提取所有符合条件的行。提取的行将粘贴到首页“ FootpathStrategyTool”中-从单元格A20(在预定义标题下方)开始。 我很难让循环将复制的行粘贴到下一个空白行中。该代码可以运行,但是似乎错过了前几个工作表。这可能与我的最后一行有关。 如果有人可以帮助我,我将万分感谢!!!!
Sub getdata()
'1. what is the priority value you wish to search for?
'2. define lastrow for the sheets
'3. Find records that match priority value and paste them into the main sheet
Dim Priority As Integer
Dim Worksheet As Worksheet
Priority = Sheets("FootpathStrategyTool").Range("I10").Value
' Find the final row of data for all sheets
For Each Worksheet In ActiveWorkbook.Worksheets
Worksheet.Select
finalrow = Cells(Rows.Count, 1).End(xlUp).Row
Dim NextRow As Range
Set NextRow = Sheets("FootpathStrategyTool").Cells(Cells.Rows.Count,
1).End(xlUp).Offset(1, 0)
' Loop through each row
For x = 4 To finalrow
' Decide if to copy based on column H Priority
ThisValue = Cells(x, 8).Value
If ThisValue = Priority Then
Cells(x, 1).Resize(1, 33).Copy
Sheets("FootpathStrategyTool").Select
NextRow = Cells(“A” & Rows.Count).End(xlUp).Row + 19
Cells(NextRow, 1).Select
End If
' Step down 1 row from present location.
ActiveCell.Offset(1, 0).Select
ActiveSheet.Paste
Sheets("FootpathStrategyTool").Select
Next x
Next Worksheet
End Sub
答案 0 :(得分:0)
一些建议希望对您有所帮助:
Dim Worksheet As Worksheet
。通常,您不想声明与对象同名的变量名。它会使事情变得有些混乱。我建议Dim wsData As Worksheet
。
ThisValue = Cells(x, 8).Value
。在执行此操作的同时,您没有使用类型明确声明ThisValue,这意味着ThisValue现在是Variant类型。在上面的示例中,您对“ If ThisValue = Priority Then
”进行了简单的评估,因此该代码中的所有内容都可能会按预期运行,但是在更复杂的应用程序中可能会出现意外结果或错误。 “ finalrow”和“ x”也是如此。
“单元格”和“ .Cells”之间存在显着差异。 “ .Cells”是指您在代码中前面定义的特定工作表,而“ Cells”是指上一个活动的工作表。由于您的代码试图在前表和数据表之间来回跳转时尝试创建行号,因此很容易忘记实际上是从哪个表创建数字的。
在引用特定的工作表和范围时,不一定总是在它们之间跳转并激活它们。您只需指定源和目标位置即可将数据作为数组移动。
不过,我相信,问题的根源是
NextRow = Cells(“A” & Rows.Count).End(xlUp).Row + 19
。在上面进行“声明并设置”时,您建立了最后一行范围,现在重新建立它,然后向其添加另外19行。另外,Cells(“A” & Rows.Count)
不是有效的参考。 “ A”将返回为空,这意味着您实际上是在说Cells(Rows.Count)而不提供列引用。
请考虑将其作为替代。
Sub getdata()
'1. what is the priority value you wish to search for?
'2. define lastrow for the sheets
'3. Find records that match priority value and paste them into the main sheet
'This identifies precisely which sheet you're copying data to.
Dim wsHome As Worksheet: Set wsHome = ActiveWorkbook.Worksheets("FootpathStrategyTool")
Dim Priority As Integer: Priority = wsHome.Range("I10").Value
Dim wsData As Worksheet
Dim ThisValue As Variant
Dim NextRow As Variant
Dim finalrow As Long
'i0 will be used to cycle through the data worksheets.
Dim i0 As Long
'i1 will be used to increment through the rows on the Home sheet starting at row 20.
Dim i1 As Long: i1 = 20
'This says to start at the second worksheet and look at each one to the end.
For i0 = 2 To ActiveWorkbook.Worksheets.Count
'This specifies exactly which worksheet you're working with.
With ActiveWorkbook.Worksheets(i0)
finalrow = .Cells(.Rows.Count, 1).End(xlUp).Row
'Loop through each row
For x = 4 To finalrow
'Decide if to copy based on column H Priority. Pull the whole range as an object.
If .Cells(x, 8).Value = Priority Then
ThisValue = .Range(.Cells(x, 1), .Cells(x, 33)).Value
'Identify the data's destination on the Home tab.
Set NextRow = wsHome.Cells(i1, 1)
'Resize the destination range and drop the data in one line.
NextRow.Resize(1, UBound(ThisValue, 2)).Value = ThisValue
'Increment the cell reference to the next row.
i1 = i1 + 1
End If
Next x
End With
Next i0
End Sub