1

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.

then I'm trying to loop through busdates like so,

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
Vityata
  • 42,633
  • 8
  • 55
  • 100
excelguy
  • 1,574
  • 6
  • 33
  • 67
  • 2
    Max of what? You're getting a single row number. –  Oct 03 '18 at 15:48
  • @Jeeped , it is getting the last row of column M + 1. I am doing this because I want to paste to after the last row of M. – excelguy Oct 03 '18 at 15:50
  • @excelguy, @Jeeped is referencing your call to `WorksheetFunction.Max(...` and I agree, it doesn't make sense to take the max of a single number – Jchang43 Oct 03 '18 at 15:53
  • 2
    `busdates` is a range (or array) so it doesn't make sense to loop from `2 to busdates`. – SJR Oct 03 '18 at 15:54
  • Are you just trying to copy all of the data from `F2:K & lastrow` without including the actual `M` column, or are you trying to only copy values that are not currently included within the `M` column – Jchang43 Oct 03 '18 at 16:46
  • Trying to copy the values that are not included within the column M. So column F and M are dates. Where the dates are alike, i dont want to copy the data over to M3:M & Lastrow – excelguy Oct 03 '18 at 16:51

2 Answers2

2

The range to be copied here ActiveSheet.Range("F2:K").Copy is not completely defined. There is a row for the K column missing.


Gessing that busdates is inteded to be a range, then it should be assigned as such:

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:

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:

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.

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
Vityata
  • 42,633
  • 8
  • 55
  • 100
  • Thanks. However I dont want to include the same values between column F and busdates. Don't I need an if statement to exclude them? – excelguy Oct 03 '18 at 16:04
  • You don't seem to use the `d` within your for loop, it seems like it would be sufficient to just execute the copy and paste code once. – Jchang43 Oct 03 '18 at 16:19
  • @Brotato but i want to loop through to find the dates between col F and busdates – excelguy Oct 03 '18 at 16:25
  • @excelguy if there is no reference to `d` within any of the functions called, the functions are essentially running over and over again. In this case, you would just be pasting the same values into the same range over and over again. – Jchang43 Oct 03 '18 at 16:35
1

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. 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

Jchang43
  • 891
  • 6
  • 14
  • Thanks Brotato, I get subscript out of range `ElseIf Cell2 Like BusDates(UBound(BusDates)) Then` , seems like its referring to this part, `BusDates(UBound(BusDates))` . Maybe busdates is not being defined properly? – excelguy Oct 04 '18 at 13:14
  • Works nicely! however I forgot to mention I have some data in column A, that is the highest row. I think lastrow is using that as the line to paste. However the column with pasting is correct. – excelguy Oct 04 '18 at 15:45
  • Was able to fix the correct column. However when pasting F2 is being repeated many times then going to F3.. Do you know why this is? – excelguy Oct 04 '18 at 16:32
  • @excelguy No sorry, I'm not sure. Try debugging it line by line and watching the paste array. There's only one paste operation at the end of the code, so it would have to be added on to the array. – Jchang43 Oct 04 '18 at 17:10
  • hey, any reason as to why this pastes dates in a different format? format changes from some from dd-mm-yyyy to mm-dd-yyyy – excelguy Oct 09 '18 at 19:10
  • [Here](https://www.bluepecantraining.com/portfolio/excel-vba-formatting-cells-and-values/) is a short page describing how to alter some of the text properties. Try reformatting it to how you want. – Jchang43 Oct 09 '18 at 20:27
  • But doesnt the number formatting have to be done when putting items in the array, not on the output or pasting? – excelguy Oct 09 '18 at 20:30
  • If it's pasting the values, it should be pasting as a number which is formatted as a date. I believe the number is equal to the number of days since January 1900. – Jchang43 Oct 09 '18 at 20:49