To any lines of code

0774don

New Member
Joined
May 25, 2012
Messages
2
I am a relative beginner at writing macros, self taught but enthusiastic. I would appreciate any help you could offer with my coding problem.
Attached is a simple macro to illustrate my problem.
The macro code, amongst other things, enters data from a row into a column.
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>
The value in the first cell entered manually.
Then the macro, (lines 4-33) auto fills the range C20:R20, and cuts and pastes
the individual cells values in row 20 into the column C.
As further data is entered into row 20, it is copied into column C
<o:p> </o:p>
Even in this simple example this action generates 29 lines of repetitive code, and lots of opportunities for errors.
<o:p> </o:p>
Is there any way of condensing these 29 lines of code, and achieve the same result?
Any assistance would be gratefully received.
<o:p> </o:p>
Thank you,
Don.

Code:
Sub DOUBLE_ENTRY_TRIAL()<o:p></o:p>
'<o:p></o:p>
' DOUBLE_ENTRY_TRIAL Macro<o:p></o:p>
' Macro recorded <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:date Month="5" Day="25" Year="2012">25/05/2012</st1:date> by Don<o:p></o:p>
'<o:p></o:p>
<o:p> </o:p>
'<o:p></o:p>
    Range("C20").Select<o:p></o:p>
    Selection.AutoFill Destination:=Range("C20:R20"), Type:=xlFillDefault<o:p></o:p>
    Range("C20:R20").Select<o:p></o:p>
    Range("D20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C21")<o:p></o:p>
    Range("E20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C22")<o:p></o:p>
    Range("F20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C23")<o:p></o:p>
    Range("G20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C24")<o:p></o:p>
    Range("H20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C25")<o:p></o:p>
    Range("I20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C26")<o:p></o:p>
    Range("J20").Select
      Selection.Cut Destination:=Range("C27")<o:p></o:p>
    Range("K20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C28")<o:p></o:p>
    Range("L20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C29")<o:p></o:p>
    Range("M20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C30")<o:p></o:p>
    Range("N20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C31")<o:p></o:p>
    Range("O20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C32")<o:p></o:p>
    Range("P20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C33")<o:p></o:p>
    Range("Q20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C34")<o:p></o:p>
    Range("R20").Select<o:p></o:p>
    Selection.Cut Destination:=Range("C35")<o:p></o:p>
    Range("D20").Select<o:p></o:p>
    ActiveCell.FormulaR1C1 = "=R[-1]C-RC[-1]"<o:p></o:p>
    Range("D20").Select<o:p></o:p>
    Selection.AutoFill Destination:=Range("D20:D35"), Type:=xlFillDefault<o:p></o:p>
    Range("D20:D35").Select<o:p></o:p>
    Range("I31").Select<o:p></o:p>
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Code:
[color=darkblue]Sub[/color] DOUBLE_ENTRY_TRIAL()
[color=green]'[/color]
[color=green]' DOUBLE_ENTRY_TRIAL Macro[/color]
[color=green]' Macro recorded 25/05/2012 by Don[/color]
[color=green]'[/color]
    Range("C20").AutoFill Destination:=Range("C20:C35"), Type:=xlFillDefault
    Range("D20:D35").FormulaR1C1 = "=R[-1]C-RC[-1]"

[color=darkblue]End[/color] [color=darkblue]Sub[/color]
 
Upvote 0
Hi, try
Code:
Sub DOUBLE_ENTRY_TRIAL()
    Range("C20:R20").Cut Destination:=Range("C21")
    Range("D20").FormulaR1C1 = "=R[-1]C-RC[-1]"
    Range("D20").AutoFill Destination:=Range("D20:D35"), Type:=xlFillDefault
End Sub

.Select method is known for its slowness.
So, You can reproduce this code by just replacing the object (whatever is in front of .Select) with the following Selection

So, replace this code with
Code:
Range("F20").Select
Selection.Cut Destination:=Range("C23")

with
Code:
Range("F20").Cut Destination:=Range("C23")

Just a simple way to speed up the code from recording macro
 
Upvote 0
I am assuming there is a legitimate reason you autofill it to the right first.
This emulates the entire process, but with less code.

Code:
Sub DOUBLE_ENTRY_TRIAL()
'
' DOUBLE_ENTRY_TRIAL Macro
' Macro recorded 25/05/2012 by Don
'
 
    Range("C20:R20").Formula = Range("C20").Formula
    Range("D20:R20").Copy
    Range("C21").PasteSpecial xlPasteAll, Transpose:=True
    Range("D20:R20").Clear
    Application.CutCopyMode = False
    Range("D20").FormulaR1C1 = "=R[-1]C-RC[-1]"
    Range("D20").AutoFill Destination:=Range("D20:D35"), Type:=xlFillDefault
    Range("I31").Select
End Sub

I suspect if we understood exactly what this was doing there is an even easier way.
 
Upvote 0
Re: To many lines of code

Further to my post,
Windows XP, Office XP Pro SP2, Excel 2002
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p> </o:p>
Sorry didn’t know how to post my example worksheet.
<o:p> </o:p>
Attached is an Excel worksheet to illustrate simply, a method of accounting that I have developed for my own use.
The example consists of a sample “weekly cash book”, and one specimen “ledger sheet”.
The entries in the cash book would be copied and pasted from weekly collection records, and automatically from there onto the individual ledger sheets.
<o:p> </o:p>
When a new ledger sheet is created, the invoice value is entered manually into Cell B19 (ledger).
Cell D5 (cash book) is copied and pasted to Cell C20 (Ledger),
Then the macro is run.
<o:p> </o:p>
In the macro, (lines 4-33) selects, and auto fills the range C20:R20, and cuts, and pastes the individual cells in the range into column C. in the “ledger sheet.”
<o:p> </o:p>
As further data is entered into the “Cash Book” it is copied to the correct “ledger sheet”
<o:p> </o:p>
Even in this simple example this action generates 29 lines of repetitive code, and lots of opportunities for errors.
<o:p> </o:p>
The Transpose Function, or the Copy> Paste special> Transpose, route will not work as only existing values are transposed.
Hope that makes my question a little clearer
<o:p> </o:p>
Thank you,
Don.

Excel 2002
ABCDEFGHIJKLMNOPQR
1Weekly Cash Book
2Payments
3Account nameInvoiceBalanceWk.123456789101112131415
4
5A.N.Other60.005.0010.00-5.005.004.006.00--5.00
6G Raffe10.004.005.006.005.006.004.00---
7N.E.Bodie10.002.0010.0010.0041.002.003.006.004.005.00
8
9Totals60.0025.0016.0015.0021.0051.0012.0013.006.004.0010.00-----
10
11
12
13
14SALES LEDGER SHEET
15
16Account NameA.N.Other
17
18DRCRBAL
19Invoice100.00100.00
20Payment Wk15.0095.00
21210.0085.00
223-85.00
2345.0080.00
2455.0075.00
2564.0071.00
2676.0065.00
278-65.00
289-65.00
29105.0060.00
3011-60.00
3112-60.00
3213-60.00
3314-60.00
34-60.00
35-60.00
36100.0040.0060.00
Sheet1

 
Upvote 0

Forum statistics

Threads
1,215,007
Messages
6,122,670
Members
449,091
Latest member
peppernaut

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