简体   繁体   中英

Count number of rows from a specific row number using excel VBA

Below is a portion of my script which I use for copying data from my sheet RawImport to PullData . I have to copy all rows of some single columns from PullData worksheet, which contains data starting from row 8.

Dim lrA As Integer

lrA = RawImport.Range("B" & Rows.Count).End(xlUp).Row

PullData.Range("A2:A" & lrA).Value = RawImport.Range("G8:G" & lrA).Value
PullData.Range("A2:A" & lrA).NumberFormat = "d mmm yyyy h:mm;@"
PullData.Range("A:A").Columns.AutoFit

PullData.Range("B2:B" & lrA).Value = RawImport.Range("E8:E" & lrA).Value
PullData.Range("C2:C" & lrA).Value = RawImport.Range("C8:C" & lrA).Value
PullData.Range("D2:D" & lrA).Value = RawImport.Range("D8:E" & lrA).Value
PullData.Range("E2:E" & lrA).Value = RawImport.Range("B8:B" & lrA).Value
PullData.Range("F2:F" & lrA).Value = RawImport.Range("F8:F" & lrA).Value

The problem I face with above code is that it's including data from the first 8 rows as well. Thus the script is copying some values as N/A in the remaining rows at the end.

I tried to edit the line which counts the rows as follows:

 lrA = RawImport.Range("B" & Rows.Count).End(xlUp).Row - 7

But this removed 7 rows from the bottom of the range that contained data, instead of first 7.

How can insert data from row 8?

This is because you didn't specify the starting point of your range. Since you have to copy all rows, (I'm assuming you can copy-paste everything, not just the values) just use the following:

RawImport.Rows("8:" & lra).Copy Destination:=PullData.Range("A2")

This will copy all the rows from the 8th to the lra th in the PullData Worksheet, starting from range "A2", exactly like you copy-pasted with CTRL+C / CTRL+V

EDIT:

Ok I think I got what you were thinking (and edited your question accordingly). You can use .Copy anyway:

RawImport.Range(Cells(8, x).Address(0, 0), Cells(lra, x).Address(0, 0)).Copy _
Destination:=PullData.Range("A2") 'or whatever range you need

where x is in this case the column you wanted to select in your source worksheet.

Did you try the code like this:

Dim lrA As Integer

lrA = RawImport.Range("B" & Rows.Count).End(xlUp).Row

PullData.Range("A2:A" & lrA - 6).Value = RawImport.Range("G8:G" & lrA).Value
PullData.Range("A2:A" & lrA - 6).NumberFormat = "d mmm yyyy h:mm;@"
PullData.Range("A:A").Columns.AutoFit

PullData.Range("B2:B" & lrA - 6).Value = RawImport.Range("E8:E" & lrA).Value

PullData.Range("C2:C" & lrA - 6).Value = RawImport.Range("C8:C" & lrA).Value

PullData.Range("D2:D" & lrA - 6).Value = RawImport.Range("D8:E" & lrA).Value

PullData.Range("E2:E" & lrA - 6).Value = RawImport.Range("B8:B" & lrA).Value

PullData.Range("F2:F" & lrA - 6).Value = RawImport.Range("F8:F" & lrA).Value

Hope this help.

Your ranges differ, try subtracting 6 from lrA on the left-hand-side. Apply the change to all lines, where you copy data:

PullData.Range("A2:A" & lrA - 6).Value = RawImport.Range("G8:G" & lrA).Value

Also, I think this is unwanted:

PullData.Range("D2:D" & lrA).Value = RawImport.Range("D8:E" & lrA).Value

You copy columns D and E from RawImport into D in PullData .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM