How to change destination ranges when using autofill in VBA - excel

I am quite new to VBA, I am making a macro where it copies data from 'Sample' workbook to 'Etracker' workbook. After copying cell "H11" from 'Sample' to the last row of C column in Etracker, I want C column to autofill down to until where data is in J column. the code works but where it says "C16926" now, it changes every time because I constantly entering data into it. so I want to autofill it from last row in column C all the time.
I tried different ways to change "C16926" to vary but it doesn't seem to work.
Please help! and thanks in advance.
LastrowC = Etracker.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0).Row
LastrowJ = Etracker.Cells(Rows.Count, 10).End(xlUp).Offset(1, 0).Row
Sample.Activate
Range("H11").Copy
Etracker.Cells(LastrowC, 3).PasteSpecial xlPasteValues
Etracker.Activate
Etracker.Range("C" & LastrowC).Select
Selection.AutoFill Destination:=Range("C16926:C" & Range("J" & Rows.Count).End(xlUp).Row), Type:=xlFillCopy

You just need to concatenate your last row into the address like
"C" & LastrowC & ":C" & Range("J" & Rows.Count).End(xlUp).Row)
Note that you don't need to .Select or .Activate if you specify a worksheet for every Range, Cells, Rows and Columns object. Using .Select or .Activate is very unreliable and makes your code really slow.
So you should end up with something like
Dim LastRowC As Long
LastrowC = Etracker.Cells(Etracker.Rows.Count, "C").End(xlUp).Offset(1, 0).Row
Dim LastrowJ As Long
LastrowJ = Etracker.Cells(Etracker.Rows.Count, "J").End(xlUp).Offset(1, 0).Row
Sample.Range("H11").Copy
Etracker.Cells(LastrowC, "C").PasteSpecial xlPasteValues
Etracker.Range("C" & LastrowC).AutoFill Destination:=Etracker.Range("C" & LastrowC & ":C" & LastrowJ), Type:=xlFillCopy

Related

Copy Paste a variable range with variable row number i to another cell

The following code is working in my mind but not in vba:
Dim i As Integer
If Range("H11") = i Then Range("U" & i & ":X" & i).Select
Selection.Copy
lrij = Cells(Rows.Count, "A").End(xlUp).Row
Range("A" & lrij + 1).PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Range("H11").ClearContents
Range("A1").Activate
i is a variable row number.
The purpose is if I enter "2" for example in Cell "H11", the Range ("U2:X2") will be copy pasted in Column "A", row under row.
braX already helped me with writing the variable range correctly, but I'm lacking the point here I guess. He advised me to use the "Long" data type.
Thank you in advance.

VBA, Fill up/down rows to align with last row

I am trying to delete rows till it meets my last row OR fill down rows to align with last row.
Sometimes my sheet will be like below where I need to delete rows to align with my last row number.:
However sometimes my sheet will be like below, where I need to fill down other columns:
Is there a function that can do this? I am finding it hard to determine when to fill up or fill down.
Thanks
keeping in mind the well known caveats of the use of UsedRange, you could give it a try
Dim lastRow As Long
With ActiveSheet ' <- change it to your actual sheet reference
With .UsedRange
lastRow = .Rows(.Rows.Count)
End With
End With
Please test the next code. It assumes that the reference column will be the seventh one and the one to check the 0 formulas value to be the sixth one. Your picture does not contain the columns header...
Sub DeleteRowsOrFillDownDiscontinuous()
Dim sh As Worksheet, lastR As Long, lastR1 As Long, lastCol As Long
Set sh = ActiveSheet
lastR = sh.Range("F" & rows.count).End(xlUp).row
lastR1 = Range("E" & rows.count).End(xlUp).row
lastCol = sh.cells(lastR1, Columns.count).End(xlToLeft).Column
If lastR < lastR1 Then
sh.rows(lastR + 1 & ":" & lastR1).EntireRow.Delete xlUp
ElseIf lastR > lastR1 Then
sh.Range("A" & lastR1, "E" & lastR1).AutoFill _
Destination:=sh.Range("A" & lastR1, sh.Range("E" & lastR))
sh.Range("G" & lastR1, "AG" & lastR1).AutoFill _
Destination:=sh.Range("G" & lastR1, "AG" & lastR)
sh.Range("AI" & lastR1, sh.cells(lastR1, lastCol)).AutoFill _
Destination:=sh.Range("AI" & lastR1, sh.cells(lastR, lastCol))
Else
MsgBox "Nothing te be processed. Everything aligned..."
End If
End Sub
Edited:
Adapted the code for F:F column as reference, AH:AH not changeable, too and existing columns to be processed after AH Column.
Please test it and send some feedback.

Excel VBA Copy/Paste Without clipboard Define Range

Sorry if this had been discussed already, I am struggling with the syntax of the ranges that I need to copy and paste. I am trying to do so without using the clipboard and found out that I could do it with .Value = .Value (Excel VBA Copy and Paste (without using Copy+Paste functions) on empty row)
There are two workbooks, I am copying from wbsource.Worksheets(1) to wb1.ws
The argument is - if column L&row has a "Yes" then ... E.g. if L5 equals Yes, then copy A5:L5 and paste in the first empty row in wb1.ws.
Part of my code is
If .Cells(rw, 12) = "Yes" Then
Dim lastrw As Long
lastrw = ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
wbsource.Worksheets(1).???? = ws.Range("A" & lastrw).Value
End If
I would really appreciate it if you could help me with the syntax of this
I believe the following should work for you:
If wbsource.Worksheets(1).Cells(rw, 12) = "Yes" Then
Dim lastrw As Long
lastrw = ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
ws.Range("A" & lastrw & ":L" & lastrw).Value = wbsource.Worksheets(1).Range("A" & rw & ":L" & rw).Value
End If

Auto-filing Formula to Last Row of a Single Column

I need to constantly start at cell R2 and auto-fill a formula down to the last row of column R. However, the number of rows is constantly changing so I need to write a macro that finds the last row and stops their. I've tried this code but keep getting errors. Any thoughts?
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("R" & Rows.Count).End(xlUp).Row
Range("R2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-4]"
Selection.AutoFill Destination:=Range("R2" & Lastrow)
The error you are getting is due to this Range("R2" & Lastrow)
The range address concatenates to R21400 if the last row was 1400. So we need to add the :R to the address.
Range("R2:R" & Lastrow)
It will now concatenate to R2:R1400
There is not need to do it with three lines, one will suffice:
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("P" & Rows.Count).End(xlUp).Row
Range("R2:R" & Lastrow).FormulaR1C1 = "=RC[-2]/RC[-4]"
End Sub
Not sure if you already tried this:
Sub InvoicePrice()
Dim Lastrow As Long
Lastrow = Range("P2").End(xlDown).Row
Range("R2").Select
ActiveCell.FormulaR1C1 = "=RC[-2]/RC[-4]"
Selection.AutoFill Destination:=Range("R2" & Lastrow)
It seems to me that Range("R" & Rows.Count) locates you out of current region

How to Paste a Range onto another worksheet with filters on

This seems like a simple task but I keep running into various errors. I need to filter worksheet B and then copy a column of data. I then need to filter worksheet A and then paste the copied data into a column.
Worksheets("SheetB").Select
lastRowOne = Range("B" & Rows.Count).End(xlUp).Row
Range("DL2:DL" & lastRowOne).AutoFilter Field:=116, Criteria1:="<>Apples"
lastRowTwo = Range("B" & Rows.Count).End(xlUp).Row
Range("DG2:DG" & lastRowTwo).AutoFilter Field:=111, Criteria1:=Target
'Target is already defined earlier in the Macro and functions fine
lastRowThree = Range("B" & Rows.Count).End(xlUp).Row
Range("DX2:DX" & lastRowThree).Copy
Worksheets("SheetA").Activate
lastRowFour = Range("B" & Rows.Count).End(xlUp).Row
Range("A2:A" & lastRowFour).AutoFilter Field:=1, Criteria1:=Target
lastRowFive = Range("B" & Rows.Count).End(xlUp).Row
Range("Z2:Z" & lastRowFive).SpecialCells(xlCellTypeVisible).Select
Selection.PasteSpecial Paste:=xlPasteRange, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
In place of the last line I have also tried:
ActiveSheet.Paste
The first returns a "Run-time error '1004':
PasteSpecial method of range class failed
the ActiveSheet.Paste returns a "Run-time error '1004':
Paste method of Worksheet class failed
Although this code is not the cleanest, it all functions with the exception of the "pasting" onto 'sheetA' in Column Z. I also need the data pasted into AA if that can be included in a fix.
Thanks !
Here's (I hope) the same macro, but without .Select/.Activate, and a little tweaking. For instance, you don't need more than one "lastRow" variable. Since you really just reset it, you can use one.
Sub tester()
' First create, then SET, worksheet variables to hold the sheets. We use these when
' referring to ranges, cells, etc.
Dim aWS As Worksheet, bWS As Worksheet
Set aWS = Worksheets("SheetA")
Set bWS = Worksheets("SheetB")
Dim lastRow As Long 'AFAICT, you only need this one Last Row variable. Just update it each time.
Dim copyRng As Range
With wsB ' working with SheetA
lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
.Range("DL2:DL" & lrOne).AutoFilter Field:=116, Criteria1:="<>Apples"
lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
.Range("DG2:DG" & lastRow).AutoFilter Field:=111, Criteria1:=Target
lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
' We now SET the range we want to copy. We can avoid copy/paste by setting two ranges equal
' to eachother. For now, let's store the COPY RANGE in a Range variable
Set copyRng = .Range("DX2:DX" & lastRow).SpecialCells(xlCellTypeVisible)
End With 'bWS
Dim pasteRng As Range
With aWS
lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
.Range("A2:A" & lastRow).AutoFilter Field:=1, Criteria1:=Target
lastRow = .Cells(.Rows.Count, 2).End(xlUp).Row
Set pasteRng = .Range("Z2:Z" & lastRow).SpecialCells(xlCellTypeVisible)
End With 'aWS
pasteRng.Value = copyRng.Value
End Sub
The only hesitation I have is the pasting to SpecialCells. AFAIK, if the paste range is different than the copy range, you might get some errors. In any case, try the above and let me know what happens.
An important thing to pay attention to, especially when using multiple worksheets, is that you should be explicit with which sheet you want to get a Range(),Cells(),Rows(),Columns(),etc. Otherwise, it's going to get that info. from the ActiveSheet, whatever that may be.

Resources