index > Visual Basic for Applications (VBA) > Autofill

Autofill


Hello,

below is a part of the code im using. I want this code to be flexible enough so that any range of data will work with it. Thus I want the autofill part to not be limited to N2:n1860. I may have data thats only N2:n100 or n2:n3000.


Selection.AutoFill Destination:=Range("N2:N1860")
polska2180

hello,

Try this... it should allow the user to select the range to be auto filled,

Selection.AutoFill Destination:=Range(Application.Selection)

Derek Smyth

Thanks for your reply. I put the code you had into my code and here is how part of it looks.

Sheets("Variables").Select
range("P1").Select
Application.CutCopyMode = False
Sheets("Stats").Select
range("N2").Select
ActiveCell.FormulaR1C1 = "=RC[-8]-(RC[-4]+RC[-3]+RC[-1]+(RC[-5]*R1C9))"
range("B42").Select
Sheets("Variables").Select
range("Q1").Select
Sheets("Stats").Select
range("O2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/RC[-9]"
range("N2").Select

Selection.AutoFill Destination:=range(Application.Selection)
Selection.AutoFill Destination:=range(Application.Selection.Offset(rowOffset:=0, columnOffset:=1))
Columns("O:O").Select
Selection.Style = "Percent"
Columns("N:N").Select
Selection.NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
Columns("N:N").EntireColumn.AutoFit
range("M:M,K:K,J:J,F:F").Select

Application.Selection.Style = "Percent"


range("F1").Activate
Selection.NumberFormat = "_(* #,##0.00_);_(* (#,##0.00);_(* ""-""??_);_(@_)"
Selection.NumberFormat = "_($* #,##0.00_);_($* (#,##0.00);_($* ""-""??_);_(@_)"
range("M:M,K:K,J:J,F:F").EntireColumn.AutoFit
Cells.Select
With Selection
.HorizontalAlignment = xlGeneral
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0

I'm not sure what "Application.Selection.Style = "Percent" is for. Am I supposed to put this into the code as above?

Right now I get en error "method 'range' of object '_global' failed.

More help needed. Thanks

polska2180

Hello again,

The code you had originally referenced two columns N and O. You changed that in the autofill statements but you still have code that references columns N and O, for example.

Columns("O:O").Select
Selection.Style = "Percent"

Perhaps that's what your needing to do in your code or perhaps it's not, I don't know....

I was just pointing out that just as you changed the N and O references in the autofill statement then, if you need to do the same with the rest of the code, then continue to use Application.Selection as the range.

If on the other hand your code always changes column O to percentage then you don't need to use Application.Selection.Style.




www.dsmyth.net/blog
Derek Smyth

I'm a little confused as to what i should do. I appreciate you patience as i am a novice at this stuff. What I did is replaced my code with the code you gave me and i had the problems as I stated in the previous post. Below is the same piece of script as the one above with out any changes.

Range("H2").Select
Selection.AutoFill Destination:=Range("H2:H1860")
Range("H2:H1860").Select
Columns("H:H").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("G:G").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Cells.Select
Cells.EntireColumn.AutoFit
Columns("B:C").Select
Selection.ColumnWidth = 10.71
Range("B23").Select
Sheets("Variables").Select
Range("A1:O1").Select
Selection.Copy
Sheets("Stats").Select
Range("A1").Select
ActiveSheet.Paste
Sheets("Variables").Select
Range("P1").Select
Application.CutCopyMode = False
Sheets("Stats").Select
Range("N2").Select
ActiveCell.FormulaR1C1 = "=RC[-8]-(RC[-4]+RC[-3]+RC[-1]+(RC[-5]*R1C9))"
Range("B42").Select
Sheets("Variables").Select
Range("Q1").Select
Sheets("Stats").Select
Range("O2").Select
ActiveCell.FormulaR1C1 = "=RC[-1]/RC[-9]"
Range("N2").Select
Selection.AutoFill Destination:=Range("N2:N1860")
Range("N2:N1860").Select
Range("O2").Select
Selection.AutoFill Destination:=Range("O2:O1860")
Range("O2:O1860").Select
Columns("O:O").Select
Selection.Style = "Percent"
Columns("N:N").Select
Selection.NumberFormat = "$#,##0.00_);[Red]($#,##0.00)"
Columns("N:N").EntireColumn.AutoFit
Range("M:M,K:K,J:J,F:F").Select

polska2180

I resolved my problem using...thanks for all the help.

 

Dim DestinationRng, OriginalRng As Range
    Dim end_row As Integer
    end_row = Sheets("stats").Range("a65536").End(xlUp).Row
    Set DestinationRng = Sheets("stats").Range("H2:H" & end_row)
    Set OriginalRng = Range("H2")
    OriginalRng.AutoFill Destination:=DestinationRng, Type:=xlFillDefault

polska2180
reply 6

You can use google to search for other answers

 

More Articles

• Programming with MULTIPAGE
• Read data from excel through VB.NET
• precisely position embedded charts in worksheet
• Add code in control image in time of execution
• Sorting Subreport
• VB Script Crashes Access When Exporting to Excel
• How Can I create an UDF I can use in a cell?
• Access Link Table with Outlook Problem
• Help with simple program
• Global Variable in Excel question
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

• macro that hides rows - excel
• InputBox() in MS Word form textbox.
• Using VB in Excel 2003
• OutPutTo Macro
• Access Security Warnings
• Commandbar excel bug for "Document
• chart creation - type mismatch
• Global Variable in Excel question
• Question for Windows wizards re: VBA/VBS
• VBA code to search and modify in Word
• Acces functions in my ouwn services appl
• Run SQL Server 2005 DTS Package from MS
• Compiling a VBA project is increasing it
• Standalone aplication
• VBA and HTML Help

Hot Articles

• Derek and/or Cindy
• Automaticly updating charts in excel
• Loading Select data through the linked t
• Programming Adobe Acrobat Reader 7.0
• Exceptional kind of Error - Menus delete
• How to preserve the date format while re
• Excel 9.0 Library and Generating chart w
• Close excel
• Date Problem
• Another SendKeys
• What language was Microsoft Word develop
• Need help with VBA project, need it to t
• Help with interface?
• startup properties in Access
• How to populate a list box using adodb r

Recommend Articles

• How do you find out the Windows User Nam
• OutPutTo Macro
• Increase Speed of a Word Macro
• Novice VBA user trying to consolidate in
• Disabling enter and mouse click PPT navi
• whats wrong with this code?
• Count the rows
• access populate combo box help needed
• Excel: Treeview handle events for dynami
• Application Error
• Someone mind helping me with Error 426
• Is VBA SDK free and ...?
• Opening a Word file from a macro in Excel
• ActiveX RICHTX32.OCX
• Problem in Saving Excel 2003 Workbook in