Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8919637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:04:57+00:00 2026-06-15T06:04:57+00:00

I am trying to copy data starting at a row with cell with value

  • 0

I am trying to copy data starting at a row with cell with value of “Depth (m)” and ending at the next blank row. The number of rows between these will vary and I have 400 spreadsheets to do this on.

The error:

Run-time error '1004':
Application-defined or object-defined error

Here is my code:

Sub CopyDepth()
   Dim rownum As Long

   Dim startrow As Long
   Dim endrow As Long
   Dim lastrow As Long
   rownum = 1
   colnum = 1
   lastrow = Worksheets("Profile").Range("A65536").End(xlUp).Row
   With ActiveWorkbook.Worksheets("Profile").Range("a1:a" & lastrow)

   For rownum = 1 To lastrow
    Do
       If .Cells(rownum, 1).Value = "Depth (m)" Then
          startrow = rownum
       End If

       rownum = rownum + 1

   If (rownum > lastrow) Then Exit For

   Loop Until .Cells(rownum, 1).Value = ""
   endrow = rownum
   rownum = rownum + 1

   Worksheets("Profile").Range(startrow & ":" & endrow).Copy

   Sheets("data").Select
   Range("A1").Select
   ActiveSheet.Paste

   Next rownum
   End With
   End Sub

It works if change this line:

Loop Until .Cells(rownum, 1).Value = ""

to this:

Loop Until .Cells(rownum, 1).Value = "Doe (Jane, corrected):"

Unfortunately, I can’t use that because that text will vary as well from workbook to workbook.

Here I a sample of my data:

Big Lake
29 October, 2012
Joe & Jill
Blue [Big] Lake, Utah (USA)

OLD meter (#00314)
Depth (m)   T (deg-C)
0   31.21
1   32.64
2   34.70
3   36.76
4   36.92
5   36.92
6   36.12
7   35.47
8   35.05
9   34.32
10  33.96

Doe (Jane, corrected):
N: 8.0m
S: 8.0m

What I really want to copy is from Depth (m) down to 10 33.96 to a new sheet. I have been picking at this for a while but can’t get it. The debugger points to this line:

Worksheets("Profile").Range(startrow & ":" & endrow).Copy

Any assistance would be great. Thanks you! -susan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:04:57+00:00Added an answer on June 15, 2026 at 6:04 am

    You’re only referencing row numbers in this line:

    Worksheets("Profile").Range(startrow & ":" & endrow).Copy
    

    Try changing that to:

    Worksheets("Profile").Range("A" & startrow & ":B" & endrow).Copy
    

    Change the column letters to the appropriate columns for your requirements.

    EDIT:
    Correct you are, the answer I gave before was an unnecessary change. What was happening is you were referencing the startrow variable before instantiating it because your Do ... Loop would only run until the first blank line (line 5), whereas the “Depth” test didn’t occur until line 7 (thus, the startrow = rownum line was never run). So, in the Copy action, you were telling it to copy starting at Row 0. You can fix this by correcting the placement of your Next rownum statement like so:

    Sub CopyDepth()
       Dim rownum As Long
    
       Dim startrow As Long
       Dim endrow As Long
       Dim lastrow As Long
       rownum = 1
       colnum = 1
       lastrow = Worksheets("Profile").Range("A65536").End(xlUp).Row
       With ActiveWorkbook.Worksheets("Profile").Range("a1:a" & lastrow)
    
       For rownum = 1 To lastrow
         Do
           If .Cells(rownum, 1).Value = "Depth (m)" Then
              startrow = rownum
           End If
    
           rownum = rownum + 1
    
           If (rownum > lastrow) Then Exit For
    
         Loop Until .Cells(rownum, 1).Value = ""
         endrow = rownum
         rownum = rownum + 1
       Next rownum
    
       Worksheets("Profile").Range(startrow & ":" & endrow).Copy
       Worksheets("data").Range("A1").PasteSpecial
       End With
    End Sub
    

    EDIT 2:

    Sub CopyDepth()
        Dim ws As Worksheet
        Dim rng As Range
        Set ws = Worksheets("Profile")
        Set rng = ws.UsedRange.Find(What:="Depth (m)")
        Range(rng, rng.End(xlToRight).End(xlDown)).Copy
        Worksheets("data").Range("A1").PasteSpecial
    End Sub
    

    As long as the blank row (in this example in row 19) is truly blank, this should do the job.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to copy data only between two SQL server 2008 databases. I need
I'm currently trying to copy data from table into another by using a SELECT
I am trying to copy data from my MYSQL table to SQL Server using
I'm trying to copy data from an array of character that send from main
I am trying to copy a header and a set of data to a
I am trying to return a new copy of the data in a C++
I'm trying to copy data into a unsigned char buffer within an array of
I'm trying to copy data from MySQL to SQL Server 2008. My SSIS is
I'm trying to copy vertex data from a texture to a vertex buffer, and
I've trying to get byte-array and copy from there to another array some rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.