Hi all,
Being new to VBA I have spent the last few days trying to name 43 columns of my spreadsheet as ranges then adding data to the lastrow.
I nearly posted yesterday looking for the entire code, but with a fresh head today I decided to have another stab, utilising a "Teach VBA App" on the phone and good old google.
The spreadsheet combines with a Userform and is basically for keeping my financial accounts on, the idea is that I can open up the userform, choose from a couple of comboboxes where I will be presented with a selection of textboxes that I can add data to, ie dates, figures, receipt numbers, etc and with the click of an "Add" button the data in the textboxes is transferred into the lastrow of the corresponding named column range of the sheet.
So far I have managed to put together the VBA to name each column (range) according to the contents of the cell at row 1, the accounts data actually starts at row 10 and column "A" is the only column that always has data (Date of transaction):
This works perfectly (although being a VBA newbie, I'm not sure if it's the best method), coincidentally, the reason for wanting named columns is because if I ever add in a column to the spreadsheet the data from the Userform will still go to its' respective column. I did originally have each UserForm TextBox coded to enter into a column, but found that if I added a column, it would all need rewriting.
Anyway, as mentioned, the column/range naming has gone to plan, the problem I am having now is getting the data from the Userform over and into the lastrow of its range. I am currently testing with a button on the sheet as opposed to using the UserForm, this following code goes in at the end of the sub above.
This places "Test Date" into the Range.("Date") column, but to far down the sheet, currently Row 17, clicking the button again places "Test Date" in row 25, these entries should be in rows 10 and 11. I think the error is in the "lastrow" code, specifically Cells(Rows.Count, "A"), which at first entry is counting 8 rows, this is then repeating itself every time an entry is made, but I'm not sure.
Any help on this issue would be hugely appreciated, I've thought about tweaking it in places but after the last few days of headache - this is the closest I've got and don't want to mess it up. Also, if there are any suggestions on tidying up what I currently have or even if someone a little more knowledgeable than me has 10 minutes to throw together a dozen or so lines of code that do the above but better, that would be fantastic.
Thanks all in advance, Wayne
Being new to VBA I have spent the last few days trying to name 43 columns of my spreadsheet as ranges then adding data to the lastrow.
I nearly posted yesterday looking for the entire code, but with a fresh head today I decided to have another stab, utilising a "Teach VBA App" on the phone and good old google.
The spreadsheet combines with a Userform and is basically for keeping my financial accounts on, the idea is that I can open up the userform, choose from a couple of comboboxes where I will be presented with a selection of textboxes that I can add data to, ie dates, figures, receipt numbers, etc and with the click of an "Add" button the data in the textboxes is transferred into the lastrow of the corresponding named column range of the sheet.
So far I have managed to put together the VBA to name each column (range) according to the contents of the cell at row 1, the accounts data actually starts at row 10 and column "A" is the only column that always has data (Date of transaction):
Code:
Private Sub CommandButton1_Click()
Dim ws As Worksheet, i As Integer, lastrow As Long
Set ws = ThisWorkbook.Sheets("Accounts")
i = 1
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
Do While ws.Cells(1, i).Value <> ""
If ws.Cells(1, i).Value <> "" Then
Range(Cells(9, i), Cells(lastrow + 1, i)).Select
ws.Names.Add Name:=ws.Cells(1, i).Value, RefersTo:=Selection
End If
i = i + 1
Loop
End Sub
This works perfectly (although being a VBA newbie, I'm not sure if it's the best method), coincidentally, the reason for wanting named columns is because if I ever add in a column to the spreadsheet the data from the Userform will still go to its' respective column. I did originally have each UserForm TextBox coded to enter into a column, but found that if I added a column, it would all need rewriting.
Anyway, as mentioned, the column/range naming has gone to plan, the problem I am having now is getting the data from the Userform over and into the lastrow of its range. I am currently testing with a button on the sheet as opposed to using the UserForm, this following code goes in at the end of the sub above.
Code:
..... i + 1
Loop
ws.Range("Date").Cells(lastrow).Value = "Test Date"
End Sub
This places "Test Date" into the Range.("Date") column, but to far down the sheet, currently Row 17, clicking the button again places "Test Date" in row 25, these entries should be in rows 10 and 11. I think the error is in the "lastrow" code, specifically Cells(Rows.Count, "A"), which at first entry is counting 8 rows, this is then repeating itself every time an entry is made, but I'm not sure.
Any help on this issue would be hugely appreciated, I've thought about tweaking it in places but after the last few days of headache - this is the closest I've got and don't want to mess it up. Also, if there are any suggestions on tidying up what I currently have or even if someone a little more knowledgeable than me has 10 minutes to throw together a dozen or so lines of code that do the above but better, that would be fantastic.
Thanks all in advance, Wayne
Last edited: