What is wrong with this code

MAMIBUSSOL

Board Regular
Joined
Jun 2, 2011
Messages
95
for info:

I have a selection of transactions which I applied a filter. using the filter below I am selecting all transaction which have the code "REPAIR", I am then selecting just these records and transferring them to a new sheet called TRIAL. I need to run the process 3 times using firstly sheet CRED1, then CRED2 and finally CRED3, the data needs to be started in TRIAL in cell A3, transfer the data, on the next run it needs to put the data in the first blank row. which is why I am attempting to use the rows.count

Code:
    Range("A" & .Rows.Count).End(xlUp).Offset (1)

i hope someone can help me as this has me stumped

Code:
Sub main()
    Sheets.Add(AFTER:=Sheets(11)).Name = "TRIAL"
    ActiveWorkbook.Worksheets("CRED1").Select
    Call COPYPHASE
    ActiveWorkbook.Worksheets("CRED2").Select
    Call COPYPHASE
    ActiveWorkbook.Worksheets("CRED3").Select
    Call COPYPHASE
End Sub
Sub COPYPHASE()
    ActiveSheet.Range("$B$1:$IE$388").AutoFilter FIELD:=3, Criteria1:="=REPAIRS"
    ActiveSheet.Range("$A$3:$IE$388").Copy
    Sheets("TRIAL").Select
    Range("A" & .Rows.Count).End(xlUp).Offset (1)
    ActiveSheet.Paste
End Sub
 

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.
Try like this

Code:
Sub main()
    Dim i As Long
    Sheets.Add(AFTER:=Sheets(11)).Name = "TRIAL"
    For i = 1 To 3
        With ActiveWorkbook.Worksheets("CRED" & i)
            .Range("$B$1:$IE$388").AutoFilter FIELD:=3, Criteria1:="=REPAIRS"
            .Range("$A$3:$IE$388").Copy Sheets("TRIAL").Range("A" & .Rows.Count).End(xlUp).Offset(1)
        End With
    Next i
End Sub
 
Upvote 0
Dave,

Shouldn't this be

.Range("$A$3:$IE$388").Copy Sheets("TRIAL").Range("A" & Sheets("TRIAL").Rows.Count).End(xlUp).Offset(1)
 
Upvote 0
Thanks for the correction Sandeep.

;)

Dave
 
Upvote 0
Isn't Rows.Count the same for each worksheet in the same workbook?:)
 
Upvote 0
It is...but I've come across many errors on not qualifying it while using With/End With.... :) ... better to be safe :)
 
Upvote 0
The code could be written as

Code:
Sub main()
    Dim i As Long
    Sheets.Add(AFTER:=Sheets(11)).Name = "TRIAL"
    For i = 1 To 3
        With ActiveWorkbook.Worksheets("CRED" & i)
            .Range("$B$1:$IE$388").AutoFilter FIELD:=3, Criteria1:="=REPAIRS"
            .Range("$A$3:$IE$388").Copy Sheets("TRIAL").Range("A" & Rows.Count).End(xlUp).Offset(1)
        End With
    Next i
End Sub

Note the removal of the period.
 
Upvote 0
Sandeep

It is qualified, to the worksheet ActiveWorkbook.Worksheets("CRED" & I) via the With.:)

You are right thought, better to be safe.
 
Upvote 0
It is qualified, to the worksheet ActiveWorkbook.Worksheets("CRED" & I) via the With.:)


Yeah... I know....

I've still seen errors arise in such cases... (havent been able to explain why)... which disappear on using, for example, sheets("xyz").rows.count... etc.:)
 
Upvote 0
Strange.

The only problems I have had are when I've been automating Excel from another application.

That's just me being sloppy though.:)
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,735
Members
452,939
Latest member
WCrawford

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