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
 
i think defining the lastrow earlier simplifies the code. am I correct ?
I guess that depends on your opinion of simple.
There is always more than 1 way to skin a cat, each accomplishes the task desired.
Which way is 'simpler' is subject to opinion and varies from one person to the next.
 
Last edited:
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Here is the final code

Code:
Sub copysubclientsandsplit()
n = WorksheetFunction.CountA(Worksheets("Sub-Client Config").range("A:A"))
If n <= 1 Then
'do nothing
Else
'
With Sheets("Sub-Client Config")
    .range(range("A1"), .range("A1").Offset(0, 2).End(xlDown)).Copy
End With
With Sheets("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
End With
End If
End Sub
 
Last edited:
Upvote 0
which didn't work...

this is the one that did!

Code:
Sub copysubclientsandsplit()
n = WorksheetFunction.CountA(Worksheets("Sub-Client Config").range("A:A"))
If n <= 1 Then
'do nothing
Else
'
With Sheets("Sub-Client Config")
    .range(.range("A1"), .range("A1").Offset(0, 2).End(xlDown)).Copy
End With
With Sheets("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
End With
End If
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,085
Messages
6,128,733
Members
449,465
Latest member
TAKLAM

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