I want to select the dynamic cell and autofill it till the last row - excel

the value is in cell C and lastrow2 and want to autofill till lastrow3
Range("C" & lastrow2).Select
Selection.AutoFill Destination:=Range(.Cells(lastrow2, 3), .Cells(lastrow3, 3)).FillDown

Rather than .Select and then Autofill, you can populate the entire range in one go. Is this what you are trying?
ws.Range(ws.Cells(lastrow2, 3), ws.Cells(lastrow3, 3)).Formula = _
ws.Cells(lastrow2, 3).Formula
Where ws is your relevant sheet. For example
Dim ws As Worksheet
Set ws = Sheet1

Related

Code modification to copy all data in a row or the whole row instead of only copying a cell

New VBA user here, the below code matches the 1st column in a worksheet with the 1st column in another worksheet using vlookup then copies the first cell from 1st to 2nd as the screenshots.
Code
Sub solution()
Dim oldRow As Integer
Dim newRow As Integer
Dim lrow_output As Integer 'variable indicating last fulfilled row
Dim WB_Input As Workbook
Dim WB_Output As Workbook
Dim WS_Input As Worksheet
Dim WS_Output As Worksheet
Dim funcStr As String
Set WB_Input = Workbooks("input")
Set WB_Output = Workbooks("output1")
Set WS_Input = WB_Input.Worksheets("input")
Set WS_Output = WB_Output.Worksheets("Sheet1")
With WS_Output
lrow_output = .Cells(.Rows.Count, 1).End(xlUp).Row
End With
With WS_Input
funcStr = "=IFERROR(VLOOKUP(" & Cells(1, 1).Address(False, False) & "," & "'[" & WB_Input.Name & "]" & .Name & "'!" & Range(.Columns(1), .Columns(2)).Address & ",2,0),"""")"
End With
With WS_Output
.Cells(1, 2).Formula = funcStr
.Cells(1, 2).Copy
Range(.Cells(1, 2), .Cells(lrow_output, 2)).PasteSpecial xlPasteFormulas
WS_Output.Calculate
Range(.Cells(1, 2), .Cells(lrow_output, 2)).Copy
Range(.Cells(1, 2), .Cells(lrow_output, 2)).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End With
End Sub
Problem: I need the code to copy and paste the all data in the row, not just the first cell.
Problem2:If possible I need the code to scan multiple sheets, not just one so it would be 1 input main workbook sheet and 4 output sheets in the output workbook.
Problem3(Optional): if possible I need the successfully matched and copied rows in the input workbook to be colored to tell them from the unsuccessful matches.
Thank you in advance, I really appreciate all the possible aid.
Here is a quick macro that will take the active cell row copy it and then select specified sheet and paste it in active cell row:
Sub CopyPaste()
'
' CopyPaste Macro
'
'
ActiveCell.Rows("1:1").EntireRow.Select
Selection.Copy
Sheets("Sheet#").Select
ActiveCell.Rows("1:1").EntireRow.Select
ActiveSheet.Paste
End Sub

How to enter a variable value decided by the user on vba?

I have to do a calculation on using several excel sheets and I need the user to enter the number of lines except that this number varies each year so I want this variable to be variable and entered by the user who will use the macro. Unfortunately I can't find a way to do it.
Range("B2").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[7],RIGHT(RC[-1]))"
Range("B2").Select
Selection.AutoFill Destination:=Range("B2:B95145"), Type:=xlFillDefault
Range("B2:B95145").Select
Range("J1").Select
for example, when I want to perform a calculation I want the B2:B95145 to be variable, the numbers that must be entered before the macro is launched.
You can automatically find the last used row in column A for example using
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
So this code should fill as many rows in column B as there is data in column A
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'define your sheet name
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Range("B2").FormulaR1C1 = "=CONCATENATE(RC[7],RIGHT(RC[-1]))"
ws.Range("B2").AutoFill Destination:=ws.Range("B2:B" & LastRow), Type:=xlFillDefault
or even just write the formula without using Autofill into all cells directly:
ws.Range("B2:B" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[7],RIGHT(RC[-1]))"
If you have another formula eg in column C then just add it like
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1") 'define your sheet name
Dim LastRow As Long
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
ws.Range("B2:B" & LastRow).FormulaR1C1 = "=CONCATENATE(RC[7],RIGHT(RC[-1]))"
ws.Range("C2:C" & LastRow).FormulaR1C1 = 'your formula here

I have Set range to Last row and I want to copy it

I have a set of range (headers) and for each column I'm trying to copy the cells (headers + 1 row offset) until last row and paste it to another worksheet.
I've tried this:
Sub CopyandPaste
Dim Lastrow as Long
Lastrow = Worksheets("Sheet1").Range("A" & rows.Count).End(xlUp).Row
Set P = Application.InputBox("Code", "CODE", , , , , , 8)
P.Columns(2).offset(1, 0).Select
**From activecell to lastrow copy**
Worksheets("Sheets2").Range("A2").PasteSpecial xlPasteValues
P.Columns(3).Offset(1,0).Select
**From activecell to lastrow copy**
Worksheets("Sheets2").Range("C2").PasteSpecial xlPasteValues
End Sub
Also, if I do a loop, can you please tell me how I can approach it? Basically the idea is to copy a set range and paste it to a new worksheet with a blank column in between.

How to copy a column from a specific row all the way down to the last row with values?

I have two columns.. "AE" and "AG"
The headers are in row 4 of the worksheet and all the data starts from row 5 onwards.
I just want to copy the whole column of "AG" starting from "AG5" until the first blank row and paste it AS values into "AE" in the exact same range (starting from "AE5"). How can I go about doing that?
Try below.
With ActiveSheet
LR = .Range("AG1048576").End(xlUp).Row
.Range("AG5:AG" & LR).Copy
.Range("AE5").PasteSpecial xlValues
End With
You may need to change Activesheet to
sheets("NAME OF SHEET")
Value transfer is faster than a copy and paste.
dim lr as long
with worksheets("sheet1")
lr = .cells(.rows.count, "AG").end(xlup).row
.range(.cells(5, "AE"), .cells(lr, "AE")) = .range(.cells(5, "AG"), .cells(lr, "AG")).value
end with

Find last row > merge cells > copy and paste into it - Excel VBA macros

I have no experience with VBA and it's proving to be more difficult than what I imagined...in part because I don't know the syntax, but I have the following:
Sub testMe()
LastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
Worksheets("Sheet2").Range("A1").Copy Destination:=Range("A" & LastRow)
End Sub
This kinda works, but it's jamming everything into one cell in the first column. How do I merge the cells of the last row before pasting into it? The macro is supposed to find the last row of the last page, merge the cells of that row, and paste text that was copied from another cell. Thank you in advance.
This should do what you're after. You should just change the column number to reflect the column which you wish to merge cells until.
Option Explicit
Sub copy_and_paste_merge()
Dim last_row As Long
last_row = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(1, 1).Copy
Cells(last_row, 1).PasteSpecial Paste:=xlPasteValues
Range(Cells(last_row, 1), Cells(last_row, 5)).MergeCells = True 'change the column
End Sub
I ended up doing it like this...
Sub testMe()
LastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
Range("A" & LastRow & ":L" & LastRow).Merge
Range("A" & LastRow) = Worksheets("Sheet2").Range("A1")
End Sub

Resources