With statement not working unless sheet is active

DanGK1

Board Regular
Joined
Jul 7, 2010
Messages
60
Hi

This is boggling me. Unless I make the sheets active this code does not work and I get run-time error 1004 Application-defined or object-defined error
Code:
Sub copysubclientsandsplit()
'
With Worksheets("Sub-Client Config")
    .range(range("A1"), range("A1").Offset(0, 2).End(xlDown)).Copy
End With
With Worksheets("Sub-Client Info")
    .range("A5").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    .range(range("A5"), range("A5").End(xlDown)).TextToColumns Destination:=range("A5"), DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
        :="|", FieldInfo:=Array(Array(1, 2), Array(2, 2)), TrailingMinusNumbers:=True
     .Borders.LineStyle = xlContinuous
End With
End Sub

Any suggestions please?

Many thanks
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
When using syntax like
Range(Range(...), Range(...))

And you qualify the sheet, you must qualify the sheet on all 3 parts of the range.
Sheets(...).Range(Sheets(...).Range(...), Sheets(...).Range(...))
 
Upvote 0
Since I am a beginner in vba, i would like to know what is a easy alternative to that.
 
Upvote 0
I'm using Excel 2007 that provides the maximum of 1048576 rows.
Your macro is asking Excel to copy all 1048576 rows, then move all of them down 5 rows and paste.

In essence you are wanting Excel to create 5 new additional rows that it doesn't have.
 
Upvote 0
Since I am a beginner in vba, i would like to know what is a easy alternative to that.
The With structure is a nice alternative, as the OP was trying to do.
But needed to add the . to both ranges inside the original range.

Code:
With Worksheets("Sub-Client Config")
   [COLOR=#FF0000][B] .[/B][/COLOR]range(range("A1"), range("A1").Offset(0, 2).End(xlDown)).Copy
End With
Should be
Code:
With Worksheets("Sub-Client Config")
   [COLOR=#FF0000][B] .[/B][/COLOR]range([B][COLOR=#ff0000].[/COLOR][/B]range("A1"), [B][COLOR=#ff0000].[/COLOR][/B]range("A1").Offset(0, 2).End(xlDown)).Copy
End With
 
Upvote 0
Your macro is asking Excel to copy all 1048576 rows
Only if there is no data in column C
If there is data populated in C1 and below, then the end(xldown) will only go to the last cell before an empty cell.
But if there is no data below C1, then yes, it goes all the way to the end.
 
Upvote 0
Thanks Jonmo and thanks Logit for the observation - I need to add an 'if' row count = 1 to the code to avoid the whole of column C being selected
 
Upvote 0
You're welcome.

Alot of people prefer to use LastRow and end(xlUP) to deal with this issue.


That would be like this
.range(.range("A1"), .range("A" & Rows.Count).Offset(0, 2).End(xlUp)).Copy


Keep in mind, it's not quite exactly the same.

If you were intentionally using row 1 DOWN because you have blanks mixed in and only want to get data down to the first blank,
Then you're original is more appropriate.

Using LastRow UP goes to the very last (furthest down) non blank cell in the column.
 
Upvote 0
Please post your final working macro so others will benefit in the future.

Thanks.
 
Upvote 0
thanks for the info Jomno, I now understand that this .range(.range(A1") format is used when the last row is not defined.
i think defining the lastrow earlier simplifies the code. am I correct ?
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

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