1
votes

Writing VBA code to copy a dynamic range to a new worksheet. The code is supposed to first define a range which is the range to be copied. It does this by starting in the upper left hand corner of where the range will begin, then using Range.End(xlDown) to find the last entry. The offset then finds the bottom right hand corner of the range, and the range is set to span from the upper left hand corner to the lower right hand corner. That's how it is supposed to work, and how it does work for a verbatim sub, where the only changes are in the variable names for clarity.

Here's where it goes south. The Range.End(xlDown) is indicating that the last non-blank cell in the column is the very, very bottom cell on the worksheet (Like row 40,000 something). This is clearly not true, as the last non-blank cell is three rows down from the range I am looking at. Thus, rather than getting a 4x5 size range, I get one that spans nearly the entire height of the sheet. I have also tried clearing all the formatting of the column in case something was lingering, but to no avail. The code is below.

    Sub Copy_Starters_ToMaster()

    Dim MasterIO As Worksheet, IOws As Worksheet
    Set MasterIO = Worksheets("Master IO Worksheet")
    Set IOws = Worksheets("IO Worksheet")

    'Sets a range to cover all VFDs entered for an enclosure.
    Dim EnclosureStarters As Range, BottomLine As Range
    Set BottomLine = IOws.Range("$Z$6").End(xlDown).Offset(0, 3)
    Set EnclosureStarters = IOws.Range("$Z$6", BottomLine)

    'Finds first blank line in Master VFD table
    Dim myBlankLine As Range
    Set myBlankLine = MasterIO.Range("$AB$6")

    Do While myBlankLine <> vbNullString
       Set myBlankLine = myBlankLine.Offset(1, 0)
    Loop

    'Copies over enclosure list of VFDs, pastes into the master at the bottom of the list.
    EnclosureStarters.Copy
    myBlankLine.PasteSpecial
    Dim BottomInEnclosure As Range, currentStarterRange As Range, EnclosureNumber As Range

    'Indicates which enclosure each VFD copied in belongs to, formats appropriately.
    Set BottomInEnclosure = myBlankLine.End(xlDown)
    Set currentStarterRange = Range(myBlankLine, BottomInEnclosure).Offset(0, -1)
        For Each EnclosureNumber In currentStarterRange
            With EnclosureNumber
                .Value = Worksheets("Math Sheet").Range("$A$11").Value
                .BorderAround _
                ColorIndex:=1, Weight:=xlThin
                .HorizontalAlignment = xlCenter
            End With
        Next EnclosureNumber

    End Sub

Any advice on this would be much appreciated, it is endlessly frustrating. Please let me know if I need to post photos of the errors, or any further code, etc.

2
Is the "supposedly blank" cell actually blank, or is it ""? Because if you are on cell A1, and type ="" into the next 10 cells, and you go CTRL + DOWN, it will take you down to the end of those 10 cells. And are you sure you're looking at the right column?Grade 'Eh' Bacon
Are the values in column A numbers or strings? Were they returned by a formula? Has the formula(s) been reverted to its/their returned values?user4039065
@Grade 'Eh' Bacon Yep, it should be actually blank. I selected all the columns of the source range from the column bar at the top, and did a "Clear All". So theoretically it should have also cleared any null strings like ="". And yes, looking at the first column in the source range where the data is entered, so it is indicative of how many rows of entries I have.cmwarner
@Jeeped Sorry, not quite sure which column A you are referring to. If it is the "Math Sheet" column A, the range mentioned in the code is a number placed there by code. It just is a placeholder to hold a parsed Enclosure number that came from a string.cmwarner
Which line is throwing the error?KFichter

2 Answers

0
votes

I think the answer here is to use xlUp and a generic lastrow formula such as:

Set BottomLine = IOws.Cells(Rows.Count, "Z").End(xlUp).Offset(0, 3)

That gives the last used cell in column Z, offset by 3

0
votes

Cleaner again to use Find rather than rely on the xlUp type shortcuts, something like this (which also caters for the column being empty, which is a common error when setting ranges):

Dim rng1 As Range
Set rng1 = IOws.Range("Z:Z").Find("*", IOws.[z1], xlFormulas, , xlPrevious)
If Not rng1 Is Nothing Then Set Bottomline = rng1.Offset(0, 3)
`if rng1 is Nothing then the area searched is blank