VBA Help

coliervile

Well-known Member
Joined
May 19, 2006
Messages
724
The following code is suppose to unload data from a VBA form into the first empty cell in column A and in columns A, B, E, H, K, and N...but it is not doing that. I'm racking my brain here and can't fiure out why...every have one of these days???

Thanks for looking and your help-
Charlie

Here is the header:

Excel Workbook
ABCDEFGHIJKLMNOPQR
1DateG$ Change% ChangeF$ Change% ChangeC$ Change% ChangeS$ Change% ChangeI$ Change% ChangeBestWorst
Funds


Last Row:

Excel Workbook
ABCDEFGHIJKLMNOPQR
409526-Apr-1113.61460.00110.000114.44360.03030.002116.38080.14570.009023.30910.21800.009421.46510.10710.0050SG
409627-Apr-1113.61580.00120.000114.4218(0.0218)(0.0015)16.48610.10530.006423.46100.15190.006521.75070.28560.0133IF
409728-Apr-1113.61690.00110.000114.45970.03790.002616.54590.05980.003623.52520.06420.002721.87920.12850.0059IG
409829-Apr-1113.61920.00230.000214.47510.01540.001116.58410.03820.002323.61750.09230.003921.94840.06920.0032SG
4099**(13.6192)(1.0000)*(14.4751)(1.0000)*(16.5841)(1.0000)*(23.6175)(1.0000)*(21.9484)(1.0000)GG
Funds


Form Coding:

Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Funds")

'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(0, 0).Row

'check for a date
If Trim(Me.txtDate.Value) = "" Then
Me.txtDate.SetFocus
MsgBox "Please enter date"
Exit Sub
End If

'copy the data to the database
ws.Cells(iRow, 0).Value = Me.txtDate.Value
ws.Cells(iRow, 1).Value = Me.txtG.Value
ws.Cells(iRow, 4).Value = Me.txtF.Value
ws.Cells(iRow, 7).Value = Me.txtC.Value
ws.Cells(iRow, 10).Value = Me.txtS.Value
ws.Cells(iRow, 13).Value = Me.txtI.Value

'clear the data
Me.txtDate.Value = ""
Me.txtG.Value = ""
Me.txtF.Value = ""
Me.txtC.Value = ""
Me.txtS.Value = ""
Me.txtI.Value = ""
Me.txtDate.SetFocus

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
I'm usng this coding to find the first empty cell in Column A (see below). It seem to select the first empy cell in Column A but, I get the following "Run-time error '1004'". When I Debug it this line is highlighted "ws.Cells(iRow, 0).Value = Me.txtDate.Value" and I can't figure this out now!!!

Charlie

If Application.WorksheetFunction.CountA("A:A") = 0 Then
[A1].Select
Else
On Error Resume Next
Columns(1).SpecialCells(xlCellTypeBlanks)(1, 1).Select
If Err <> 0 Then
On Error GoTo 0
[A65536].End(xlUp)(2, 1).Select
End If
On Error GoTo 0
End If
 
Upvote 0
Change 0 to 1 or "A".
Code:
Cells(iRow ,1)

You'll probably need to change the column for the rest of the Cells.

You can use the column number or the column letter(s).
I'd suggest you use the latter in this case so you don't need to work out the number.
 
Upvote 0
If the first empty cell in Column A (Date) was custom formatted as d-mmm-yy would this cause the run-time error since the form is trying to download it as "txtDate"? If this were the case how would I go about changing the user form vba to make it enter the date in the custom format?

Charlie
 
Upvote 0
Thank you Norie for your suggestion. I tried doing it both ways using the alphanumerics and numbers, A, B, E, H, and N AND 1, 2, 5, 8, 11, and 14. I get the same run-time error message.

'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtDate.Value
ws.Cells(iRow, 2).Value = Me.txtGFund.Value
ws.Cells(iRow, 5).Value = Me.txtFFund.Value
ws.Cells(iRow, 8).Value = Me.txtCFund.Value
ws.Cells(iRow, 11).Value = Me.txtSFund.Value
ws.Cells(iRow, 14).Value = Me.txtIFund.Value

Charlie
 
Upvote 0
Charlie

I'm a little confused.

In your first code you appear to be getting the value for iRow with one line of code but them you seen to say your doing it with a fairly large chunk of code.
 
Upvote 0
Norie I did change it. I was thinking that was the part of the coding that was the problem. I changed the iRow back to its original coding. After changing the coding back the data off of the user form does go into the proper columns but it puts the new data in the last row filled row (4098) instead of the first empty row (4099), in the example in post #1.

Charlie
 
Upvote 0
That's because the Offset you use when working out iRow is wrong.

It should be Offset(1, 0).
 
Upvote 0
Thank you Norie it seems to be working as I want it to. It gets frustrating when you can't see the problem and I'm the problem...ha ha ha.

Thanks again,
Charlie
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,713
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top