Looping or Autofill to Final Row in VBA

Vlr516

New Member
Joined
Sep 11, 2017
Messages
22
I have worked several days to get my macro to work but itwill not fill down the data to the FinalRow of a referenced worksheet.

Background: a person opens a workbook (wbPPM) and goes to a variable worksheet(shtReq) where the person kicks off a macro. The macro open another workbook(wbQC) with one preformatted worksheet (shtQC) and inserts a new worksheet(shtLoad) into the active workbook and then closes the other workbook (wbQC).The new worksheet (shtLoad) will refer to the data on the variable worksheet(shtReq) which begins on row 6 and the FinalRow is determined by column C,perform a formula to concatenate, and then drop the data into the new worksheetwhich also begins on row 6.

I can get it to do the first line, but not loop or autofill down. I've triedboth.

Sub [Name] ()
Dim wbPPM As Workbook
Dim wbQC As Workbook
Dim shtTitle As Worksheet
Dim shtQC As Worksheet
Dim shtLoad As Variant
Dim shtReq As Variant
Dim ReqNo As String
Dim ReqName As String
Dim FinalRow As Long

Set wbPPM = ThisWorkbook
Set shtTitle = wbPPM.Worksheets(1)
Set shtReq = ActiveSheet
Set wbQC = Workbooks.Open("[Location])
Set shtQC = wbQC.Worksheets("[Name]")

Desc = shtReq.Range("$D6").Value
ReqNo = shtReq.Range("$A6).Value
ReqName = shtReq.Range("$C6")
PPMNo = shtTitle.Range("$G$9").Value
shtQC.Copy Before:=wbPPM.Sheets("[Name]")
wbQC.Close
Set shtLoad = ActiveSheet

With shtReq
FinalRow = shtReq.Cells(Rows.Count, 2).End(xlUp).Row
End With

Range("C6").Formula = ReqNo & "--" & ReqName
ActiveCell.AutoFill Destination:=Range("C6:C" & FinalRow),Type:=xlFillDefault
Range("D6").Formula = Desc
ActiveCell.AutoFill Destination:=Range("D6:D" & FinalRow), Type:=xlFillDefault
Range("L6").Formula = PPMNo
ActiveCell.AutoFill Destination:=Range("L6:L" & FinalRow),Type:=xlFillDefault

End Sub<o:p></o:p>

 
Thank you for your help. After moving the FinalRow above the Set statements I was able to get 2 out of the 3 autofills to work properly. Yay!

For the last autofill:
Desc = shtReq.Range("D6").Value This is really a merged cells of D6:G on the original sheet.
On the new worksheet:
Range("D6").Formula = Desc
This too is really a merged cells D6:G

Therefore the autofill isn't working because it says we can't do that to a merged cell:
Range ("D6").AutoFill Destination:=Range("D6:D" & FinalRow), Type:=xlFillDefault

Without changing the merged cells, is there a different code I can use on this one? The reason for the merged cells is to keep all the columns consistently the same size but allow someone a larger area for particular information.
 
Last edited:
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top