ryancgarrett
Board Regular
- Joined
- Jun 18, 2011
- Messages
- 122
I am a VBA novice and I'm trying to clean up some code in a program I wrote. The program is basically some rudimentary accounting software for a basic business if that helps at all.
I have a form to add a new account and when I do the following things need to happen:
The hidden "Account Template" sheet should be copied and pasted after the first sheet in the workbook.
The sheet should be renamed to the name given on the form.
The name of the account should be added in cell B2 of the new sheet.
The name of the account should be added to the "Trial Balance" sheet at the end of the list of accounts.
Here is the code that I have, which only works some of the time but not others for some reason:
Sub AddAccount()
Sheets("Account Template").Copy after:=Sheets("Journal")
Sheets(2).Select
Sheets(2).Name = frmAddAccount.lbxAccountName.Value
Sheets(2).Cells(2, 2).Value = frmAddAccount.lbxAccountName.Value
Sheets(2).Visible = True
Sheets("Trial Balance").Range("B" & BlankRow("Trial Balance", 4, 2)).Value = frmAddAccount.lbxAccountName.Value
CloseAddAccount
End Sub
Function BlankRow(sName As String, sRow As Integer, sCol As Integer) As Integer
Do Until Sheets(sName).Cells(sRow, sCol).Value = Empty
sRow = sRow + 1
Loop
BlankRow = sRow
End Function
Can this be cleaned up and does anyone know why it only works some of the time? As well, when other people post I notice they format their code differently on here, how is that done? Sorry I'm such a noob!
I have a form to add a new account and when I do the following things need to happen:
The hidden "Account Template" sheet should be copied and pasted after the first sheet in the workbook.
The sheet should be renamed to the name given on the form.
The name of the account should be added in cell B2 of the new sheet.
The name of the account should be added to the "Trial Balance" sheet at the end of the list of accounts.
Here is the code that I have, which only works some of the time but not others for some reason:
Sub AddAccount()
Sheets("Account Template").Copy after:=Sheets("Journal")
Sheets(2).Select
Sheets(2).Name = frmAddAccount.lbxAccountName.Value
Sheets(2).Cells(2, 2).Value = frmAddAccount.lbxAccountName.Value
Sheets(2).Visible = True
Sheets("Trial Balance").Range("B" & BlankRow("Trial Balance", 4, 2)).Value = frmAddAccount.lbxAccountName.Value
CloseAddAccount
End Sub
Function BlankRow(sName As String, sRow As Integer, sCol As Integer) As Integer
Do Until Sheets(sName).Cells(sRow, sCol).Value = Empty
sRow = sRow + 1
Loop
BlankRow = sRow
End Function
Can this be cleaned up and does anyone know why it only works some of the time? As well, when other people post I notice they format their code differently on here, how is that done? Sorry I'm such a noob!