I'm putting together a series of macros and have reached the end of my skillset in VBA (which is limited to begin with).
I have two sheets with the second being dependent on the first for its content. Currently, users are required to copy and insert the content from the first sheet into the second and then manipulate it, which, understandably, nobody does because they're either lazy or intimidated.
To make it more likely that they will copy the information to the second sheet, I want to use a macro called from a button.
There are essentially two issues I need to overcome, and I'm praying that you'll be able to help.
- The region to be copied is not static and will vary in the number of rows that need to be copied; and
- Not all of the columns in the copied range need to be inserted into the destination sheet.
To address the first issue, I know that I will need to use some variation of the LastrRow used as provided in this answer: https://stackoverflow.com/a/11169920/4693144
I've tried using that code as follows, but it keeps kicking "Error '9': Subscript out of range" back at me:
Sub CopyBudget()
Dim LastRow As Long
Dim CopyRange As Range
With Sheets("Project Budget")
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
LastRow = .Cells.Find(What:="*", _
After:=.Range("A8"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlFormulas, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
LastRow = 1
End If
End With
Sheets("Project Plan").Range("D60").Resize(LastRow) = _
Sheets("Project Budget").Range("A8").Resize(LastRow).Value
If Not CopyRange Is Nothing Then
CopyRange.Copy Sheets("Project Plan").Cells(60, "D")
End If
End Sub
EDIT: My apologies for not posting my error reference when I originally wrote the post (I was running out the door at work). The Error highlights the entire block of code:
LastRow = .Cells.Find(What:="*", _
After:=.Range("A8"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlFormulas, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
I realise that this code would only serve to copy the range itself and not to differentiate between the columns. The issue is that I'm not sure how to go about ensuring that the copied rows only contain the desired columns. Should I just use several variations of the functional version of the code above based on the desired columns running in series to copy and insert each column independently, or should I just grab it all and then use code to select the columns I want from the copied or inserted cells?
I'm really looking for as much help on this as I can get.