Forum Discussion
Use VBA to Autofill a Row until the end of the number of data in another row
- Mar 25, 2019
Hi Haytham Amairah,
I was facing similar issues and chanced upon this thread. If i have two columns (O and P) that i wish to autofill via VBA, do I amend the code from:
Selection.AutoFill Destination:=Range("O2:P313")
Range("O2:P313").Selectto:
Selection.AutoFill Destination:=Range("O2:O" & Range("E" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
Selection.AutoFill Destination:=Range("P2:" & Range("E" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
Haytham Amairah hi I ran a macro to sort a file, the last step is to sort data by "sales unit this year "means data from Column H must Goto Column G. to do this i used sort function to move all the data to column G now to move the remaining data from H to G I went to the last cell of G that has data , move one cell below it to the empty cell the use a formula to bring data from H(e.g lastE
mptycellOF G=H56) and tried to autofill this to the equivalent last H cell that has data
this is the code that i added on top of the quote that was recorded by VBA when i was recoding a macro
Range("A4").Select
Selection.End(xlToRight).Select
Range("G4").Select
Selection.End(xlDown).Select
Range("G1059044").Select 'go all the way down to cell 1059044
Selection.End(xlUp).Select 'then co the way up until you find data
lastCell = ActiveCell.Address(True, False) 'trying to give the lastcell the address of the active cell because last cell always changes according to the length of the data
ActiveCell.Offset(1, 0).Select 'from the active cell which is supposed to be the last cell go one cell down
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[1]"
Selection.AutoFill Destination:=Range("lastCell" & Range("G4" & Rows.Count).End(xlUp).Row) ' want it to select the dynamic cell and autofill the formula
Range(Selection, Selection.End(xlDown)).Select
- Makwa2070Mar 16, 2023Copper Contributor
Maybe this will be a simplified version of the below even though it doesn't solve the entire problem
How do i make this below VBA code that sort two columns by "sales unit this year " to be able to run on any worksheet ,please note that i got this code from running macro now i want to modify it
code is failing to run on a new worksheet can this be because of the name difference?
With ActiveWorkbook.Worksheets("ME5").Sort
.SetRange Range("A4:H661325")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply