Consulting

Results 1 to 12 of 12

Thread: Autofill

  1. #1
    VBAX Newbie
    Joined
    Nov 2011
    Posts
    1
    Location

    Autofill

    Hi,

    I need some help to finish a macro to run an autofill!

    I'm trying to indicate the final range through the contents of the A1 cell:

    [VBA]Dim lastrow As String

    lastrow = Cells(1, 1)
    Selection.AutoFill Destination:=Range("A5", lastrow), Type:=xlFillDefault
    [/VBA]

    Someone knows the reason for do not assume the variable?

    Many Thanks

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Welcome to the forum!

    Always Dim row numbers as Long.

  3. #3
    VBAX Regular darweesh8's Avatar
    Joined
    Feb 2012
    Location
    Amman-Jordan
    Posts
    14
    Location

    Arrow

    Kenneth Hobs,

    can you modify his code so it find the last cell in the column and fill up to it?

    regards,

    Darweesh

  4. #4
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    If there is a value already in Column A that you are using to determine the last row, then this should fill the value that is in "A5", down to one cell above the last row.
    [VBA]Dim LastRow As Long

    With ActiveSheet
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    .Range("A5").AutoFill .Range("A5:A" & LastRow - 1), Type:=xlFillDefault
    End With[/VBA]

  5. #5
    VBAX Regular darweesh8's Avatar
    Joined
    Feb 2012
    Location
    Amman-Jordan
    Posts
    14
    Location
    Thanks Frank but it worked only if i put it in sub procedure and call it in the main, if i put it directly to main a run time error occured " class failed"

  6. #6
    VBAX Expert
    Joined
    Sep 2010
    Posts
    604
    Location
    Can you through together a sample workbook that fails so I see what you mean? - Or at least more of your main sub code. And a perhaps a little more detail of the data and sheet structure.

  7. #7
    VBAX Regular darweesh8's Avatar
    Joined
    Feb 2012
    Location
    Amman-Jordan
    Posts
    14
    Location
    here it is Frank, this sheet will open a raw data and copy from it to this workbook.
    i will upload the raw data next post
    Attached Files Attached Files

  8. #8
    VBAX Regular darweesh8's Avatar
    Joined
    Feb 2012
    Location
    Amman-Jordan
    Posts
    14
    Location
    here is the raw data
    Attached Files Attached Files

  9. #9
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    I don't see a syntax error. What is the problem?

    When I entered 6 for the elevation, column 3 started with 6 but the friction numbers started at the 0 elevation from the slave file.

  10. #10
    VBAX Regular darweesh8's Avatar
    Joined
    Feb 2012
    Location
    Amman-Jordan
    Posts
    14
    Location
    Mr. Hobs,

    Try to move the code from the macro to the main code, it will not work!!
    That is the problem, I know it's working but I like to know why it happened.

  11. #11
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    Do you mean that there is a problem with running Macro7? By running the Main "macro", I think that you mean running CommandButton1_Click.

    A problem, run-time error 1004, when running Macro7 after the CommandButton1_Click is that you did not unprotect the sheet as you did in the latter routine. Rather than doing all that unprotect and protect for each sheet, I recommend doing this sort of thing for each sheet:

    [vba] Sheets("start").Protect UserInterfaceOnly:=True[/vba]
    This allows your code to change sheet data but not manual changes by the user. I normally do this in the Open event of the ThisWorkbook object but it should be done for each newly added sheet if your code will then modify the data.

  12. #12
    VBAX Regular darweesh8's Avatar
    Joined
    Feb 2012
    Location
    Amman-Jordan
    Posts
    14
    Location
    a million thanks Mr. Hobs, I got it.

Posting Permissions

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