Macro to Find Last Row in a Column and Add Letter

Michael151

Board Regular
Joined
Sep 20, 2010
Messages
247
Hello all,

Trying to write a macro that will add the letter "X" in the first blank row of a column.

In the column marked "Goods" (header in row 1), the macro should find the first empty row and add an X. For example:

Before:

Goods
data1
data2
data3
data4

After:

Goods
data1
data2
data3
data4
X

"Goods" can be in any column letter, which is why I'd like to use the header in row 1 to identify. Thanks!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Sub Test

dim C as integer, R as long 'C for column, R for row

with thisworkbook.sheets("Sheet1") 'edit to suit

c = .rows(1).find("Goods", , xlvalues, xlwhole).column

r = .cells(1048576, c).end(xlup).row + 1 'use 65536 if you are in XL03 or before

.cells(r,c).value = "X"

end with

End Sub
 
Upvote 0
Thanks Airfix9, that worked great!

Was also wondering if there was a way to write a reverse of this - that is, if the macro finds an "X" in the "Goods" column, then delete that row.

For example:

Before:

Goods
title1
title2
title3
X

After:

Goods
title1
title2
title3

The macro would delete the row with the X in it.

Thanks for your help!
 
Upvote 0
Do you want to delete the row? Or just remove the X?

Sub Test

dim C as integer, R as long 'C for column, R for row

with thisworkbook.sheets("Sheet1") 'edit to suit

c = .rows(1).find("Goods", , xlvalues, xlwhole).column

r = columns(c).find("X", , xlvalues, xlwhole).row

' now you have a choice:

' this clears the X

.cells(r,c).clearcontents

' this deletes the row

.rows(r).entirerow.delete

' Delete whichever line you don't want

end with

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,547
Messages
6,179,436
Members
452,915
Latest member
hannnahheileen

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