PC Review


Reply
Thread Tools Rate Thread

Clean up code

 
 
Dorian C. Chalom
Guest
Posts: n/a
 
      6th Nov 2009
Please let me know if there is a cleaner way to do this...

Sub CopyRangeToNewSheetAndNameValues()
With Sheets("Quote Form")
newname = .Range("h10")
.Range(.Range("a1"), .Range("a1").SpecialCells(xlLastCell)).Copy
End With

Sheets.Add after:=Sheets(Sheets.Count)

With ActiveSheet
.Paste
.Range(.Range("a1"), .Range("a1").SpecialCells(xlLastCell)).Copy
.Range(.Range("a1"),
..Range("a1").SpecialCells(xlLastCell)).PasteSpecial Paste:=xlPasteValues
.Name = newname
.Range("a1").Select
End With

Worksheets("Quote Form").Activate
nCol = ActiveCell.SpecialCells(xlLastCell).Column
nRow = ActiveCell.SpecialCells(xlLastCell).Row

For iSht = 1 To Sheets.Count
If Sheets(iSht).Name = "Quote Form" Then
iSrcSht = iSht
End If
If Sheets(iSht).Name = Val(newname) Then
iDstSht = iSht
End If
Next iSht

For iCol = 1 To nCol
nSrcColWidth = Sheets(iSrcSht).Columns(iCol).ColumnWidth
Sheets(iDstSht).Columns(iCol).ColumnWidth = nSrcColWidth
Next iCol

For iRow = 1 To nRow
nSrcRowHeight = Sheets(iSrcSht).Rows(iRow).RowHeight
Sheets(iDstSht).Rows(iRow).RowHeight = nSrcRowHeight
Next iRow

Application.CutCopyMode = False

With Sheets("Quote Form")
.Range("B19:B46").ClearContents 'Item Number
.Range("H10:I11").ClearContents 'Invoice Number
.Range("G12:H12").ClearContents 'Address
End With
End Sub




 
Reply With Quote
 
 
 
 
Tim Williams
Guest
Posts: n/a
 
      6th Nov 2009
How about this ?

Sub CopyRangeToNewSheetAndNameValues()

Dim newname

With Sheets("Quote Form")
newname = .Range("h10").Value
.Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
.Range("B19:B46").ClearContents 'Item Number
.Range("H10:I11").ClearContents 'Invoice Number
.Range("G12:H12").ClearContents 'Address
End With

With ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
.Name = newname
.UsedRange.Value = .UsedRange.Value
End With

End Sub



Tim

"Dorian C. Chalom" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Please let me know if there is a cleaner way to do this...
>
> Sub CopyRangeToNewSheetAndNameValues()
> With Sheets("Quote Form")
> newname = .Range("h10")
> .Range(.Range("a1"), .Range("a1").SpecialCells(xlLastCell)).Copy
> End With
>
> Sheets.Add after:=Sheets(Sheets.Count)
>
> With ActiveSheet
> .Paste
> .Range(.Range("a1"), .Range("a1").SpecialCells(xlLastCell)).Copy
> .Range(.Range("a1"),
> .Range("a1").SpecialCells(xlLastCell)).PasteSpecial Paste:=xlPasteValues
> .Name = newname
> .Range("a1").Select
> End With
>
> Worksheets("Quote Form").Activate
> nCol = ActiveCell.SpecialCells(xlLastCell).Column
> nRow = ActiveCell.SpecialCells(xlLastCell).Row
>
> For iSht = 1 To Sheets.Count
> If Sheets(iSht).Name = "Quote Form" Then
> iSrcSht = iSht
> End If
> If Sheets(iSht).Name = Val(newname) Then
> iDstSht = iSht
> End If
> Next iSht
>
> For iCol = 1 To nCol
> nSrcColWidth = Sheets(iSrcSht).Columns(iCol).ColumnWidth
> Sheets(iDstSht).Columns(iCol).ColumnWidth = nSrcColWidth
> Next iCol
>
> For iRow = 1 To nRow
> nSrcRowHeight = Sheets(iSrcSht).Rows(iRow).RowHeight
> Sheets(iDstSht).Rows(iRow).RowHeight = nSrcRowHeight
> Next iRow
>
> Application.CutCopyMode = False
>
> With Sheets("Quote Form")
> .Range("B19:B46").ClearContents 'Item Number
> .Range("H10:I11").ClearContents 'Invoice Number
> .Range("G12:H12").ClearContents 'Address
> End With
> End Sub
>
>
>
>



 
Reply With Quote
 
Dorian C. Chalom
Guest
Posts: n/a
 
      7th Nov 2009
Tim;

This works really well...but why?
When I tried to Copy the sheet before it gave me errors because of the
lookup formulas attached to other workbooks. But in your code it works
great.

Also what does the UsedRange do?

Thank you

"Tim Williams" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> How about this ?
>
> Sub CopyRangeToNewSheetAndNameValues()
>
> Dim newname
>
> With Sheets("Quote Form")
> newname = .Range("h10").Value
> .Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
> .Range("B19:B46").ClearContents 'Item Number
> .Range("H10:I11").ClearContents 'Invoice Number
> .Range("G12:H12").ClearContents 'Address
> End With
>
> With ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
> .Name = newname
> .UsedRange.Value = .UsedRange.Value
> End With
>
> End Sub
>
>
>
> Tim
>
> "Dorian C. Chalom" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Please let me know if there is a cleaner way to do this...
>>
>> Sub CopyRangeToNewSheetAndNameValues()
>> With Sheets("Quote Form")
>> newname = .Range("h10")
>> .Range(.Range("a1"), .Range("a1").SpecialCells(xlLastCell)).Copy
>> End With
>>
>> Sheets.Add after:=Sheets(Sheets.Count)
>>
>> With ActiveSheet
>> .Paste
>> .Range(.Range("a1"), .Range("a1").SpecialCells(xlLastCell)).Copy
>> .Range(.Range("a1"),
>> .Range("a1").SpecialCells(xlLastCell)).PasteSpecial Paste:=xlPasteValues
>> .Name = newname
>> .Range("a1").Select
>> End With
>>
>> Worksheets("Quote Form").Activate
>> nCol = ActiveCell.SpecialCells(xlLastCell).Column
>> nRow = ActiveCell.SpecialCells(xlLastCell).Row
>>
>> For iSht = 1 To Sheets.Count
>> If Sheets(iSht).Name = "Quote Form" Then
>> iSrcSht = iSht
>> End If
>> If Sheets(iSht).Name = Val(newname) Then
>> iDstSht = iSht
>> End If
>> Next iSht
>>
>> For iCol = 1 To nCol
>> nSrcColWidth = Sheets(iSrcSht).Columns(iCol).ColumnWidth
>> Sheets(iDstSht).Columns(iCol).ColumnWidth = nSrcColWidth
>> Next iCol
>>
>> For iRow = 1 To nRow
>> nSrcRowHeight = Sheets(iSrcSht).Rows(iRow).RowHeight
>> Sheets(iDstSht).Rows(iRow).RowHeight = nSrcRowHeight
>> Next iRow
>>
>> Application.CutCopyMode = False
>>
>> With Sheets("Quote Form")
>> .Range("B19:B46").ClearContents 'Item Number
>> .Range("H10:I11").ClearContents 'Invoice Number
>> .Range("G12:H12").ClearContents 'Address
>> End With
>> End Sub
>>
>>
>>
>>

>
>



 
Reply With Quote
 
Tim Williams
Guest
Posts: n/a
 
      7th Nov 2009
I don't know why it wouldn't have worked before...

UsedRange is just that: the "used range" on the sheet, ie, it contains
(usually) all cells on a sheet which have been "used".

It's much quicker than doing something like this:

with ActiveSheet.cells
.value = .value
end with

Tim

"Dorian C. Chalom" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Tim;
>
> This works really well...but why?
> When I tried to Copy the sheet before it gave me errors because of the
> lookup formulas attached to other workbooks. But in your code it works
> great.
>
> Also what does the UsedRange do?
>
> Thank you
>
> "Tim Williams" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> How about this ?
>>
>> Sub CopyRangeToNewSheetAndNameValues()
>>
>> Dim newname
>>
>> With Sheets("Quote Form")
>> newname = .Range("h10").Value
>> .Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
>> .Range("B19:B46").ClearContents 'Item Number
>> .Range("H10:I11").ClearContents 'Invoice Number
>> .Range("G12:H12").ClearContents 'Address
>> End With
>>
>> With ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
>> .Name = newname
>> .UsedRange.Value = .UsedRange.Value
>> End With
>>
>> End Sub
>>
>>
>>
>> Tim
>>
>> "Dorian C. Chalom" <(E-Mail Removed)> wrote in message
>> news:%(E-Mail Removed)...
>>> Please let me know if there is a cleaner way to do this...
>>>
>>> Sub CopyRangeToNewSheetAndNameValues()
>>> With Sheets("Quote Form")
>>> newname = .Range("h10")
>>> .Range(.Range("a1"), .Range("a1").SpecialCells(xlLastCell)).Copy
>>> End With
>>>
>>> Sheets.Add after:=Sheets(Sheets.Count)
>>>
>>> With ActiveSheet
>>> .Paste
>>> .Range(.Range("a1"), .Range("a1").SpecialCells(xlLastCell)).Copy
>>> .Range(.Range("a1"),
>>> .Range("a1").SpecialCells(xlLastCell)).PasteSpecial Paste:=xlPasteValues
>>> .Name = newname
>>> .Range("a1").Select
>>> End With
>>>
>>> Worksheets("Quote Form").Activate
>>> nCol = ActiveCell.SpecialCells(xlLastCell).Column
>>> nRow = ActiveCell.SpecialCells(xlLastCell).Row
>>>
>>> For iSht = 1 To Sheets.Count
>>> If Sheets(iSht).Name = "Quote Form" Then
>>> iSrcSht = iSht
>>> End If
>>> If Sheets(iSht).Name = Val(newname) Then
>>> iDstSht = iSht
>>> End If
>>> Next iSht
>>>
>>> For iCol = 1 To nCol
>>> nSrcColWidth = Sheets(iSrcSht).Columns(iCol).ColumnWidth
>>> Sheets(iDstSht).Columns(iCol).ColumnWidth = nSrcColWidth
>>> Next iCol
>>>
>>> For iRow = 1 To nRow
>>> nSrcRowHeight = Sheets(iSrcSht).Rows(iRow).RowHeight
>>> Sheets(iDstSht).Rows(iRow).RowHeight = nSrcRowHeight
>>> Next iRow
>>>
>>> Application.CutCopyMode = False
>>>
>>> With Sheets("Quote Form")
>>> .Range("B19:B46").ClearContents 'Item Number
>>> .Range("H10:I11").ClearContents 'Invoice Number
>>> .Range("G12:H12").ClearContents 'Address
>>> End With
>>> End Sub
>>>
>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
clean up code a little terilad Microsoft Excel Misc 12 12th Apr 2010 07:35 PM
MultiLine Code Builder, Need Help to Clean Code: Benjamin Microsoft Excel Programming 0 18th Nov 2009 04:40 PM
Clean-up Code Howard Microsoft Excel Programming 4 3rd Dec 2008 02:11 AM
Clean up code. =?Utf-8?B?VGlt?= Microsoft Excel Programming 2 1st Oct 2004 05:37 PM
Help clean up this code... scottnshelly Microsoft Excel Programming 8 21st Jun 2004 09:30 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:15 PM.