Consulting

Results 1 to 4 of 4

Thread: Streamline code from MRecorder to Good Code Help

  1. #1

    Streamline code from MRecorder to Good Code Help

    Here is code from the macro recorder, starting on line 3

    [VBA]
    Lastrow = ActSh.Cells(Rows.Count, 1).End(xlUp).Row
    ActSh.Range("A4", ActSh.Cells(Lastrow, "F")).ClearContents
    Sheets("DataEdited").Select
    Range("L2:S2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Sheets("PCCombined_FF").Select
    Range("A4").Select
    ActiveSheet.Paste
    Cells.Columns.AutoFit
    [/VBA]

    My thought is to convert it are
    #1- Remove the .Select from Sheets("DataEdited").Select and Range("L2:S2").Select
    #2- Remove Range(Selection, Selection.End(xlDown)).Select and replace w/ Select Up, (not that it matters in my current data, but thought it would be good practice
    #3- Set a variable for the Sheet it needs to copy from and copy to, so Remove Range("A4").Select & ActiveSheet.Paste and replace w/ one statment.

    My Attempt:
    [VBA]
    Dim rngSource As Range
    Dim rngTarget As Range
    Dim ActSh As Worksheet
    Dim TrgSh As Worksheet
    Dim Lastrow As Long
    Set ActSh = Sheets("PCCombined_FF")
    Set TrgShFF = Sheets("DataEdited")
    Lastrow = ActSh.Cells(Rows.Count, 1).End(xlUp).Row
    Lastrow = TrgShff.Range(Rows.Count,12).End(xlUp).Row
    ActSh.Range("A4", ActSh.Cells(Lastrow, "F")).ClearContents

    TrgSh.Range("L4",TrgSh.Cells(Lastrow, "S")).copy Destination:=Range("A4",ActSh.Range("A4"))[/VBA]

    Thanks for having a look

    YLP

  2. #2
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    I would suggest to add the following:

    [vba]Set ActSh = Nothing
    Set Trgsh = Nothing[/vba] But this doesn't seem like it works (at least in a test run it didn't work for me):
    [vba]
    TrgSh.Range("L4", TrgSh.Cells(Lastrow, "S")).Copy Destination:=Range("A4", ActSh.Range("A4"))[/vba] Although I think you wanted to do this:

    [vba]TrgSh.Range("L4", TrgSh.Cells(Lastrow, "S")).Copy Destination:=ActSh.Range("A4")[/vba]
    Right???




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  3. #3
    Hi Joseph,
    I think you are correct in the last line of code. I have not tried your code, because it sounds like you were not able to get it to work based on my code, is that correct?

    YLP

  4. #4
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Basically.

    This is what I came up with...revised stuff in bold:
    EDIT: I removed/changed some other things
    [vba]
    Sub YLP()
    Dim ActSh As Worksheet
    Dim TrgSh As Worksheet
    Dim Lastrow As Long

    Set ActSh = Sheets("PCCombined_FF")
    Set TrgSh = Sheets("DataEdited") 'Noticed this line didn't match your variable in your original code

    Lastrow = ActSh.Cells(Rows.Count, 1).End(xlUp).Row
    ActSh.Range("A4", ActSh.Cells(Lastrow, "F")).ClearContents

    Lastrow = TrgSh.Cells(Rows.Count, 12).End(xlUp).Row
    TrgSh.Range("L4", TrgSh.Cells(Lastrow, "S")).Copy Destination:=ActSh.Range("A4")

    Set TrgSh = Nothing
    Set ActSh = Nothing

    End Sub[/vba]
    I wasn't sure what you were doing with "TrgShFF"....so the code above is just my interpretation of what you were trying to acclomplish.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

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