Help needed with code

G

Gemz

Hi,

I have this sample code but everytime i try to run it this line is
highlighted and nothing happens:

Set rng = .Range(Cells(1, 17), Cells(LastRow, LastColumn))


The rest of the code:
LastColumn = .Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row

Set rng = .Range(Cells(1, 17), Cells(LastRow, LastColumn))
rng.Copy Destination:=NewbkS1.Range("E1")
oldbk.Activate
.Columns("H:H").AutoFilter Field:=1, Criteria1:="X"
.Columns("C:F").Copy Destination:= _
NewbkS1.Range("A1")
.Columns("K:Q").Copy Destination:= _
NewbkS1.Range("E1")

Whats going wrong?

please help, thanks!
 
P

PCLIVE

Is this code in the middle of some sort of WITH statement? It doesn't
appear to be except for some periods in the code.

A several lines stand out.

LastColumn = .Cells(1, Columns.Count).End(xlToLeft).Column 'Try
removing period before "Cells".
LastRow = .Cells(Rows.Count, 1).End(xlUp).Row 'Try
removing period before "Cells".

Set rng = .Range(Cells(1, 17), Cells(LastRow, LastColumn)) 'Try
removing period before "Range".
rng.Copy Destination:=NewbkS1.Range("E1")
oldbk.Activate
.Columns("H:H").AutoFilter Field:=1, Criteria1:="X"
'Try removing period before "Columns".
.Columns("C:F").Copy Destination:= _
'Try removing period before "Columns".
NewbkS1.Range("A1")
.Columns("K:Q").Copy Destination:= _
'Try removing period before "Columns".
NewbkS1.Range("E1")



HTH,
Paul
 
G

Gemz

Hi PCLIVE,

I tried moving the '.' where stated and it did worked ok but this time it
causes different errors in the macro!

Below is my whole macro - please see whats the problem, thanks.

(I do have names where you see "" ive just deleted them here.)

Set oldbk = ActiveWorkbook
Set newbk = Workbooks.Add

newbk.Sheets("Sheet1").Name = ""
Set NewbkS1 = newbk.Sheets("")
If newbk.Sheets.Count > 1 Then
newbk.Sheets("Sheet2").Name = ""
Else
newbk.Sheets.Add After:=Sheets(1)
newbk.Sheets("Sheet2").Name = ""
End If
Set NewbkS2 = newbk.Sheets("")

With oldbk.Sheets(1)
.AutoFilterMode = False

LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

Set rng = Range(Cells(1, 17), Cells(LastRow, LastColumn))
rng.Copy Destination:=NewbkS1.Range("E1")
oldbk.Activate
.Columns("H:H").AutoFilter Field:=1, Criteria1:=""
.Columns("C:D").Copy Destination:= _
NewbkS1.Range("A1")
.Columns("K:L").Copy Destination:= _
NewbkS1.Range("C1")
.Columns("N:Q").Copy Destination:= _
NewbkS1.Range("E1")
.Columns("R:AF").Copy Destination:= _
NewbkS1.Range("I1")

End With
Worksheets("").Activate
Cells.Select
Cells.EntireColumn.AutoFit
Cells.EntireRow.AutoFit



With oldbk.Sheets(2)
.Columns("H:H").Copy Destination:= _
NewbkS2.Range("A1")
.Columns("T:T").Copy Destination:= _
NewbkS2.Range("B1")
.Columns("AK:AK").Copy Destination:= _
NewbkS2.Range("C1")
.Columns("G:G").Copy Destination:= _
NewbkS2.Range("D1")
.Columns("AJ:AJ").Copy Destination:= _
NewbkS2.Range("E1")
.Columns("D:D").Copy Destination:= _
NewbkS2.Range("F1")
.Columns("AM:AM").Copy Destination:= _
NewbkS2.Range("G1")
.Columns("AP:AP").Copy Destination:= _
NewbkS2.Range("H1")
.Columns("AS:AS").Copy Destination:= _
NewbkS2.Range("I1")
.Columns("AU:AU").Copy Destination:= _
NewbkS2.Range("J1")
.Columns("AQ:AQ").Copy Destination:= _
NewbkS2.Range("K1")

End With

Worksheets("").Activate
Cells.Select
Cells.EntireColumn.AutoFit
Cells.EntireRow.AutoFit
With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True

End With
End Sub


thanks!!
 
D

Don Guillett

As I said in my post, the trick you have to learn is
Either activate the sheet and forget using the with and dots
or better coding is to PROPERLY use the dots within the with statement

with blah
 
P

PCLIVE

Usually when you use a With / End With section, everything within that
section will begin with a period (.).
But in your code you're mixing it up. You don't want to start activating
other sheets or workbooks in the middle of a With statement. In a With
statement, if the code deals with anything other than the With content, then
you should be ending the With.
See if this helps.

Set oldbk = ActiveWorkbook
Set newbk = Workbooks.Add

newbk.Sheets("Sheet1").Name = ""
Set NewbkS1 = newbk.Sheets("")

If newbk.Sheets.Count > 1 _
Then
newbk.Sheets("Sheet2").Name = ""
Else
newbk.Sheets.Add After:=Sheets(1)
newbk.Sheets("Sheet2").Name = ""
End If


Set NewbkS2 = newbk.Sheets("")
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

Set rng = Range(Cells(1, 17), Cells(LastRow, LastColumn))
rng.Copy Destination:=NewbkS1.Range("E1")

With oldbk.Sheets(1)
.AutoFilterMode = False
.Columns("H:H").AutoFilter Field:=1, Criteria1:=""
.Columns("C:D").Copy Destination:= _
NewbkS1.Range("A1")
.Columns("K:L").Copy Destination:= _
NewbkS1.Range("C1")
.Columns("N:Q").Copy Destination:= _
NewbkS1.Range("E1")
.Columns("R:AF").Copy Destination:= _
NewbkS1.Range("I1")
End With

Worksheets("").Activate
Cells.Select
Cells.EntireColumn.AutoFit
Cells.EntireRow.AutoFit



With oldbk.Sheets(2)
.Columns("H:H").Copy Destination:= _
NewbkS2.Range("A1")
.Columns("T:T").Copy Destination:= _
NewbkS2.Range("B1")
.Columns("AK:AK").Copy Destination:= _
NewbkS2.Range("C1")
.Columns("G:G").Copy Destination:= _
NewbkS2.Range("D1")
.Columns("AJ:AJ").Copy Destination:= _
NewbkS2.Range("E1")
.Columns("D:D").Copy Destination:= _
NewbkS2.Range("F1")
.Columns("AM:AM").Copy Destination:= _
NewbkS2.Range("G1")
.Columns("AP:AP").Copy Destination:= _
NewbkS2.Range("H1")
.Columns("AS:AS").Copy Destination:= _
NewbkS2.Range("I1")
.Columns("AU:AU").Copy Destination:= _
NewbkS2.Range("J1")
.Columns("AQ:AQ").Copy Destination:= _
NewbkS2.Range("K1")
End With

Worksheets("").Activate
Cells.Select
Cells.EntireColumn.AutoFit
Cells.EntireRow.AutoFit

With Application
.DisplayAlerts = True
.EnableEvents = True
.ScreenUpdating = True
End With


HTH,
Paul

--
 
G

Gemz

Hi,

Thanks a lot PCLIVE you made it work!



Gemz said:
Hi,

Sorry im confused, PCLIVE said to remove the dots and you said to use them?
unless i misinterpreted PCLIVE's post, im not too sure.

Please help!

thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top