+ Reply to Thread
Results 1 to 9 of 9

I need help with cleaning up some ugly Macro Code.

Hybrid View

  1. #1
    Registered User
    Join Date
    10-13-2014
    Location
    Assen, The netherlands
    MS-Off Ver
    2013
    Posts
    27

    I need help with cleaning up some ugly Macro Code.

    Hi All,

    I made some ugly macro code.
    It functions, but I do know it's not the "standard" way to get my trick done.

    Here's some of the code:

    Private Sub Name1_Click()
    
    Dim ws As Worksheet:    Set ws = Worksheets("Factuur")
    Dim lastrowD As Range, lastrowE As Range, lastrowF As Range, lastrowG As Range, lastrowH As Range, lastrowI As Range, lastrowJ As Range, lastrowK As Range, lastrowL As Range
    
    Set lastrowD = ws.Range("D13:D14").Find(What:="", LookIn:=xlValues, LookAt:=xlWhole)
    Set lastrowE = ws.Range("D14:D15").Find(What:="", LookIn:=xlValues, LookAt:=xlWhole)
    Set lastrowF = ws.Range("D15:D16").Find(What:="", LookIn:=xlValues, LookAt:=xlWhole)
    Set lastrowG = ws.Range("G15:G16").Find(What:="", LookIn:=xlValues, LookAt:=xlWhole)
    Set lastrowH = ws.Range("D16:D17").Find(What:="", LookIn:=xlValues, LookAt:=xlWhole)
    Set lastrowI = ws.Range("G16:G17").Find(What:="", LookIn:=xlValues, LookAt:=xlWhole)
    Set lastrowJ = ws.Range("N14:N15").Find(What:="", LookIn:=xlValues, LookAt:=xlWhole)
    Set lastrowK = ws.Range("N15:N16").Find(What:="", LookIn:=xlValues, LookAt:=xlWhole)
    Set lastrowL = ws.Range("N16:N17").Find(What:="", LookIn:=xlValues, LookAt:=xlWhole)
    
    If Not lastrowD Is Nothing Then
        lastrowD.Value = Me.Klant1.Value
        lastrowE.Value = Me.Adres1.Value
        lastrowF.Value = Me.Plaats1.Value
        lastrowG.Value = Me.Postcode1.Value
        lastrowH.Value = Me.Telefoon1.Value
        lastrowI.Value = Me.Email1.Value
        lastrowJ.Value = Me.Werkbon_nr1.Value
        lastrowK.Value = Me.Kenteken1.Value
        lastrowL.Value = Me.km_stand1.Value
    Else
        MsgBox ("The range is full")
    End If
    
    UserForm3.Hide
    
    End Sub
    I only need the value from Userform3 to be placed into the fields.
    As I don't know how else to achieve this, I made a range, and that works.
    But I'm sure it must be possible to point it to a cell.
    To make it simple, the code I used : "Set lastrowD = ws.Range("D13:D14").Find" makes a range from D13 to D14, but as it add's 1, it's always D14.
    And D14 is also the field that requires the value of that (name) field of the Userform3.

    It's ugly. But it works ! And I'm happy about that

    Next code that needs to be simplified:

    Sub Print_Factuur()
    Worksheets("Factuur").PrintOut
    Worksheets("Factuur").PrintOut
    Opslaan
    End Sub
    I need a nice PrintOut, and I need 2 copies.
    Tons of information on the WWW about printing, but how to make a simple duplicate copy? I really did search (for over 2 hours, then i gave up).
    This code of mine works, it does the trick, but I'm sure there is a better way!

    That's all for today

    All the help is appreciated!
    Attached Files Attached Files
    Last edited by Fhorst; 10-22-2014 at 06:57 PM. Reason: Solved :)

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: I need help with cleaning up some ugly Macro Code.

    As you would find in Help,

    Worksheets("Factuur").PrintOut Copies:=2
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    10-13-2014
    Location
    Assen, The netherlands
    MS-Off Ver
    2013
    Posts
    27

    Re: I need help with cleaning up some ugly Macro Code.

    Thanks!

    I did not look in the "Help"
    Google usually gives better help then the build-in help, but in this case....

  4. #4
    Registered User
    Join Date
    03-12-2014
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: I need help with cleaning up some ugly Macro Code.

    Fhorst. I'm assuming you're looking at pasting data on to Empty cells in Excel sheet? Sorry if that isn't the case the post isn't very clear.

    However if that is the case you could use a more clearer method. Use the below code.

    Dim wb As Object, counter as integer
    Set wb = Workbooks("Yourbookname.xls").Sheets("Yoursheetname.xls").Cells
    counter = 1
    'loop to find a blank cell
    Do while wb(counter,4).value <> ""
    counter = counter + 1
    Loop
    'once you've found your blank cell
    wb(counter,4).value = Me.Klant1.Value
    wb(counter,5).value = Me.Adres1.Value
    wb(counter,6).value = Me.Plaats1.Value
    wb(counter,7).value = Me.Postcode1.Value
    wb(counter,8).value =  Me.Telefoon1.Value
    wb(counter,9).value =  Me.Email1.Value
    wb(counter,10).value =  Werkbon_nr1.Value
    wb(counter,11).value =  Me.Kenteken1.Value
    wb(counter,12).value =  Me.km_stand1.Value
    Unload Me
    Msgbox ("Your data has been uploaded"),vbinformation
    End Sub
    The method could've been shorter if your controls were named similar with numeric order..

  5. #5
    Registered User
    Join Date
    10-13-2014
    Location
    Assen, The netherlands
    MS-Off Ver
    2013
    Posts
    27

    Re: I need help with cleaning up some ugly Macro Code.

    Hi Jayesh,

    I'm sorry that I can't be more clear.
    I'll try to make it more specific:

    Klant1.Value : needs to enter in field D13 on the sheet "Factuur"
    Adres1.Value : needs to enter in field D14 on the sheet "Factuur"
    Plaats1.Value : needs to enter in field D15 on the sheet "Factuur"
    Postcode1.Value : needs to enter in field G15 on the sheet "Factuur"
    Telefoon1.Value : needs to enter in field D16 on the sheet "Factuur"
    Email1.Value : needs to enter in field G16 on the sheet "Factuur"
    Werkbon_nr1.Value : needs to enter in field N14 on the sheet "Factuur"
    Kenteken1.Value : needs to enter in field N15 on the sheet "Factuur"
    km_stand1.Value : needs to enter in field N16 on the sheet "Factuur"

    Thanks!

  6. #6
    Registered User
    Join Date
    03-12-2014
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    41

    Re: I need help with cleaning up some ugly Macro Code.

    Well If it's a fixed cell you'd like your values to be pasted on then you could directly refer them to a cell range as below.

    Activeworkbook.Sheets("Factuur").Range("D13").value = Klant1.value
    Activeworkbook.Sheets("Factuur").Range("D14").value = Adres1.Value
    Activeworkbook.Sheets("Factuur").Range("D15").value = Plaats1.Value
    Activeworkbook.Sheets("Factuur").Range("D16").value = Telefoon1.Value
    Activeworkbook.Sheets("Factuur").Range("G15").value = Postcode1.Value
    Activeworkbook.Sheets("Factuur").Range("G16").value = Email1.Value 
    Activeworkbook.Sheets("Factuur").Range("N14").value = Werkbon_nr1.Value
    Activeworkbook.Sheets("Factuur").Range("N15").value = Kenteken1.Value
    Activeworkbook.Sheets("Factuur").Range("N16").value = km_stand1.Value

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: I need help with cleaning up some ugly Macro Code.

    Or ...

        With Worksheets("Factuur")
            .Range("D13:D16").Value = WorksheetFunction.Transpose(Array(Klant1.Value, _
                                                                        Adres1.Value, _
                                                                        Plaats1.Value, _
                                                                        Telefoon1.Value))
            .Range("G15:G16").Value = WorksheetFunction.Transpose(Array(Postcode1.Value, _
                                                                        Email1.Value))
            .Range("N14:N16").Value = WorksheetFunction.Transpose(Array(Werkbon_nr1.Value, _
                                                                        Kenteken1.Value, _
                                                                        km_stand1.Value))
        End With

  8. #8
    Registered User
    Join Date
    10-13-2014
    Location
    Assen, The netherlands
    MS-Off Ver
    2013
    Posts
    27

    Re: I need help with cleaning up some ugly Macro Code.

    Thank you both! SHG and Jayesh25

    The code is much better this way!

    For me, the code from SHG looks like one I can follow, and edit if needed. (not that the one from Jayesh25 is not good, but one has to chose...)

    Now I still have this issue :copy values and cross check

    This post can be closed. (I think I can do that right?)

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: I need help with cleaning up some ugly Macro Code.

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Looking for help Cleaning up code
    By SmoothRider in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-20-2014, 02:58 PM
  2. [SOLVED] cleaning up code for macro with multiple tabs
    By ds0919 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-19-2013, 02:51 PM
  3. Ugly slow code....
    By Slyone2 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 08-16-2010, 08:13 PM
  4. Sort Macro - Cleaning up code
    By max57 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-25-2010, 06:29 PM
  5. Cleaning up the code after recording a macro.
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-01-2010, 02:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1