logo

Generating Invoices

Posted by Malkit Sidhu in VB 6.0

Monday, June 01, 2009 1:05:10 AM

I am new to VBA and I need to Figure out how to generate Invoice. The requirement is that when the PO is opened from a template it will increment the PO number and, after it is filled, it will save and print the PO at a Prompt. As well I would like to store some fields(cells) in a separate workbook as a  "invoice log" which would include PO#, date, customer, amount, Paid.  Thanks in advance Below is how far I got so far going through the web . This copies cells from one sheet to the second sheet but I would like to Copy to different workbook.
Could you guys give me hand.



'Creates a Invoice copy with Incremented PO#
Private Sub Workbook_Open()
Range("A1") = Range("A1") + 1
ActiveWorkbook.Save
Dim FName     As String
Dim FPath     As String
   
    FPath = "C:\Users\Malkit\Desktop\Bull_Shit"
    FName = Sheets("Sheet1").Range("A1").Text
    ThisWorkbook.SaveAs Filename:=FPath & "\" & "Trailer_Invoice#_" & FName



'Copies Cells in sheet1 to sheet 2
Private Sub CopyThem()
    NextRow = Worksheets("Sheet2").Range("A65536").End(xlUp).Row + 1
    Worksheets("Sheet2").Cells(NextRow, 1).Resize(1, 4).Value = Array( _
        Worksheets("Sheet1").Range("C10").Value, _
        Worksheets("Sheet1").Range("d14").Value, _
        Worksheets("Sheet1").Range("e12").Value, _
        Worksheets("Sheet1").Range("I18").Value)
        MsgBox "file copied ,printed and log updated"

End Sub

Biography
Malkit has not submitted biographical details.

Site Rank:  Not applicable - Current Winnings:  $0.00

Reply Reply Using Power Editor
Rolf Jaeger

A Suggestion

Rolf Jaeger replied to Malkit Sidhu

EggHeadCafe's human moderators scored this post for our messageboard contest.

Monday, June 01, 2009 3:07:34 PM

To copy to another workbook follow this idea:

Sub Demo()
    Dim wb1 As Excel.Workbook
    Dim wb2 As Excel.Workbook
    Set wb1 = ActiveWorkbook
    
    Dim myExcelApp As New Excel.Application
    myExcelApp.Visible = True
    Set wb2 = myExcelApp.Workbooks.Add
    
    Dim r As Excel.Range
    Set r = wb1.ActiveSheet.Range("A1:A2")
    
    Dim c As Excel.Range
    For Each c In r.Cells
        wb2.Worksheets("Sheet1").Range(c.Address).Value = c.Value
    Next
End Sub

Biography
PhD in physics, Technical University Munich, Germany. Over 20 years of technical management experience at Hewlett-Packard and Agilent Technologies. Programming expertise: - VB.NET - C# - ASP.NET - Excel VBA

Site Rank:  9th place - Current Winnings:  $40.00

Reply Reply Using Power Editor

Copying book1 cellc to book2

Malkit Sidhu replied to Rolf Jaeger

Wednesday, June 03, 2009 12:07:39 AM

Hi Rolf,
Sorry....
Besides being new to VBA (my first attempt at macro), my description of my requirements is also sadly lacking....

Anyway, Iwill try again. Please bear with me...

Book1(SourceFile),  Book2 (LogFile) and Book3(updated Book1....as a new invoice) ----- same directory

Book1  (SourceFile)
Write protected file. Opens  (with password) and increments and updates cell A1   ......used as New Invoice# (Please see my original lines of code which works for incrementing invoice# and saving as a new file with new invoice#)
Important cells (example): B2, C7, D7,E7, H10

Book2 (LogFile)
Write protected file and stays closed. It is essentially a log of all invoices.
Important cells: A1, B1, C1, D1, E1  
next invoice
                         A2, B2, C2, D2, E2
and so on.....

Book3 (Filled Book1) copy .....(copy of Full Invoice)
After Book1 is Updated, Its is saved as a new invoiced

Process:
When Book1 is opened, it unprotects itself  with a password and increments A1 indicating new Invoice# and saves itself as a new invoice(Book3) and remains open for it to be updated.
After it is updated, a button activates save(as Book3) ... new invoice....as well as an update of information on the next empty row of Book2. A prompt is displayd after to email and/or print a copythis invoice.

The two small bits of code I included originally work separately but the Log is only filled in the same workbook.
I looked at your code and I can uderstand , I think most of it, but I cant seam to get it working so I found some other code,below, written by Zack which appear to suggest should do exactly what you suggested but I cant understand what some of the code is. Perhaps you could explain it for me if you dont mind but I cant work this one either. Sorry I appear to be a little too green. My background is RF.  Thanks again.


Zack Barresse: solution
Option Explicit

Sub TransferData()
Dim wkb As Workbook, wks As Worksheet, LastRow As Long
Dim FilePath As String, FileName As String
Dim ws As Worksheet, blnOpened As Boolean
'Change these variables as desired...
FilePath = "C:\YourFullFilePathHere\" 'change path here
FileName = "Book2.xls" 'change name here
Call ToggleEvents(False)
Set ws = ThisWorkbook.Sheets("Sheet1") 'change source sheet name here
If WbOpen(FileName) = True Then
Set wkb = Workbooks(FileName)
blnOpened = False
Else
If Right(FilePath, 1) <> Application.PathSeparator Then
FilePath = FilePath & Application.PathSeparator
End If
Set wkb = Workbooks.Open(FilePath & FileName)
blnOpened = True
End If
Set wks = wkb.Sheets("Sheet1") 'change destination sheet name here
LastRow = wks.Cells.Find(what:="*", after:=wks.Cells(1, 1), searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
wks.Cells(LastRow, "A").Value = ws.Cells(1, "B").Value
wks.Cells(LastRow, "B").Value = ws.Cells(4, "B").Value
wks.Cells(LastRow, "C").Value = ws.Cells(7, "B").Value
wks.Cells(LastRow, "D").Value = ws.Cells(7, "E").Value
If blnOpened = True Then
wkb.Close SaveChanges:=True
End If
If MsgBox("Clear values?", vbYesNo, "CLEAR?") = vbYes Then
Call ClearData
End If
Call ToggleEvents(True)
End Sub

Sub ClearData()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet1") 'change as desired
ws.Range("B1").ClearContents 'Name
ws.Range("B4").ClearContents 'Address
ws.Range("B7").ClearContents 'Age
ws.Range("E7").ClearContents 'Sex
End Sub

Sub ToggleEvents(blnState As Boolean)
'Originally written by firefytr
With Application
.DisplayAlerts = blnState
.EnableEvents = blnState
.ScreenUpdating = blnState
If blnState Then .CutCopyMode = False
If blnState Then .StatusBar = False
End With
End Sub

Function WbOpen(wbName As String) As Boolean
'Originally found written by Jake Marx
On Error Resume Next
WbOpen = Len(Workbooks(wbName).Name)
End Function


Biography
Malkit has not submitted biographical details.

Site Rank:  Not applicable - Current Winnings:  $0.00

Reply Reply Using Power Editor
Rolf Jaeger

Attempt to interpret Zack's Code

Rolf Jaeger replied to Malkit Sidhu

EggHeadCafe's human moderators scored this post for our messageboard contest.

Saturday, June 06, 2009 1:37:11 AM

Hi Malkit:

you should probably contact Zack directly with your question on how to interpret his code, but I will give it a try, as neutral party so to speak. I have worked with a lot of people with RF background in my past, so I understand how frustrated you must feel.

I have embedded my interpretation comments in Zack's code. I am not sure whether they will be much help.

Please don't hesitate to contact me with more questions. If you think a contractual arrangement in the context of a larger project might help I am doing this for a living now (although my background is in physics).

Best wishes,
Rolf
SoarentComputing
http://soarentcomputing.com
510.300.7462

 
Option Explicit
Sub TransferData()
    Dim wkb As Workbook, wks As Worksheet, LastRow As Long
    Dim FilePath As String, FileName As String
    Dim ws As Worksheet, blnOpened As Boolean
    'Change these variables as desired...
    FilePath = "C:\YourFullFilePathHere\"
    'change path here    FileName = "Book2.xls"
    'change name here    Call ToggleEvents(False)
    'Rolfs comment:
    ' I presume ws is intended to be the active worksheet in your workbook Book1
    Set ws = ThisWorkbook.Sheets("Sheet1")
    'change source sheet name here
    
    'Rolfs comments:
    ' I presume FileName is intended to be the name of your workbook Book2
    ' The next couple of lines are just making sure that the workbook is actually open

    If WbOpen(FileName) = True Then
        Set wkb = Workbooks(FileName)
        blnOpened = False
    Else
        If Right(FilePath, 1) <> Application.PathSeparator Then
            FilePath = FilePath & Application.PathSeparator
        End If
        Set wkb = Workbooks.Open(FilePath & FileName)
        blnOpened = True
    End If
    Set wks = wkb.Sheets("Sheet1")
    'change destination sheet name here
    
    'Rolfs comment:
    ' This line simply sets LastRow to the index of the first empty row
    LastRow = wks.Cells.Find(what:="*", after:=wks.Cells(1, 1), searchorder:=xlByRows, searchdirection:=xlPrevious).Row + 1
    'Rolfs comments:
    ' Here is where the copying of values from Book1 to Book2 is taking place.
    ' Let's for the moment for the sake of simplicity say that the value of the LastRow index is n.
    ' The next couple of lines will now make the following range copies:
    ' Cell "An" in Book1 to Cell "B1"
    ' Cell "Bn" in Book1 to Cell "B4"
    ' Cell "Cn" in Book1 to Cell "B7"
    ' Cell "Dn" in Book1 to Cell "E7"
    ' This seems a bit odd. Is that what you want?
    wks.Cells(LastRow, "A").Value = ws.Cells(1, "B").Value
    wks.Cells(LastRow, "B").Value = ws.Cells(4, "B").Value
    wks.Cells(LastRow, "C").Value = ws.Cells(7, "B").Value
    wks.Cells(LastRow, "D").Value = ws.Cells(7, "E").Value
    If blnOpened = True Then
        wkb.Close SaveChanges:=True
    End If
    If MsgBox("Clear values?", vbYesNo, "CLEAR?") = vbYes Then
        Call ClearData
    End If
    Call ToggleEvents(True)
End Sub

Biography
PhD in physics, Technical University Munich, Germany. Over 20 years of technical management experience at Hewlett-Packard and Agilent Technologies. Programming expertise: - VB.NET - C# - ASP.NET - Excel VBA

Site Rank:  9th place - Current Winnings:  $40.00

Reply Reply Using Power Editor

copy range from book1 to book2

Malkit Sidhu replied to Rolf Jaeger

Thursday, June 11, 2009 8:49:01 PM

I know that it has taken awhile for me to respond. But, Thank you Rolf. I have the prog working. Alot of trial and error. One day I am going to understand what I did. Agian THX

Malkit

Biography
Malkit has not submitted biographical details.

Site Rank:  Not applicable - Current Winnings:  $0.00

Reply Reply Using Power Editor
 

Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum








  $1000    Adam Houldsworth - $187  |  Jonathan VH - $139  |  Kirtan Patel - $117  |  F Cali - $116  |  Huggy Bear - $88  |  more Neado  |  Free Icons  |  Privacy  |   (c) 2010