[英]VBA, define range with LastRow
I am having trouble defining my variable with my last row variable. 我在用最后一行变量定义变量时遇到麻烦。 Getting error: 出现错误:
application-defined or object defined error 应用程序定义或对象定义的错误
LastRow = WorksheetFunction.Max(Sheets("stack").Cells(Rows.Count, "M").End(xlUp).Row + 1)
busdates = Sheets("stack").Range("M3" & ":" & "M & LastRow - 1")
I know it is something to do with my range. 我知道这与我的范围有关。 Can someone help with the format of this? 有人可以帮忙吗? Trying to get the range of M3 to M Last row. 试图获取M3到M最后一行的范围。
then I'm trying to loop through busdates
like so, 然后,我通过努力环busdates
像这样,
For d = 2 To busdates
If ActiveSheet.Range("F") <> busdates Then
ActiveSheet.Range("F2:K").Copy
ActiveSheet.Range("M" & LastRow).PasteSpecial Paste:=xlPasteValues
End If
Next
The range to be copied here ActiveSheet.Range("F2:K").Copy
is not completely defined. 此处要复制的范围ActiveSheet.Range("F2:K").Copy
尚未完全定义。 There is a row for the K
column missing. K
列缺少一行。
Gessing that busdates
is inteded to be a range, then it should be assigned as such: 考虑到busdates
实际上是一个范围,因此应该这样指定:
Dim busDates As Range
Set busDates = Sheets("stack").Range("M3:M" & lastRow - 1)
And looping through the rows of a range is a bit meaningless, if the d
variable is not used in the loop, but still: 如果在循环中未使用d
变量,但仍然循环遍历范围的行是没有意义的:
For d = 2 To busDates.Rows.Count + 2
ActiveSheet.Range("F2:K" & lastRow).Copy
ActiveSheet.Range("M" & lastRow).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Next
Probably looping through busDates
could be done like this: 大概通过busDates
循环可以像这样完成:
Dim myCell As Range
For Each myCell In busDates
If myCell.Row > 2 Then
'some cut and copy here
End If
Next myCell
Last but not least, the ActiveSheet
is to be avoided in VBA, but in this case it is probably harmless - How to avoid using Select in Excel VBA . 最后但并非最不重要的一点是,在VBA中应避免使用ActiveSheet
,但在这种情况下,它可能是无害的- 如何避免在Excel VBA中使用Select 。
The whole code that works somehow is here: 可以正常工作的整个代码在这里:
Sub TestMe()
Dim lastRow As Long
lastRow = WorksheetFunction.Max(Sheets("stack").Cells(Rows.Count, "M").End(xlUp).Row)
lastRow = lastRow + 1
Dim busDates As Range
Set busDates = Sheets("stack").Range("M3:M" & lastRow - 1)
Dim d As Long
For d = 2 To busDates.Rows.Count + 2
ActiveSheet.Range("F2:K" & lastRow).Copy
ActiveSheet.Range("M" & lastRow).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Next
End Sub
I haven't tested this with any data yet, but you might be able to adapt something like this 我还没有用任何数据测试过,但是您也许可以适应这样的情况
Option Explicit
Sub test()
Dim DataArr() As Variant
Dim BusDates() As Variant
Dim PasteArr() As Variant
Dim LastRow As Long
Dim Cell1 As Variant
Dim Cell2 As Variant
Dim index As Long
Dim Matched As Boolean
Dim subcount As Long
LastRow = Worksheets("stacks").Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
DataArr() = Worksheets("stacks").Range("F2:K" & Worksheets("stacks").Cells(Rows.Count, "F").End(xlUp).Row).Value
BusDates() = Worksheets("stacks").Range("M3:M" & LastRow).Value
ReDim PasteArr(1 To 1, 1 To 6)
subcount = 1
For Cell1 = 1 To UBound(DataArr(), 1)
For Each Cell2 In BusDates()
If DataArr(Cell1, 1) Like Cell2 Then
Matched = True
Exit For 'if it matches it will exit
ElseIf Cell2 Like BusDates(UBound(BusDates), 1) Then 'if it gets to the end, it's truly unique and needs to be added
For index = 1 To 6
PasteArr(subcount, index) = DataArr(Cell1, index)
Next index
subcount = subcount + 1
PasteArr = Application.Transpose(PasteArr)
ReDim Preserve PasteArr(1 To 6, 1 To subcount)
PasteArr = Application.Transpose(PasteArr)
Matched = False
End If
Next Cell2
If Matched = False Then
BusDates = Application.Transpose(BusDates)
ReDim Preserve BusDates(1 To UBound(BusDates) + 1)
BusDates = Application.Transpose(BusDates)
BusDates(UBound(BusDates), 1) = DataArr(Cell1, 1)
End If
Next Cell1
Worksheets("stacks").Range("M" & LastRow + 1 & ":" & Cells(LastRow + UBound(PasteArr, 1) - 1, 18).Address).Value = PasteArr
End Sub
You need two for loops so that you can iterate through each date in the data array and compare it to every date in the M column to ensure that it's truly unique. 您需要两个for循环,以便可以遍历数据数组中的每个日期,并将其与M列中的每个日期进行比较,以确保其真正唯一。 The exit for speeds it up a little bit by skipping the rest of comparisons once it gets a match. 出口通过在获得匹配项后跳过其余比较来加快速度。
EDIT: I've tested it a little and made some changes but this seems to work. 编辑:我已经对其进行了一点测试,并进行了一些更改,但这似乎工作。 It's worth noting that the LastRow
will screw up if your data isn't in a square or rectangular shape because it might end up adding an null character or something to the compare array and you'll get a type mismatch when comparing Cell2
值得注意的是,如果您的数据不是正方形或矩形, LastRow
将会搞砸,因为它可能最终会在比较数组中添加空字符或其他内容,并且在比较Cell2
时会出现类型不匹配的情况
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.