Hi everyone,
I have a workbook that I am using for estimates and invoices, with code that save as Pdf, create a new sheet as a clone of the estimate you created, and lastly clear contents of the estimate template after everything is saved.
My problem gets in when the sheet is changed, in terms of amount of rows where items are listed. What happened today is the accounts department deleted rows, which moved details into the clearContents area that's not supposed to be cleared. Is there a way apart from a userform, that one can link the area where items are listed with clear contents? I.e. if rows are added or deleted, automatically change clearContents range?
Code below
I have a workbook that I am using for estimates and invoices, with code that save as Pdf, create a new sheet as a clone of the estimate you created, and lastly clear contents of the estimate template after everything is saved.
My problem gets in when the sheet is changed, in terms of amount of rows where items are listed. What happened today is the accounts department deleted rows, which moved details into the clearContents area that's not supposed to be cleared. Is there a way apart from a userform, that one can link the area where items are listed with clear contents? I.e. if rows are added or deleted, automatically change clearContents range?
Code below
VBA Code:
Private Sub CommandButton1_Click()
Dim WS1 As Worksheet
Dim WS2 As Worksheet
Set WS1 = Worksheets("Estimate")
Set WS2 = Worksheets("EDatabase")
' Figure out which row is next row
nextrow = WS2.Cells(Rows.Count, 1).End(xlUp).Row + 1
'Write the important values to register
WS2.Cells(nextrow, 1).Resize(, 4).Value = Array(WS1.Range("F4").Value, WS1.Range("F3").Value _
, WS1.Range("A14").Value, WS1.Range("EstTot").Value)
With Worksheets("Estimate")
docno = .Range(.Cells(4, 6), .Cells(4, 6))
End With
Call saveAsPdf
With Worksheets("EDatabase")
.Hyperlinks.Add Anchor:=.Range(.Cells(nextrow, 1), .Cells(nextrow, 1)), Address:="", SubAddress:= _
docno & "!A1", TextToDisplay:=docno
End With
End Sub
Sub saveAsPdf()
Dim saveLocation As String
Dim rng As Range
saveLocation = "C:\Users\***\Estimates\" & Range("f4").Value & Range("a14").Value & ".pdf"
Set rng = Worksheets("Estimate").Range("A1:g50")
rng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=saveLocation
Call saveSheetWithoutFormulas
End Sub
Sub saveSheetWithoutFormulas()
Dim ws As Worksheet
'f4 is document number
Set wh = Worksheets(ActiveSheet.Name)
ActiveSheet.Copy After:=Worksheets(Sheets.Count)
If wh.Range("f4").Value <> "" Then
ActiveSheet.Name = wh.Range("f4").Value
End If
wh.Activate
With Sheets("Estimate").Range("f4")
.Value = "E" & (Mid(.Value, 2) + 1)
End With
Call clearContents
End Sub
Sub clearContents()
Range("a23:d43").clearContents
Range("f23:f43").clearContents
Range("a14").clearContents
End Sub
Thanks in advance