Application or object-defined error 1004

juliet_jia

New Member
Joined
Sep 1, 2011
Messages
7
Hi everybody,

I'm trying to loop through a column which consists of blocks of empty and text cells. I need to get the first and last cell of each blocks which have values in the cells. The (simplified) macro looks something like this:


For i = 1 To j
x(1) = 2
x(i) = Cells(x(i), 22).End(xlDown).row
Next i

But I get application or object-defined error. Can anyone tells what's wrong?

Thanks
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
The following code will iterate through all the blocks of data... your code for each block would go inside the For..Next loop and use the Range variable A to reference each block of text. My code calculates the first and last cell address (as you requested) and displays them in a MessagBox.

Code:
Sub DemoGetAreasOfData()
  Dim A As Range, FirstCell As String, LastCell As String, Addresses() As String
  Const DataColumn As String = "A"
  For Each A In Columns(DataColumn).SpecialCells(xlCellTypeConstants).Areas
    Addresses = Split(A.Address(0, 0) & ":" & A.Address(0, 0), ":")
    FirstCell = Addresses(0)
    LastCell = Addresses(1)
    MsgBox "First Cell Address: " & FirstCell & vbLf & "Last Cell Address: " & LastCell
  Next
End Sub
 
Upvote 0
Thanks Rick! Your way does give me the addresses....
But I'd like to use the way I had, if possible of course. As I need to get the FirstCell and LastCell value, plus the corresponding rows in other columns...also my mind works better with arrays.

Code:
For m = 1 To n
    
    For i = 1 To j
        x(1) = 2
        x(i) = Cells(x(i), 22).End(xlDown).row
        
        If x(i) <> "singapore" Then
            If x(i) Mod 2 = 0 Then
                v_port(m) = Cells(x(i), 22).Text  'these are the FirstCell in the text range
                v_TA(m) = Cells(x(i), 4).Text
            Else
                V_TD(m) = Cells(x(i), 4).Text
            End If
            
        End If

   Next i
Next m


Any way to make this work?
Many thanks!
 
Upvote 0
I could try to do it your way, but I think the approach might be more cumbersome. However, it order to even try, you will have to describe what your variable mean (their names are not very descriptive) and, in the case of n and j, what they are set to initially.
 
Upvote 0
to describe what your variable mean
I'm not so good at Dim variables specifically, anyway:

Code:
Dim i, j, m, n as integer
Dim x(), v_port(), v_ta(), v_tb() as variant


j = worksheets("sheet1").range("row_count").count
n= 100
meanwhile, I'm trying to implement your way in my code.....
 
Upvote 0
I'm not so good at Dim variables specifically
I didn't mean for you to show me your Dim statement, I meant for you to tell me the physical significance of the various variable. I have no idea what v_port or v_ta() is storing or why you are putting whatever into it. For instance, which is holding the first cell address, last cell address, etc.

Code:
Dim i, j, m, n as integer
But, as long as you did show me your code, the above Dim statement is not doing what I think you think it is doing. My guess is you think i, j, m and n are all being declared as Integers. They are not... only n is declared as an Integer, all the rest get declared as Variants. In VB, you must declare the data type for each variable individually. So, that Dim statement should read...

Code:
Dim i As Integer, j As Integer, m As Integer, n As Integer
 
Upvote 0
OK, I try to describe it....

The excel data consists, e.g.

V2:V10 empty V11:V15 = "New York", V16:V20 empty, V21:V39 = "London"...... 'note the height of the blocks is random

Column D: date+time

My goal is to get the contents in cells, in this case, V11,V15, V21, V39 and the corresponding timing in D11, D15, D21, D39.

In my code:
v_port(m) = V11,V15, V21, V39....
v_TA(m) = D11, D21.... i.e. Cells(FirstCell.Row,4)
v_TD(m) = D15, D39..... i.e. Cells(LastCell.Row,4)

so on.... I also need to pull out other corresponding data, some has to use if...then...

x(i) is an intermediate used as the row numbers...

Hope this is clear!

Also, thx for pointing out DIM
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,331
Members
452,907
Latest member
Roland Deschain

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