VB Macro - Put data in first empty cell in column not row

MatthewSeven

New Member
Joined
Mar 5, 2014
Messages
7
Hey Guys!

I have the following code to add data to a particular column but it looks for an empty row rather than looking for an empty column cell which is resulting in data being overwritten. Been searching for ages and get the same code for just empty row, Any fixes guys?



Private Sub CommandButton1_Click()


Dim LastRow As Long


LastRow = Worksheets("Totals").Range("A65536").End(xlUp).Row + 1
Worksheets("Totals").Range("DW" & LastRow).Value = TextBox1.Text
Worksheets("Totals").Range("DX" & LastRow).Value = Date
Worksheets("Totals").Range("DY" & LastRow).Value = Format(Now, "HH:mm")
MsgBox "Added"


Unload Me


End Sub

Thanks, Matthew :)
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Maybe try

Code:
Private Sub CommandButton1_Click()


Dim LR As Long, LC As Long
LR = Worksheets("Totals").Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByRows).Row
LC = Worksheets("Totals").Cells.Find(What:="*", SearchDirection:=xlPrevious, SearchOrder:=xlByColumns).Column
Worksheets("Totals").Cells(LR + 1, LC + 1).Value = TextBox1.Text
Worksheets("Totals").Cells(LR + 1, LC + 2).Value = Date
Worksheets("Totals").Cells(LR + 1, LC + 3).Value = Format(Now, "HH:mm")
MsgBox "Added"


Unload Me


End Sub
 
Upvote 0
Mathew,

it is a little unclear of what you want.

Do you want the data inputed in Col "DW" as-

[TABLE="class: grid, width: 500"]
<TBODY>[TR]
[TD]Last row
[/TD]
[TD]text box
[/TD]
[/TR]
[TR]
[TD]Last row +1
[/TD]
[TD]date
[/TD]
[/TR]
[TR]
[TD]last row +2
[/TD]
[TD]time
[/TD]
[/TR]
</TBODY>[/TABLE]




FarmerScott
 
Upvote 0
Mathew,

it is a little unclear of what you want.

Do you want the data inputed in Col "DW" as-

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Last row[/TD]
[TD]text box[/TD]
[/TR]
[TR]
[TD]Last row +1[/TD]
[TD]date[/TD]
[/TR]
[TR]
[TD]last row +2[/TD]
[TD]time[/TD]
[/TR]
</tbody>[/TABLE]




FarmerScott

Hey Scott!

Well the text box data is to go into column DW
Date to go in DX
Time into DY

I just need the values to go into the next empty cell in the column rather than it seeing if there is an empty row. At the minute if there is an empty row and data is in DW,DX,DY the macro will overwrite it instead of writing to the next empty cell in column.

Hope u kinda get what i mean lol
Thanks, Matthew
 
Upvote 0
Mathew,

that explains alot.

Have a look at Col A. Does the last cell in Col "A" match the last used cell in Col "DW"? Your code

Code:
LastRow = Worksheets("Totals").Range("A65536").End(xlUp).Row +1

finds the last used cell in Col A and then adds 1 row to find the first empty cell to put the data in. Thus if Col "A" and Col "DW" do not have the same last cell then your code will overwrite the existing data.

Try

Code:
LastRow = Worksheets("Totals").Range("DW65536").End(xlUp).Row +1

This will find the last used cell in Col "DW" and add 1 row.

Are you using Excel 2003?

Hope that helps,

FarmerScott
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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