Hi,
I used to transfer invoice data to another workbook [data], sheet "sales" and sheet"csales also add new workbook invoice sheet with below code, it took 3 second to complete.
is there is way to speed up this process.
I used to transfer invoice data to another workbook [data], sheet "sales" and sheet"csales also add new workbook invoice sheet with below code, it took 3 second to complete.
is there is way to speed up this process.
Code:
Sub SavingSalesData()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlManual
Dim wb As Workbook '''!
Dim CurrentWB As Workbook '''!
Dim WBLoc As String '''!
Dim rng As Range
Dim i As Long
Dim a As Long
Dim b As Long
Dim rng_dest As Range
WBLoc = "g:\data.xlsm" '''! Location of the workbook
Set CurrentWB = Excel.ThisWorkbook '''!
Set wb = Workbooks.Open(WBLoc) '''! Opens the workbook
wb.Sheets("sales").Unprotect Password:="123"
wb.Sheets("csales").Unprotect Password:="123"
i = 1
Set rng_dest = wb.Sheets("sales").Range("D:i") '''! Change Sheets() to whichever sheet you want to use
' Find first empty row in columns D:F on sheet Sales Book
Do Until WorksheetFunction.CountA(rng_dest.Rows(i)) = 0
i = i + 1
Loop
'Copy range A23:D27 on sheet Invoice to Variant array
Set rng = CurrentWB.Sheets("Invoice").Range("A8:e24") '''!
' Copy rows containing values to sheet Sales Book
For a = 1 To rng.Rows.Count
If WorksheetFunction.CountA(rng.Rows(a)) <> 0 Then
rng_dest.Rows(i).Value = rng.Rows(a).Value
With wb.Sheets(1) '''! Change Sheets() to whichever sheet you want to use
'Copy Invoice number
.Range("a" & i).Value2 = CurrentWB.Sheets("Invoice").Range("e3").Value2 '''!
'Copy Date
.Range("b" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f4").Value2 '''!
'Copy Company name
.Range("C" & i).Value2 = CurrentWB.Sheets("Invoice").Range("d6").Value2 '''!
'tel
.Range("d" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f6").Value2 '
'dis
.Range("j" & i).Value2 = CurrentWB.Sheets("Invoice").Range("h26").Value2 '
'id
.Range("l" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f5").Value2 '
'amount
.Range("i" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f25").Value2 '
i = 1
Set rng_dest = wb.Sheets("csales").Range("D:i") '''! Change Sheets() to whichever sheet you want to use
' Find first empty row in columns D:F on sheet Sales Book
Do Until WorksheetFunction.CountA(rng_dest.Rows(i)) = 0
i = i + 1
Loop
'Copy range A23:D27 on sheet Invoice to Variant array
Set rng = CurrentWB.Sheets("Invoice").Range("A8:e24") '''!
With wb.Sheets(2) '''! Change Sheets() to whichever sheet you want to use
'Copy Invoice number
.Range("a" & i).Value2 = CurrentWB.Sheets("Invoice").Range("e3").Value2 '''!
'Copy Date
.Range("b" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f4").Value2 '''!
'name
.Range("C" & i).Value2 = CurrentWB.Sheets("Invoice").Range("d6").Value2 '''!
'tel
.Range("d" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f6").Value2 '
'id
.Range("e" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f5").Value2 '
'amount
.Range("f" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f25").Value2 '
'Discount
.Range("g" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f26").Value2 '
'paid
.Range("h" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f27").Value2 '
'balance
.Range("i" & i).Value2 = CurrentWB.Sheets("Invoice").Range("f28").Value2 '
'.Range("f" & i).Value = CurrentWB.Sheets("Invoice").Range("f5").Value '
End With '''!
End With '''!
'''!
i = i + 1
End If
Next a
wb.Sheets("sales").Protect Password:="123"
wb.Sheets("csales").Protect Password:="123"
ThisWorkbook.Activate
wb.Close savechanges:=True '''! This wil close the Workbook and save changes
Set wb = Nothing '''! Cleaning memory
Set CurrentWB = Nothing '''! Cleaning memory
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.Calculation = xlAutomatic
End Sub
Code:
Sub saveInvWithNewName()Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlManual
Dim NewFN As Variant
SavingSalesData
Dim smallrng As Range
'copy invoice to a new workbook
If Dir("g:\aaa", vbDirectory) = "" Then
MkDir Path:="g:\aaa"
End If
If Len(Dir("g:\aaa\" & MonthName(Month(Date), False), vbDirectory)) = 0 Then
MkDir "g:\aaa\" & MonthName(Month(Date), False)
End If
Set newbook = Workbooks.Add
Workbooks("invoice.xlsm").Worksheets("invoice").Range("b1:f28").Copy
newbook.Worksheets("Sheet1").Range("b1").PasteSpecial Paste:=xlPasteColumnWidths
Selection.PasteSpecial Paste:=xlPasteAllUsingSourceTheme, Operation:=xlNone _
, SkipBlanks:=False, Transpose:=False
For Each smallrng In Range("f8:f28,f4").Areas
With smallrng
.Cells.Copy
.Cells.PasteSpecial xlPasteValues
.Cells(1).Select
End With
Application.CutCopyMode = False
Next smallrng
NewFN = "g:\aaa\" & MonthName(Month(Date), False) & "\" & "inv" & Range("e3") & "-" & Format(Date, "mmm.yyyy") & ".xlsx"
ActiveSheet.SaveAs Filename:=NewFN
ActiveWorkbook.Close
nextInvoice
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.Calculation = xlAutomatic
End Sub
Thanks