473,804 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Excel VBA: ledger, dynamically creating button

118 New Member
Hi folks,

I have made a ledger sub in Excel. The task is to create a spreadsheet solution for a foreign exchange bureau so each transaction that happens it adds the details to a ledger sheet.

For this I have this code:

Expand|Select|Wrap|Line Numbers
  1. Sub ledge()
  2.     Dim r As Excel.Range
  3.     Dim i As Integer
  4.     Dim p As Excel.Range
  5.     Dim q As Excel.Range
  6.  
  7.  
  8.     If MsgBox("Are you sure you want to make this transaction?", _
  9.     vbYesNo, "System Message") = vbNo Then Exit Sub
  10.  
  11.   i = Worksheets("Home").Range("G64").Value
  12.  
  13.     Set r = Worksheets("ledger").Range("B1").End(xlDown).Offset(1).EntireRow.Cells
  14.     Set p = Worksheets("stock").Range("B1").End(xlDown).Offset(1).EntireRow.Cells
  15.  
  16.  
  17.     With Worksheets("Home")
  18.         If WorksheetFunction.CountA(r) <> 0 Then Exit Sub
  19.           If ((Worksheets("stock").Range("C9").Value - Worksheets("Home").Range("E25").Value) <= 0) Then
  20.        If MsgBox("The number of GBP in stock is too low to make this transaction. Do you want to order some more?", _
  21.     vbYesNo, "System Message") = vbYes Then
  22.     Worksheets("stock").Range("F22").Value = (10000 - Worksheets("stock").Range("C9").Value)
  23.         Worksheets("stock").Range("C9").Value = 10000
  24.             Else: MsgBox "Transaction Cancelled", vbOKOnly, "System Message"
  25.             Exit Sub
  26.     End If
  27.     End If
  28.         r(i, "B").Value = "FXB" & .Range("G64").Value - 3
  29.         r(i, "C").Value = Now()
  30.         r(i, "E").Value = "GBP"
  31.         r(i, "D").Value = .Range("E12").Value
  32.         r(i, "G").Value = .Range("E14").Value
  33.         r(i, "J").Value = .Range("E25").Value
  34.         r(i, "M").Value = .Range("E20").Value
  35.         r(i, "P").Value = .Range("E9").Value
  36.  
  37.     With Worksheets("Home")
  38.         If Worksheets("Home").Range("E12").Value = "USD" Then
  39.         p(3, "C").Value = p(3, "C").Value + .Range("E14").Value
  40.         p(2, "C").Value = p(2, "C").Value - .Range("E25").Value
  41.         p(15, "H").Value = p(15, "H").Value + .Range("E20").Value
  42.         p(15, "C").Value = p(15, "C").Value + .Range("E25").Value
  43.         p(16, "D").Value = p(16, "D").Value + .Range("E14").Value
  44.  
  45.         ElseIf Worksheets("Home").Range("E12").Value = "EUR" Then
  46.  
  47.         p(4, "C").Value = p(4, "C").Value + .Range("E14").Value
  48.         p(2, "C").Value = p(2, "C").Value - .Range("E25").Value
  49.         p(15, "H").Value = p(15, "H").Value + .Range("E20").Value
  50.         p(15, "C").Value = p(15, "C").Value + .Range("E25").Value
  51.         p(17, "D").Value = p(17, "D").Value + .Range("E14").Value
  52.  
  53.  
  54.         ElseIf Worksheets("Home").Range("E12").Value = "YEN" Then
  55.  
  56.         p(5, "C").Value = p(5, "C").Value + .Range("E14").Value
  57.         p(2, "C").Value = p(2, "C").Value - .Range("E25").Value
  58.         p(15, "H").Value = p(15, "H").Value + .Range("E20").Value
  59.         p(15, "C").Value = p(15, "C").Value + .Range("E25").Value
  60.         p(18, "D").Value = p(18, "D").Value + .Range("E14").Value
  61.  
  62.  
  63.         ElseIf Worksheets("Home").Range("E12").Value = "CAD" Then
  64.  
  65.         p(6, "C").Value = p(6, "C").Value + .Range("E14").Value
  66.         p(2, "C").Value = p(2, "C").Value - .Range("E25").Value
  67.         p(15, "H").Value = p(15, "H").Value + .Range("E20").Value
  68.         p(15, "C").Value = p(15, "C").Value + .Range("E25").Value
  69.         p(19, "D").Value = p(19, "D").Value + .Range("E14").Value
  70.  
  71.  
  72.         Else
  73.  
  74.         p(7, "C").Value = p(7, "C").Value + .Range("E14").Value
  75.         p(2, "C").Value = p(2, "C").Value - .Range("E25").Value
  76.         p(15, "H").Value = p(15, "H").Value + .Range("E20").Value
  77.         p(15, "C").Value = p(15, "C").Value + .Range("E25").Value
  78.         p(20, "D").Value = p(20, "D").Value + .Range("E14").Value
  79.  
  80.         End If
  81.  
  82.         MsgBox "The ledger and stock totals have been updated.", vbOKOnly, "System Message"
  83.  
  84.         Application.Goto Worksheets("Home").Range("A1")
  85.  
  86.  
  87.     Worksheets("Home").Range("G64").Value = Worksheets("Home").Range("G64").Value + 1
  88.  
  89.  
  90.  
  91.  
  92. End With
  93.     End With
  94.  
  95.      If Worksheets("stock").Range("C9").Value <= 1000 Then
  96.     If MsgBox("The number of GBP in stock is too low. Do you want to order some more?", _
  97.     vbYesNo, "System Message") = vbYes Then
  98.     Worksheets("stock").Range("F22").Value = (10000 - Worksheets("stock").Range("C9").Value)
  99.         Worksheets("stock").Range("C9").Value = 10000
  100.             Else: Exit Sub
  101.     End If
  102.     End If
  103.  
  104.     If Worksheets("stock").Range("C10").Value <= 1000 Then
  105.         If MsgBox("The number of USD in stock is too low. Do you want to order some more?", _
  106.     vbYesNo, "System Message") = vbYes Then
  107.     Worksheets("stock").Range("F23").Value = (10000 - Worksheets("stock").Range("C10").Value)
  108.         Worksheets("stock").Range("C10").Value = 10000
  109.     Else: Exit Sub
  110.     End If
  111.     End If
  112.  
  113.     If Worksheets("stock").Range("C11").Value <= 1000 Then
  114.     If MsgBox("The number of EUR in stock is too low. Do you want to order some more?", _
  115.     vbYesNo, "System Message") = vbYes Then
  116.     Worksheets("stock").Range("F24").Value = (10000 - Worksheets("stock").Range("C11").Value)
  117.         Worksheets("stock").Range("C11").Value = 10000
  118.     Else: Exit Sub
  119.     End If
  120.     End If
  121.  
  122.     If Worksheets("stock").Range("C12").Value <= 1000 Then
  123.     If MsgBox("The number of YEN in stock is too low. Do you want to order some more?", _
  124.     vbYesNo, "System Message") = vbYes Then
  125.     Worksheets("stock").Range("F25").Value = (10000 - Worksheets("stock").Range("C12").Value)
  126.         Worksheets("stock").Range("C12").Value = 10000
  127.     Else: Exit Sub
  128.     End If
  129.     End If
  130.  
  131.     If Worksheets("stock").Range("C13").Value <= 1000 Then
  132.     If MsgBox("The number of CAD in stock is too low. Do you want to order some more?", _
  133.     vbYesNo, "System Message") = vbYes Then
  134.     Worksheets("stock").Range("F26").Value = (10000 - Worksheets("stock").Range("C13").Value)
  135.         Worksheets("stock").Range("C13").Value = 10000
  136.     Else: Exit Sub
  137.     End If
  138.     End If
  139.  
  140.     If Worksheets("stock").Range("C14").Value <= 1000 Then
  141.     If MsgBox("The number of AUD in stock is too low. Do you want to order some more?", _
  142.     vbYesNo, "System Message") = vbYes Then
  143.     Worksheets("stock").Range("F27").Value = (10000 - Worksheets("stock").Range("C14").Value)
  144.         Worksheets("stock").Range("C14").Value = 10000
  145.     Else: Exit Sub
  146.     End If
  147.     End If
  148.  
  149. 'next step
  150.  
  151.     If Worksheets("stock").Range("C9").Value >= 15000 Then
  152.     If MsgBox("The number of GBP in stock is too high. Holding too much money can pose as a security risk! Do you wish to release some of these funds?", _
  153.     vbYesNo, "System Message") = vbYes Then
  154.     Worksheets("stock").Range("E22").Value = (Worksheets("stock").Range("C9").Value - 10000)
  155.         Worksheets("stock").Range("C9").Value = 10000
  156.     Else: Exit Sub
  157.     End If
  158.     End If
  159.  
  160.     If Worksheets("stock").Range("C10").Value >= 15000 Then
  161.  
  162.     If MsgBox("The number of USD in stock is too high. Holding too much money can pose as a security risk! Do you wish to release some of these funds?", _
  163.     vbYesNo, "System Message") = vbYes Then
  164.     Worksheets("stock").Range("E23").Value = (Worksheets("stock").Range("C10").Value - 10000)
  165.         Worksheets("stock").Range("C10").Value = 10000
  166.     Else: Exit Sub
  167.     End If
  168.     End If
  169.  
  170.     If Worksheets("stock").Range("C11").Value >= 15000 Then
  171.     If MsgBox("The number of EUR in stock is too high. Holding too much money can pose as a security risk! Do you wish to release some of these funds?", _
  172.     vbYesNo, "System Message") = vbYes Then
  173.     Worksheets("stock").Range("E24").Value = (Worksheets("stock").Range("C11").Value - 10000)
  174.         Worksheets("stock").Range("C11").Value = 10000
  175.     Else: Exit Sub
  176.    End If
  177.  
  178.     If Worksheets("stock").Range("C12").Value >= 15000 Then
  179.     If MsgBox("The number of YEN in stock is too high. Holding too much money can pose as a security risk! Do you wish to release some of these funds?", _
  180.     vbYesNo, "System Message") = vbYes Then
  181.     Worksheets("stock").Range("E25").Value = (Worksheets("stock").Range("C12").Value - 10000)
  182.         Worksheets("stock").Range("C12").Value = 10000
  183.     Else: Exit Sub
  184.    End If
  185.    End If
  186.  
  187.     If Worksheets("stock").Range("C13").Value >= 15000 Then
  188.     If MsgBox("The number of CAD in stock is too high. Holding too much money can pose as a security risk! Do you wish to release some of these funds?", _
  189.     vbYesNo, "System Message") = vbYes Then
  190.     Worksheets("stock").Range("E26").Value = (Worksheets("stock").Range("C13").Value - 10000)
  191.         Worksheets("stock").Range("C13").Value = 10000
  192.     Else: Exit Sub
  193.    End If
  194.    End If
  195.  
  196.     If Worksheets("stock").Range("C14").Value >= 15000 Then
  197.     If MsgBox("The number of AUD in stock is too high. Holding too much money can pose as a security risk! Do you wish to release some of these funds?", _
  198.     vbYesNo, "System Message") = vbYes Then
  199.     Worksheets("stock").Range("E27").Value = (Worksheets("stock").Range("C14").Value - 10000)
  200.         Worksheets("stock").Range("C14").Value = 10000
  201.     Else: Exit Sub
  202.     End If
  203.   End If
  204.    End If
  205.      If MsgBox("Would you like to view the receipt?", vbYesNo, "System Message") = vbYes Then
  206.     Set q = Worksheets("Receipt").Range("B1").End(xlDown).Offset(1).EntireRow.Cells
  207.  
  208.         With Worksheets("Home")
  209.         q(3, "F").Value = "FXB" & .Range("G64").Value - 4
  210.         q(8, "F").Value = .Range("E14").Value
  211.         q(10, "F").Value = .Range("E12").Value
  212.         q(13, "F").Value = .Range("E25").Value
  213.         q(15, "F").Value = "GBP"
  214.         q(17, "F").Value = .Range("E20").Value
  215.         q(43, "B").Value = "You were served by " & .Range("E9").Value
  216.         q(44, "B").Value = "You were served on " & Left(Now(), 11) & "at " & Right(Now(), 8)
  217.  
  218.         End With
  219.  
  220.      Application.Goto Worksheets("Receipt").Range("A1")
  221.  
  222.         Else
  223.           Exit Sub
  224.         End If
  225. End Sub
  226.  
I was thinking of adding a reload for the reciepts, so if the operator presses the button next to a transaction it'll load that data for the reciept.

The code for the reciept is easy, but how would I make it so that, like when it adds the new ledger line, it adds a button into the cell "R"+i (where 'i' is the new line each time) that when pressed loads the data from the same line into the reciept? It'd have to change i each time accordingly with the line it's on..

Any ideas?

Hope that was clear enough,
Sam
Apr 23 '08 #1
0 1474

Sign in to post your reply or Sign up for a free account.

Similar topics

5
4142
by: chris | last post by:
I'm creating an excel document dynamically from scratch using Python and the win32com module. All is well, but now I need to add a macro to the spreadsheet and run it (to enable some sorting features in the spreadsheet). I think I know how to run a macro once it's installed (using the Run method of the excel application object...I think), but I can't figure out how to "install" the VBA macro code into the spreadsheet to begin with from...
3
3005
by: user_5701 | last post by:
Hello, I have an Access 2000 database that I need to export certain queries to Excel 2000. The problem is that I have to take the toolbars away from the users for security purposes, but still let the users have the ability to export to Excel using forms and buttons, using vba code I write. I have attempted using the DoCmd.TransferSpreadsheet option, but it does not look formatted the way the "Analyze It with MS Excel" button does. To...
12
2435
by: D. Shane Fowlkes | last post by:
This most likely belongs in another forum but I thought I'd start here. I have a COM Object written in VB6. The DLL will access MS Excel and use it's Object Library to write a customized report and saves it to a folder. The DLL even writes to a log for each step it takes so we can troubleshoot the problems (if any). This works fine on one machine but not another. The folder the DLL is trying to write to has full permissions assigned...
10
8208
by: Steve | last post by:
I am trying to create a DLL in Visual Studio 2005-Visual Basic that contains custom functions. I believe I need to use COM interop to allow VBA code in Excel 2002 to access it. I've studied everything I can find on COM Interop and .NET. I've also tried many of the 'Walkthroughs' on the MSDN site relating to COM add-ins, .NET and Office XP but am unable to get even these working in Excel 2002 or Word 2002. I've installed the Office XP...
2
2489
by: kj | last post by:
Executive summary: Is there a way for the client side JavaScript to tell the browser to use MS Excel to view some data? The question: I'm changing an webapp from standard CGI to pure AJAX. I.e. only
16
5194
by: Phil Stanton | last post by:
I have a form with a button which is supposed to open an Excel file (With lots of Macros /VBA) in it. The Excel file gets it's data from the Access program Here is the code Private Sub Storage_Click() On Error GoTo Err_Storage_Click
1
1357
by: dhruvbhatt | last post by:
I am all new for vba excel project. i already have the code ready. I want to have a gui for this. meaning, all macros have been coded for the excel sheet. Now i want to build GUI for which there would be certain inputs and based on that excel sheet would be generated. So how do i do that. what should i use for creating gui? Like, i would like to have a button which would in turn run a macro on click event. Which is the best tool for...
1
6343
by: JFKJr | last post by:
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using the following "DateTextBox" class module). Whenever a user enters in to the textbox, the code displays "Please enter date in mm/dd/yyyy format (for ex: 01/01/2009)." message which is accomplished using "MouseUp" event. And, I used "Exit" event to...
8
5355
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a web site that automates excel. This site runs in server 2003. iis 6.0 office 2003 installed I moved this app to server 2008 iis 7.0 office 2003 installed Now when I try to automate excel I get the error Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005. System.UnauthorizedAccessException: Retrieving the COM class factory for
0
9704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10318
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10302
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9132
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7608
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.