VBA - Copy / Paste to multiple columns with variable row start (array) - vba

I'd like to shorten my code by putting multiple columns to one command.
I'm copying formulas from one row but specified columns and want to paste to another sheet in the same wksht. Just the row number is a variable. Maybe do this by an array?
As an example I'm pasting below not working code with only 3 columns.
Sub Copy()
Dim LastrowQ As Long
Dim LastrowR As Long
LastrowQ = Cells(Rows.Count, "B").End(xlUp).Row
LastrowR = LastrowQ + 1
Workbooks(Rep1).Worksheets("Formula").Range("A2, C2:F2, N2, V2:AD2").Copy
*Workbooks(Rep1).Worksheets("Report").Range("A" & LastrowR, "C" & LastrowR & ":F" & LastrowR, "N" & LastrowR, "V" & LastrowR & ":AD" & LastrowR).PasteSpecial Paste:=xlPasteFormulas*
End Sub
Could you please help to modify the code in paste part as it doesn't work right now? This would allow to make original code much shorter and hopefully faster.
What if I'd like to propagate function in second sheet from variable row till the specified row at the same time? ("A" & LastrowR & ":A" & LastrowS,...)
Thanks in advance

Related

Select one cell in each sheet to put it in another sheet

I am currently building a macro for Excel in order to read hundreds of txt files. For now everything work well and each txt file has its sheet.
I would like to take the value of a precise cell in each sheet (that would always be at the same place) and put it in a created sheet.
It is the value of the cell that I can't get to work, because I don't know how to say in VBA "this cell equal the value of this cell in this sheet, without any R[]C[]."
The naming of the sheets is precise and in function of a concatenated loop of 3 parameters.
Here is what i have done (the sheet result is created at the begining) :
'###loops k, j, i
ActiveSheet.Name = querie 'querie =k & j & i basically
Sheets("Results").Select
Range("A" & j - 1 & i).Select
ActiveCell.FormulaR1C1 = "= querie & "!" & "M1""
Next i
Next j
Next k
Hope it is clear,
Thanks a lot !

Copy data up to last row and column

I am trying to copy all the data from multiple workbooks to a single workbook and distribute them to different worksheets.
How should I change the code below to automatically select the data up to the last row and column with value and copy them instead of declaring the range?
For i = 16 To ws.Range("I" & Rows.Count).End(xlUp).Row 'Sheet1 is MasterSheet
File = ws.Range("C" & i) & ws.Range("I" & i) 'File Location and Excel Name
Copy = ws.Range("U" & i) & ":" & ws.Range("V" & i) 'Range to be copied
Workbooks.Open File, 0, 1 'Open File as Read Only
Range(Copy).Copy
ThisWorkbook.Sheets(ws.Range("W" & i).Value).Cells(Rows.Count, 1).End(xlUp)(2).PasteSpecial 12 'Paste as Values only
ActiveWorkbook.Close False 'Close WorkBook without saving
Selection.Value = Selection.FormulaR1C1 'Force F2 and Enter selected range
Range("A1").Select
Next i
Your solution works fine, Paolo. I did quite a bit of research on the topic some time ago and I found out these ways are the fastest and simplest ones. I don't think Excel is very good with these things, so I have always tried to stick to the basics.
Just two comments:
I would not use .UsedRange.Copy, since it will return the range of cells that have ever been used.
I would also try to avoid using.select as much as possible. You can find examples on how to avoid it in
How to avoid using Select in Excel VBA

Using a wildcard with a cell reference. What am I missing?

In Sheet A are a list of names. I am attempting to count the different names on Sheet A in a table on Sheet B, Column B. The names are not exact and I will be referring to the cell and not writing in the name. The sub below almost works but I think I am not using the wildcqard correctly. Please help if you can. thanks in advance.
Sub Countif_Crr_Cnt_Until_LastRow()
Dim LastRow As Long
Dim wb1 As Workbook
Set wb1 = Workbooks("macro all client v.01.xlsm")
LastRow = wb1.Sheets("A").Range("A:A").Find("Overall - Total", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For i = 21 To LastRow
Cells(i, 10) = Application.CountIfs(wb1.Sheets("B").Range("B:B"), "*" & Cells(i, 3) & "*")
Next
End Sub
I believe the issue is the unqualified references when you use just Cells(). VBA needs to know what sheet you expect that to be on, when using multiple sheets. I'm assuming that the Cells(i,10) and Cells(i,3) are to be on Sheet "A". If not, just change the sheet name:
wb1.Sheets("A").Cells(i, 10) = Application.CountIfs(wb1.Sheets("B").Range("B:B"), "*" & wb1.Sheets("A").Cells(i, 3) & "*")
As you can see, that's kind of long and a little tricky to read. An alternative way of doing that, is use With:
With wb1.Sheets("A")
.Cells(i,10) = Application.CountIfs(wb1.Sheets("B").Range("B:B"), "*" & .Cells(i,3) & "*")
End With
So, wherever you see a simple .Cells() without anything before the ., it's going to use what follows With. So it's the equivalent of the formula above...does that make sense?

Autofill to new last row from previously last row after data is added?

I have a database that is growing quite large. Every day I add about 100 rows of information.
I have about 20 columns that autofill with calculations, etc. A few of those columns pull from a VERY large file using Vlookups. That takes forever because it's pulling the entire column everyday because my current autofill macro starts in row 2.
Is there any way to write the macro so that it autofills from the previous "last row" so it's only autofilling 100 or so new rows instead of several thousand?
I have tried the following with no luck:
Range("BZ2").AutoFill Destination:=Range("BZ2:BZ" & LastRow) is an example of one of my VBA codes for an autofill I use. LastRow is the last row after the new data is pasted in. I would like it to ideally start at the OldLastRow which would be the last row before I paste in the new data. I tried Range("BZ" & OldLastRow).AutoFill Destination:=Range("BZ" & OldLastRow & ":BZ" & LastRow) without luck.
Try this.
Dim LastRow As Long
With ActiveSheet
'This will get you the OldLastRow. Add +1 to the end to get the first empty row if you need it.
'The "A" in .Rows.Count, "A" should be replaced with a column that will always have data in every row.
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range("BZ2").AutoFill Destination:=Range("BZ2:BZ" & LastRow)
'This will pause the macro until Calculations are complete.
Do While Application.CalculationState <> xlDone
DoEvents
Loop
End With
Ideally you would be doing the autofill from the point that you pasted your data in, so you wouldnt want to have "BZ2" anywhere in there if you just want to autofill new data.
you could try this one as well.
Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "BZ").End(xlUp).Row
.Range("BZ2").AutoFill Destination:=Range("BZ" & LastRow & ":BZ" & Cells(Rows.Count, "BY").End(xlUp).Row)
Do While Application.CalculationState <> xlDone
DoEvents
Loop
End With
This is assuming you only have one column of calculations. if not change the "BZ" & LastRow & ":BZ" to "BZ" & LastRow & ":CA" or whatever your last calculated column is.

Using SUMIF until last row in vba

I'm trying to incorporate a SUMIF formula into my macro. The code in excel looks like this: =SUMIF('WSO Interest' H2:H46, '20140618 Loans' D10, 'WSO Interest' S2:S46)
I set my dim as i and lastrow as integer
I already set that so that I can find the last row of the column and continue my loop until the last row.
I also used
Sheets("20140618 Loans").Select
Range("A10").Select
Selection.End(xlDown).Select
lastrow = ActiveCell.Row
to find the last row filled with text.
This is what
I have so far:
Range("W10").select
For i = 10 to lastrow
SUMIF formula would go here
Next i
So basically what I'm trying to do is use the SUMIF formula in my macro to start at W10 and keep calculating the SUMIF formula until it reaches the last row. Thanks for the help and feel free to ask any questions.
You can use the Formula property of a Range object to set the formula.
Range("W" & i).Formula = "=SUMIF('WSO Interest'!H2:H" & lastrow & _
", '20140618 Loans'!D10, 'WSO Interest'!S2:S" & lastrow & ")"
The & operator is used to concatenate strings together.
Dim r As Range
Set r = Range("W10", Range("W10").End(xlDown)) --Change it per requirement .....
For Each cell In r
Debug.Print cell.Address --Your Code goes here I guess
Next