Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2009, 12:40 PM
Registered User
 
Join Date: Sep 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default macro shorter

A question,
This macro can be written shorter ?

Code:
Sub CopyToDebiteuren()
    Application.ScreenUpdating = False
    With Sheets("Debiteuren").Range("B65536").End(xlUp)
        .Offset(1, 0).Value = Sheets("Factuur").Range("N14").Value
        .Offset(1, 1).Value = Sheets("Factuur").Range("N13").Value
        .Offset(1, 2).Value = Sheets("Factuur").Range("N16").Value
        .Offset(1, 3).Value = Sheets("Factuur").Range("D13").Value
        .Offset(1, 4).Value = Sheets("Factuur").Range("N41").Value
    End With
    Sheets("Factuur").Select
    Range("D13").Select
    MsgBox "Gegevens gekopieerd. "
    Factuur_bewaren
    NieuweFaktuur
End Sub
 
Old September 24th, 2009, 03:27 PM
JP JP is offline
Authorized User
 
Join Date: Apr 2008
Posts: 57
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Without seeing your sheet or knowing what you're trying to accomplish, it's difficult to tell what methods would be better suited to reach your goal. Your procedure looks very hard-coded.

Instead of worrying about how you can make your code shorter, you should worry about structuring your data to make your code work without having to hard code.

Can you set up your data in such a way that you don't have to put data into specific cells?

Do you need to select sheets?

Here's what I came up with:

Code:
 
Sub CopyToDebiteuren()
  Application.ScreenUpdating = False
  Dim destinationRange As Excel.Range
  Dim sourceWksht As Excel.Worksheet
  Dim sourceArray As Variant
  Dim i As Long, j As Long
  Set destinationRange = Sheets("Debiteuren").Range("B" & Rows.Count).End(xlUp)
  Set sourceWksht = Sheets("Factuur")
  With sourceWksht
    sourceArray = Array(.Range("N14").Value, .Range("N13").Value, .Range("N16").Value, .Range("D13").Value, .Range("N41").Value)
  End With
  With destinationRange
    Range(.Offset(1, 0), .Offset(1, 4)).Value = sourceArray
  End With
  ' why are you selecting?
  '  Sheets("Factuur").Select
  '  Range("D13").Select
  MsgBox "Gegevens gekopieerd. "
  Factuur_bewaren
  NieuweFaktuur
End Sub
Quote:
Originally Posted by Pinokkio View Post
A question,
This macro can be written shorter ?

Code:
Sub CopyToDebiteuren()
    Application.ScreenUpdating = False
    With Sheets("Debiteuren").Range("B65536").End(xlUp)
        .Offset(1, 0).Value = Sheets("Factuur").Range("N14").Value
        .Offset(1, 1).Value = Sheets("Factuur").Range("N13").Value
        .Offset(1, 2).Value = Sheets("Factuur").Range("N16").Value
        .Offset(1, 3).Value = Sheets("Factuur").Range("D13").Value
        .Offset(1, 4).Value = Sheets("Factuur").Range("N41").Value
    End With
    Sheets("Factuur").Select
    Range("D13").Select
    MsgBox "Gegevens gekopieerd. "
    Factuur_bewaren
    NieuweFaktuur
End Sub
__________________
Regards,
JP
JP SoftTech
 
Old September 24th, 2009, 03:48 PM
Registered User
 
Join Date: Sep 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks very much for our help!

Jean Pierre





Similar Threads
Thread Thread Starter Forum Replies Last Post
calling to xlam macro from macro inside xlsb SteveB Excel VBA 0 June 30th, 2008 06:43 PM
Writing A Macro Venusbj VB How-To 2 January 30th, 2008 01:10 AM
any shorter way to get the same result ? goldenstate ASP.NET 2.0 Basics 5 July 3rd, 2007 08:02 PM
Macro Help! Jolin VB How-To 0 April 22nd, 2004 08:38 AM
Macro KennethMungwira Access 1 June 13th, 2003 12:00 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.