Macro closes wb (not supposed to)

Jenawade

Board Regular
Joined
Apr 8, 2002
Messages
231
I tried to use parts of two working macro's to make a new one for a repetitive task. When I run the macro, all it does is close the active workbook. What have I done wrong, What needs to change?

++++++++ I USED THIS ++++++++

Code:
Sub Make_Batch_Forms()
    Dim lngRowRef As Long, lngFileNr As Long
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    With Sheets("RFMD Form").Rows("4:11")
        For lngRowRef = 2 To 102
            lngFileNr = lngRowRef + 248
            ActiveWorkbook.SaveAs Filename:="2011-" & lngFileNr & ".xlsm", _
                FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
           .Replace What:="$" & lngRowRef, Replacement:="$" & lngRowRef + 1, _
                LookAt:=xlPart
         Next lngRowRef
    End With
    ActiveWorkbook.Close SaveChanges:=False
    Application.DisplayAlerts = True
End Sub

++++++++ AND THIS ++++++++

Code:
Sub Example3_Loop()
'
' Example3_Loop
'
    StartVal = 1
    NumToFill = 12
    ActiveCell.Value = StartVal
    For Cnt = 0 To NumToFill - 1
        ActiveCell.Offset(Cnt, 0).Value = StartVal + Cnt
    Next Cnt
'
End Sub

++++++++ TO MAKE THIS ++++++++

Code:
Sub Make_Batch_Forms()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    With Sheets("PLQ Form").Rows("4:59")
    StartVal = 2
    EndVal = 26
    For Cnt = 0 To NumToFill - 1
            PQname = Range("R4").Value
            ActiveWorkbook.SaveAs Filename:=PQname & ".xlsm", _
                FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
                
           .Replace What:="$" & StartVal, Replacement:="$" & StartVal + Cnt, _
                LookAt:=xlPart
                
         Next Cnt
    End With
    ActiveWorkbook.Close SaveChanges:=False
    Application.DisplayAlerts = True
End Sub

I just needs to save as the value of R4, then change the formulas to the next row down, ($2 to $3), then save as value of R4, chance $3 to $4, etc.
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Does removing this line of code help?

Code:
    ActiveWorkbook.Close SaveChanges:=False
 
Upvote 0
Why yes, removing that line does keep the file open. Go figure. ;-) But then it does nothing at all.
 
Upvote 0
I'm not sure why it does nothing but you are missing a dot

Rich (BB code):
PQname = .Range("R4").Value
 
Upvote 0
You haven't defined what NumToFill is so it won't do anything ;)

I literally LOL'd at that one! Thank you! Its trying to work now. It errors at the save as, giving me an access error. Its trying to save in some weird location. How can I tell it to save at ThisWorkbook.Path?

Code:
NumToFill
[/code}
 
Upvote 0
Try

Code:
ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & PQname & ".xlsm", _
                FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
 
Upvote 0
Its give me type mismatch error on that line.

Code:
Sub Make_Batch_Forms()


    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
 
    With Sheets("PLQ Form").Rows("4:59")
    StartVal = 2
    NumToFill = 26
    For Cnt = 0 To NumToFill - 1
    PQname = .Range("R4").Value
                            
    ActiveWorkbook.SaveAs Filename:=ActiveWorkbook.Path & "\" & PQname & ".xlsm", _
                FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
                
         Next Cnt
    End With
    Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,798
Members
452,943
Latest member
Newbie4296

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