简体   繁体   中英

VBA in EXCEL. AUTOFILL RIGHT

How can I do an autofill to the right?

I've searched and tried to replicate the same steps that I saw on other threads but it didn't worked for me. Maybe for some lack of intuition.

My sheet is generating a new sheet from VBA (its a monte carlos simulation). I want to generate a new line on top of the matrix and fill the line from B1 to the last column with "0,1,2,...,X"

I've tried the following code:

Sheets("valores").Select
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B1").Select
ActiveCell.FormulaR1C1 = "0"
Range("B2").Select
Selection.End(xlToRight).Select
Range("LastColumn"&1).Select
Range(Selection, Selection.End(xlToLeft)).Select
Selection.DataSeries Rowcol:=xlRows, Type:=xlLinear, Date:=xlDay, Step _
    :=1, Trend:=False

  
  MsgBox "Sheet generated."
  

Can someone give me some help on this? Thank you!

Supposing you want to fill to the right from cell B1 to cell X, incrementing the value 1 serially, then you could use the following code: Assuming cell X is for example Z1, you have:

Range("B1").Value=0
Range("B1").Select
Selection.Autofill Destination :=Range("B1:Z1"), Type 
:=xlFillSeries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM