finding first blank dell in specified range

samkeenan

New Member
Joined
Nov 19, 2012
Messages
7
I'm trying to write code to cut and paste a single selected cell into the first blank cell in a specified range (eg H4:H18) but I've been using (xlUp) which isn't returning the desired effect. I don't think (xlUp) is the solution to my problem, but I'm not sure how else to go about it?

Sub TeamA()
Dim TeamRange As Range, TeamNextBlank As Range
Set TeamRange = ActiveSheet.Range("H4:H18")
Set TeamNextBlank = ActiveSheet.Range("H19").End(xlUp)(2)
ActiveCell.Cut Destination:=TeamNextBlank
End Sub

A secondary problem is that if there are no blank cells in the range I need to exit the sub without pasting anything.
 
xlUp will first the last populated cell in that range. If you want to find the first unpopulated cell in H4:H18, you can go about it something like this:
Code:
    Dim myFirstBlankCell As Range
    
    If Range("H4") = "" Then
        Set myFirstBlankCell = Range("H4")
    Else
        Set myFirstBlankCell = Range("H3").End(xlDown).Offset(1, 0)
    End If
 
Upvote 0
xlUp will first the last populated cell in that range. If you want to find the first unpopulated cell in H4:H18, you can go about it something like this:
Code:
    Dim myFirstBlankCell As Range
    
    If Range("H4") = "" Then
        Set myFirstBlankCell = Range("H4")
    Else
        Set myFirstBlankCell = Range("H3").End(xlDown).Offset(1, 0)
    End If

Thanks a lot, that helps! Could I then use something like:
Code:
If myFirstBlankCell>("H18") Then End Sub
to exit without pasting if no blank cells are found in range?
 
Upvote 0
Sure, you could do it like this:
Code:
If myFirstBlankCell.Row > 18 Then Exit Sub
 
Upvote 0

Forum statistics

Threads
1,226,906
Messages
6,193,596
Members
453,810
Latest member
Gks77117

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