VB 6.0 - Generating Invoices

Asked By Malkit Sidhu
01-Jun-09 01:05 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

A Suggestion  A Suggestion

01-Jun-09 03:07 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

Copying book1 cellc to book2  Copying book1 cellc to book2

03-Jun-09 12:07 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
http://www.mrexcel.com/forum/member.php?u=22953
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.


http://www.mrexcel.com/forum/member.php?u=22953: 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

Attempt to interpret Zack's Code  Attempt to interpret Zack's Code

06-Jun-09 01:37 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
copy range from book1 to book2  copy range from book1 to book2
11-Jun-09 08:49 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
Create New Account
help
VBA question: Sheets() vs. Worksheets()? Excel Are these the same functions? Excel Discussions Worksheets (1) LN (1) Sheets (1) Excel (1) Name (1) Sheets can include chart sheets. Worksheets no - - Don Guillett SalesAid Software dguillett1@austin.rr.com So
Export multiple datatables from a C# form to Multiple Excel sheets Excel How do I export multiple datatables from a C# form to Multiple Excel sheets using a datagrid with htmlwriter so it is simple, fast and efficient but can do it to mulitple worksheets. right now that method export html but all on one spreadsheet Thanks, - - TRBG Excel Programming Discussions Myworkbook.Sheets.Count (1) Sheets (1) Excel (1) MyWorkbook.Sheets (1) Worksheet (1) Workbook (1) SheetCount (1
Opening and retrieving information from numerous excel sheets Excel I am currently trying to do work with numerous different excel sheets. I am looking for the user to open a blank Excel worksheet and click a button. This button will make a dialogue box appear and ask which worksheets the user would like to open. After this task is done, I would like to a cell from each sheet opened (for example cell A1). How would I do this? Excel Programming Discussions SQL Server 2000 (1) Office 2003 (1) Office (1) Excel (1) Worksheet (1
E-Mail Excel Worksheets Excel Is there a way to e-mail just one sheet in a workbook that contains multiple sheets? Thanks, Debra Lassman Excel Discussions Worksheets (1) Excel (1) Workbook (1) Lassman (1) I think Ron de Bruin has code that would assist you. http: / / www.rondebruin.nl / sendmail.htm HTH, JP le keywords: E-Mail, Excel, Worksheets description: Is there a way to e-mail just one sheet in a workbook
How to "freeze" all cells in Excel sheets? Excel Hi all, I currently save copy of my Excel sheets every day, for historical records. However because some of the cells are connected to live data sources, and other cells are linked to other time-varying sheets, the copying of Excel sheets as record does not really work as it is supposed to. The reason is that