Home |
Search |
Today's Posts |
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
OK try this: Make the sheet you want to copy from the active sheet before
you run this. Sub Copy() Dim StdWidth As Long, MyWidth As Long, i As Integer Dim sh As String, WrkSht As Worksheet sh = ActiveSheet.Name For Each WrkSht In Worksheets If WrkSht.Name = "Report" Then MsgBox ("A sheet named Report already exists") Exit Sub End If Next Worksheets.Add.Name = "Report" Worksheets(sh).Range("A:F,H:H,U:U,X:Y").Copy Range("A1") Columns("A:J").AutoFit StdWidth = Columns(11).ColumnWidth 'unused column For i = 1 To 10 MyWidth = Columns(i).ColumnWidth Next If MyWidth StdWidth Then Columns("A:J").ColumnWidth = MyWidth Else Columns("A:J").ColumnWidth = StdWidth End If End Sub You wanted to make the pasted columns uniform in width. 10 columns were pasted to the new sheet. I AutoFit these columns to the contents in them. I then check the width of an unused column (11) The For i = 1 to 10 code will find the widest column in the 10 columns that were pasted. If any of those columns are wider than a default column width (column 11) then make all 10 columns that widest width. If all 10 columns are less in width than the default width, then make all 10 columns the default width. This will give some uniformity to those column widths. Mike F "Gemz" wrote in message ... I tried pasting this into a VB module but already it displayed the first line as red..."Columns("A:F, H, U, X, Y").Copy Worksheets("Sheet2").Range("A1")." meaning there is something wrong with this line. i have put 'sub and a name' just before this line but dont know why its not working. also when you make reference to sheet 2, do you mean this info will be copied into sheet 2 only if there is a sheet existing with the name sheet 2? can i not jus say copy into another sheet which is non-existant and for the macro to create it and name it report? finally, what do you mean by "For i = 1 To 10" thanks in advance for all your help. "Mike Fogleman" wrote: Assuming you have already applied the filter: Columns("A:F, H, U, X, Y").Copy Worksheets("Sheet2").Range("A1"). Worksheets("Sheet2").Columns("A:J").AutoFit Dim StdWidth As Long, MyWidth As Long, i As Integer StdWidth = Columns(11).ColumnWidth 'unused column For i = 1 To 10 MyWidth = Columns(i).ColumnWidth Next If MyWidth StdWidth Then Columns("A:J").ColumnWidth = MyWidth Else Columns("A:J").ColumnWidth = StdWidth End If Mike F "Gemz" wrote in message ... I'd like to tell a macro to copy specified columns after it has filtered for a criteria - wich is if column U is blank to hide these rows and then copy columns A,B,C,D,E,F,H,Y,U,X to another sheet within the same workbook. if possible i would also like to tell the macro to format the data in some way because when i manually copied across, the column widths etc were not the same size and it just looked messy, can i also tell the macro to do this? please reply at your earliest possible convinience, many thanks. |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Copy Columns to New Sheet | Excel Programming | |||
copy different columns from sheet to other sheet | Excel Programming | |||
Copy certain columns to another sheet using a macro | Excel Programming | |||
Copy a row from one sheet to another and not all columns copy | Excel Programming | |||
macro to copy columns to sheet | Excel Discussion (Misc queries) |