Autofill dynamic range based on another worksheet - vba

I've got 2 sheets in a workbook with different sets of data. Sheet 1 has a set of data not formatted as a table. The header row for this data is on Row 4. When I try doing a count using Range("A" & Rows.Count).End(xlUp).Row, I get the last row of that entire sheet, not the count of how many rows of data there are from my starting point.
Sheet 2 has its header row in Row 1. So when I try to use the same count of rows I mentioned above to AutoFill, I always get 3 extra rows because the Sheet 1 count is just looking at what the last row of data is.
I don't want to have to shift things around in either sheet. I just want to be able to autofill based on the same number of rows as there are in Sheet 1, beginning the count at A5 and going down to the last row of data. Is there a different count formula to start at a specific cell and only count the rows below it, and then telling the other sheet to only AutoFill based on that number that I'm missing?

You may be over thinking this. Just discount the row count by 3
Range("A" & Rows.Count).End(xlUp).Row - 3
I would qualify the objects & properties (Range & Row) as well for good measure.

could you use either:
set rng = Range("A1:A1000").SpecialCells(xlCellTypeConstants, 2)
or
set rng = Range("A5").CurrentRegion.Resize(,1)
assuming no blanks in the data range

Related

Why am I getting an object-defined error while adjust which rows I want to clear?

I have a specific template that starts it's printed array data in cell B9, but I just want rows 9-rows.count to be cleared of it's contents (not deleted because I have drop-down menus in there).
Rows(8).Offset(1, 0).Resize(Rows.Count - 1).EntireRow.ClearContents
doesn't seem to work, but it did work when I had Rows(1).Offset(1,0) etc... in there. why is it now not wanting to work when I adjusted the row number to start on?
Rows.Count gives the maximum numbers of rows in a worksheet (usually 1'048'576).
Splitting your command Rows(8).Offset(1, 0).Resize(Rows.Count - 1):
Rows(8) gives you the row 8 ($8:$8)
.Offset(1, 0) gives you the next row ($9:$9)
.Resize(Rows.Count - 1) asks the range that starts at row 9 to be resized so that it is 1'048'576 rows "high". However, that would mean row 9 until row 1'048'585, and that would be behind the maximum number of rows.
What you probably meant is to delete until the end of your data. Is is not an easy task to figure out the end of the data, usually it is done with a construct like
rowCount = Cells(Rows.Count, 2).End(xlUp).Row. This will (virtually) jump to cell B1048576, simulate the key "Ctrl+Up" and (again virtually) jumps to the last row holding data in that column. Change the 2 into any other column number (A would be 1...) if you want to check that column.
Try the following code:
Dim rowCount As Long
With ActiveSheet
rowCount = .Cells(.Rows.Count, 2).End(xlUp).Row
.Rows(8).Offset(1, 0).Resize(rowCount - 1).EntireRow.ClearContents
End With

Filter and copy certain rows from multiple excel sheets to another

I am using a workbook that has various sheets. I want to copy all the rows from the last 5 sheets that have the value "Pending" in their column "J". I want to create a new tab named "Pending week" and paste all these rows there. Any help would be really appreciated.
Thanks
You can create this yourself very easily if you just break it down:
Add a new Sheet
Name the sheet to Pending Week
Find the five latest sheets.
Create some kind of loop that copy paste row if cells in column J contains the value "Pending"
You have not provided any code, so I'll give you a base to work from:
You add a new sheet & name it using:
Worksheets.Add
ActiveSheet.Name = "Pending week"
Find the five latest sheets
To my knowledge, you can't find the latest sheets. Sheets doesn't contain the date and time of when they were created. But if we ignore that and expect the five latest sheets to be placed in the workbook to the far most right (Default position for newly created sheets). Then you need to figure out how many sheets you have and count backwards.
You can use: Worksheets.Count to count all the sheets. Use this number and count it backwards. My first thought would be to use a For Loop
Dim X As Integer
For X = (Worksheets.Count - 4) To Worksheets.Count
Next
X would be the identifier to find our latest sheets. So you should incorporate that into our loop below. You want to place the loop within this For Block.
Loop
There are many ways to find a value in a sheet, but you need to figure out what the last row of your sheets are. Without it we don't know when the code should stop.
You can use a Do Until Loop if there is a value in all J cells. Then you can simply insert the entire row into Pending week
It would look something like:
Dim XLrow As Integer
XLrow = 1
Do Until Worksheets(1).Cells(XLrow, "J") = ""
If Worksheets(1).Cells(XLrow, "J") = "Pending" Then
Worksheets(1).Range(XLrow & ":" & XLrow) = Worksheets("Pending week").Cells(XLrow, "J").Value
End If
XLrow = XLrow + 1
Loop
You will need to change the Range to the length of the range you want to copy. Note: the value Pending is case sensitive, so keep that in mind.
Alright, this is what you need to create your code. Of course you need to change values to fit your own workbook, but this is the base.

excel: correct column position

Actually, it is a simple question, although I don't know if it's possibl to do what I wanted to.
I'm just copying a column from another using this sub:
Sub copy_column()
Sheets("FROM").Columns("A").Copy Destination:=Sheets("TO").Columns("A")
End Sub
In fact, in my sheet "FROM" my first row with data is the row 3 and then when I copy to "TO" sheet it's starting from row 3 too. My idea was to place it at the row 1 at "TO" sheet.
Is there any way to write something like Columns("A"-2) to put it in the right place?
An alternative which avoids deleting rows (if you have data you want to keep in other columns for example) is to take an intersection of a range overlaid with itself, but offset...
The following code assumes you have some data in row 1 in the sheet so UsedRange would start from the correct row, but you can change the offset to accommodate missing data in the rows above
Dim CopyRange As Range
With Sheets("From")
Set CopyRange = Application.Intersect(.UsedRange, .Columns("A"))
Set CopyRange = Application.Intersect(CopyRange, CopyRange.Offset(2, 0))
CopyRange.Copy Destination:=Sheets("TO").Cells(1, 1)
End With

VBA Subroutine to fill formula down column

I have a current Sub that organizes data certain way for me and even enters a formula within the first row of the worksheet, but I am running into the issue of wanting it to be able to adjust how far down to fill the formula based on an adjacent column's empty/not empty status. For example, each time I run a report, I will get an unpredictable amount of returned records that will fill out rows in column A, however, since I want to extract strings from those returned records into different Columns, I have to enter formulas for each iteration within the next three columns (B, C, and D). Is there a way to insert a line that will evaluate the number of rows in Column A that are not blank, and then fill out the formulas in Columns B, C, and D to that final row? (I know that tables will do this automatically once information is entered in the first row, but for logistical reasons, I cannot use tables).
My current code that fills out the formula in Column B, Row 2 is:
Range("B2").Select
ActiveCell.FormulaR1C1 = "=MID(RC[-1],FIND(""By:"",RC[-1])+3,22)"
Thanks!
The formula that you actually need is
=IF(A2="","",MID(A2,FIND("By:",A2)+3,22))
instead of
=MID(A2,FIND("By:",A2)+3,22) '"=MID(RC[-1],FIND(""By:"",RC[-1])+3,22)"
This checks if there is anything in cell A and then act "accordingly"
Also Excel allows you to enter formula in a range in one go. So if you want the formula to go into cells say, A1:A10, then you can use this
Range("A1:A10").Formula = "=IF(A2="","",MID(A2,FIND("By:",A2)+3,22))"
Now how do we determine that 10? i.e the Last row. Simple
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long
'~~> Change the name of the sheet as applicable
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Find Last Row in Col A
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("B2:B" & lRow).Formula = "=IF(A2="""","""",MID(A2,FIND(""By:"",A2)+3,22))"
End With
End Sub
More About How To Find Last Row
You can use this to populate columns B:D based on Column A
Range("B2:D" & Range("A" & Rows.Count).End(xlUp).Row).Formula = _
"=MID($A2,FIND(""By:"",$A2)+3,22)"

Adjust criteria to depend on multiple values

I have been working on a code to copy the data from one specific range(always the same) and paste in another spreadsheet always in the row below. So basically, it starts pasting on row 11, but if I run again it will paste on the row 12 and there it goes.. The code has been working fine, but there is only one problem. It identifies the next empty row(to paste) based on the value of the column AP, but i want it to identify based on the values of all the columns between AP:BA. Thus, if there is any value on those cells, it should copy on the row below, not only if there is a value on AP. Does someone know how to change my code in order to solve this problem? Thank You very much
Sub Copy_Shanghai()
Dim count As Integer
count = 11
Do While Worksheets("Time Evolution").Range("AP" & count).Value <> ""
'<>"" means "is not empty", as long as this happens we go down looking for empty cell
count = count + 1
Loop
'Now count is row with first empty cell outside of top 10 rows in column C
Worksheets("Fill").Range("E5:P5").Copy
Worksheets("Time Evolution").Range("AP" & count).PasteSpecial xlPasteValues
End Sub