Using the UsedRange function within a Loop

jufglanville

New Member
Joined
Sep 11, 2017
Messages
23
I'm sure this is quite a simple question. I want to find a way to use the UsedRange function in conjunction with a loop.

Currently I am using the following code but I am having to manually input the value in Z to the bottom of the table. How do I replace this with the UsedRange function? I'm not able to go to the first cell that contains a blank as many cells within the range do!

Private Sub MergeDates2()


Dim i As Long

i = 1
Do While Cells(i, "Z").Value <> ""
If IsEmpty(Cells(i, "A")) = True Then
Cells(i, "A").Value = Cells(i, "C")

End If
i = i + 1
Loop

End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Are you trying to fill blank cells in col A with values in Col C until the last row of data?
 
Upvote 0
How about
Code:
Sub FillColA()

    Dim UsdRws As Long
    Dim Cnt As Long
    
    UsdRws = Cells.Find("*", After:=Range("A1"), SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row

    For Cnt = 1 To UsdRws
        With Range("A" & Cnt)
            If .Value = "" Then .Value = .Offset(, 2).Value
        End With
    Next Cnt

End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
That's worked perfectly, thanks!!
In case you might be interested, you can achieve the same results as Fluff's code without using any loops at all...
Code:
[table="width: 500"]
[tr]
	[td]Sub FillColA()
  Dim LastRow As Long
  LastRow = Cells.Find("*", , xlValues, , xlRows, xlPrevious).Row
  Range("A2:A" & LastRow) = Evaluate(Replace("IF(A2:A#="""",IF(C2:C#="""","""",C2:C#),A2:A#)", "#", LastRow))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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