Loop to check for empty cell and retrieve row nb

vadius

Board Regular
Joined
Jul 5, 2011
Messages
70
Hi there,

I am extracting dates from one sheet to another.
As shown on the code below I created a king of loop that reads the content of a cell in column A, every 26 rows as the data I want is stored there.

Code :
=====================================================
Sub copydate()

Dim strValue As String
Dim strCellNum As String
Dim cleandate As String
Dim x As String
x = 2

For i = 2 To 6000 Step 26
strCellNum = "A" & i
strValue = Worksheets("Extract").Range(strCellNum).Value
Debug.Print strValue
Worksheets("Cleanprice").Range("A" & x).Value = Format(CDate(Right(strValue, 10)), "dd/mmm/yy")
x = x + 1
Next

End Sub

=====================================================

In the sheet where I have the row data, the last cell I want to retrieve the data is the 5436th row. The problem is that row data are regularly updated, so this number will change. If, in my code, I don't put exactly 5436 as the upper limit (for i=2 to X...) I have an error message.


So I would like to create a macro that reads the cells of column A (every 26 rows) and checks whether or not the cell is blank. If yes, I want the macro to give me the row number of the "last 26th cell" where I can find data. So that I can replace the upper limit in the code and don't have any error message.
I think it might be possible to do this with a kind of while ( while the 26th cell of all my loops is not empty, then extract the value. Else stop and the row number) but I don't how to put that in the orght way.

I hope it's clear.

Thanks
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Does this work


Sub copydate()

Dim strValue As String
Dim strCellNum As String
Dim cleandate As String
Dim x As String
x = 2

For i = 2 To 6000 Step 26
if cells(i,1) <> "" then
strCellNum = "A" & i
strValue = Worksheets("Extract").Range(strCellNum).Value
Debug.Print strValue
Worksheets("Cleanprice").Range("A" & x).Value = Format(CDate(Right(strValue, 10)), "dd/mmm/yy")
x = x + 1
end if

Next

End Sub
 
Upvote 0
Actually may be better to do

Sub copydate()

Dim strValue As String
Dim strCellNum As String
Dim cleandate As String
Dim x As String
x = 2

For i = 2 To 6000 Step 26
if cells(i,1)="" then exit sub
strCellNum = "A" & i
strValue = Worksheets("Extract").Range(strCellNum).Value
Debug.Print strValue
Worksheets("Cleanprice").Range("A" & x).Value = Format(CDate(Right(strValue, 10)), "dd/mmm/yy")
x = x + 1
Next

End Sub
 
Upvote 0
You are looking last data filled row. Aren't you?
Code:
Dim lLastRow As Long
lLastRow = Range("A" & Rows.Count).End(xlUp).Row
 
Upvote 0
Hi

thanks. I am actually looking for the row number of the row where I can find data. Basically I created a loop to get the data of every 26 rows. So it checks Cell(A,2), then Cell(A,28) etc,.. until cell(A,5436) which is the last cell where I have data within the loop. Of course I have data row 5437, 5438,, but not anymore row number 5436+26, which is empty.

I want the macro to retrive the number 5436.

I sthat more clear ?

Cheers
 
Upvote 0
I am still not sure but may be this:
Code:
Dim lLastRow As Long
lLastRow = Range("A" & Rows.Count).End(xlUp).Row + 1
If ((lLastRow - 2) Mod 26) = 0 Then
lLastRow = lLastRow
Else
lLastRow = lLastRow - ((lLastRow - 2) Mod 26)
End If
 
Upvote 0

Forum statistics

Threads
1,224,544
Messages
6,179,430
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