This question already has answers here:
Find last used cell in Excel VBA
(14 answers)
Closed 7 years ago.
Title may not make complete sense. But description below should aid. I have similar data sets that come from one of our test machines that are no different unless by the number of samples given to the machine changes.
There is a macro now that assumes 6 samples are to be measured but if more or less than 6 are done, it screws up the selection of data and then we get false numbers.
So I would like to edit it such that the VBA script intelligently iterates through a given range of cells defined by the first cell where the header for data is contained. This ideally returns the position of the last row and then the VBA script has knowledge of where to look for the specific numbers I want. i.e. cell E12 contains the header for the data I'm concerned with, and goes until E?? has no data in that cell
TL;DR - I need help with some code to give me the position of the last row that has data in it, starting from a certain cell.
I would recommend this:
Dim LastRow As Long
With Sheets("Sheet1")
LastRow = .Range("E" & .Rows.Count).End(xlUp).Row
End With
But bare in mind that:
this would start reading cells from the rows count of the sheet and go upwards until it bums into data filled cell only then it would return the index of that row.
this would return the last cell of data based on the column E. So if
you have multiple columns and you're processing data for an entire sheet, make sure to choose that one column with the most
data.
This would yield an error if you there are merged cells in the column.
You can then construct your intelligent data range.
it would be something like:
Dim r As Range
Set r = Range("A1:B" & LastRow)
Related
I need to merge several sheets and maintain data integrity. I have managed to reduce the problem to the following example and would really appreciate some help. The two columns have to be merged into one. The data in a column has to stay in the order it is originally in as each cell is a code that refers to time data. The duplicates need to be reduced to one entry as the whole data set will later be geo coded and duplicates are not tolerated.
The data is complicated and messy but essentially the problem i cannot solve can be artificially reduced to the following for simplicity:
[edit: Yellow bus stops at the following stop codes in the given order.
Blue bus stops at the following stop codes in the given order.
The order in which the buses stop cannot be changed.
The output is one list including all of the stop codes only once, retaining the order present in both lists.]
I want to change it to this:
The colour coding is just to make it clear to readers here.
The green represents where duplicates have been reduced to one (this will allow me to vlookup the data against the code and enter the data from both sheets in the same row.
The order that the codes are in must be maintained so i think i just have to move all the data down.
So, this is, (conceptually), what i was trying to do as a first stage.
Each row retains the order of data but is spread across enough rows to have enough space for both columns in one. The duplicates take the position of the one which has greater row number.
The rest of the solution follows procedurally. I can then delete duplicate codes, and create a new list with all of the values, then remove the spaces.
So if i can get to there i should be ok.
Therefore, as far as i can tell, i need to match with two formulas:
=MATCH(A1,$B$1:$B$11,0)
=MATCH(B1,$A$1:$A$11,0)
but i am not sure if this is actually the direction to take.
It gives me the row numbers on which the common entries occur, but I am not sure how recreate the necessary positions.
Perhaps i need to create these dummy columns and then count the necessary total rows, but how to do that between errors?
Or, have a dynamic running offset. But not sure how to do that either. I am finding it very difficult to wrap my head around it.
It has occurred to me that each value will be on the row number equal to the number of unique values in the two columns above it.
Many thanks in advance for any pointers / solution.
I have tried to reduce the problem down and make it as clear as possible. If anyone can see the solution and can also see where i need to focus my learning, advice on specific area of training would also be welcome.
Cheers
D
I think your problem is more easily addressed with VBA. Let's assume you are working in columns A and B (meaning, 1st and 2nd columns). In your example you have two columns and 11 rows, and since you want to handle your data row-by-row, row is the outer loop and column is the inner loop:
Sub Indexing()
Dim irow, icol, count As Integer
Dim lookIn, FoundRange As Range
count = 1
For irow = 1 To 11:
For icol = 1 To 2:
Cells(irow, icol).Select
Selection.Copy
Set lookIn = Range("F1:F" & count)
Set FoundRange = lookIn.Find(what:=Cells(irow, icol).Value, lookIn:=xlFormulas, lookat:=xlWhole)
If FoundRange Is Nothing Then
Range("F" & count).Select
ActiveSheet.Paste
count = count + 1
End If
Next icol
Next irow
End Sub
This yields the following result, output in column F:
Note that the order is slightly different from yours, but it sort of makes more sense. In your result example, you have 113 before 68, although the first number to appear in row #4 is 68. There are other differences so maybe I misunderstood the order you wanted.
You can adapt this code as follows:
If you have more columns, just replace the 2 in the icol loop by whatever number of columns you want.
If you have more rows, just replace the 11 in the irow loop by whatever number of rows you want.
If you want this to be written in a different column (not column F, as it is now), just replace F with the column letter that you want.
If you are not familiar with Macros and need help setting this up, let me know.
I have a Excel sheet, which includes many groups and subgroups. Each subgroup has their own subtotal. And as long as I can't know how many lines users will require for the subgroups, I only left 3 rows blank for each subgroup and created a button which triggers Automatically Copy and Insert Copied Row macro.
Now I need to avoid users to trigger the button while they are on the First and the Last row of any subgroup. (If you copy and insert first row, subtotal formula doesn't recognise the new added line because it will be out of the range and Last row has another specific formulas which I don't want users to copy above)
I've been searching SO and the other Excel blogs for a long time but because of my list is very dynamic and have many different subtotals none of them was suitable to my case.
How can I define all of the first and last rows of my different subtotal ranges (and maybe their Title's rows) to avoid them being copied and then inserted above?
I can imagine 2 ways to do this. When the "New Row" button is pressed check if the current row is a headline/subheadline whatever …
by checking its format (eg specific background color etc).
If Not Worksheets("Sheet1").Range("A" & iRow).Interior.Color = 15004911 Then
'copy that row
End If
or by using an extra helper column that specifies the rows as header rows or non copyable rows. If you don't want the helper column to be seen you can hide it.
If Not Worksheets("Sheet1").Range("X" & iRow).Value = "Header" Then
'copy that row
End If
'where X is the helper column
And if it is a headline row then deny the copy process.
One way that doesn't involve programming have your sum range extend into rows that won't change. So you could start the subtotal in the header row (assuming there is no content in the relevant cell in that row). Another way is to have a hidden row at the top and bottom of each sum range and that is included in the sum range. So you would sum rows 10 to 14, but 10 and 14 would be hidden (and empty) and the user would just get shown rows 11, 12 and 13. Adding a new row would push the hidden rows down and extend the subtotal.
Another way is to use the indirect function.
Say your formula was
=SUBTOTAL(9,H7:H10)
If you use indirect for the lower bound, it will always refer to the cell immediately above, regardless of how many rows are added in between.
=SUBTOTAL(9,H7:INDIRECT("H"&ROW()-1))
And taking it one step further, use the upper title row as the anchor to always add up the gap between the title and the subtotal.
=SUBTOTAL(9,INDIRECT("H"&ROW(H6)+1):INDIRECT("H"&ROW()-1))
Does anyone know how to select entire column but only used cells.
Normally the data is continuous. One issue is that sometimes the sheet is filled only for two rows sometimes for hundreds.
What I need to obtain is to loop for all the files in the folder and copy recognized columns only with data. I cannot have selection of entire column because while pasting one below previous macro will throw an error due to range area not fitting.
The source data begins in different rows. Once it starts in second row the other time in third. There are no headers.
I know this is old, but this is an easy way to select the data in a specific column and move it to a new location. It will remove all blanks in the range.
Columns("E:E").SpecialCells(xlCellTypeConstants).Copy Destination: = ActiveSheet.Range("F1")
MaxRow = Range("E" & Rows.Count).End(xlUp).Row
Range("E1:E" & MaxRow).Select
Where E is your Column
I am trying to create a macro which dynamically performs a vlookup for a worksheet w/ a constantly increasing number of rows; I'd like to write the code so that it runs the vlookup from Q2 to the last populated row in col. Q (whatever that may be), and so far I have come up with this:
Range("Q2", Range("Q2").End(xlDown)).Value = "=VLOOKUP(A2,sheet2!A:BO,67,FALSE)"
..The macro performs everything it needs to do & changes accordingly for each row but it also runs infinitely down the column after there is no more data. I wanted to know if there's a way to do this in one line of code if possible. I'm sort of new to VBA & couldn't find an already-posted answer which sufficiently solved my problem so any help would be greatly appreciated.
This should work according to the information you provided. I'm guessing you are replacing an existing value with the formula otherwise the row lookup is going to return a value you don't want. If you want the last row from another column just change the Q in Cells(Rows.Count, "Q").End(xlUp).Row to the column you want.
Range("Q2:Q" & Cells(Rows.Count, "Q").End(xlUp).Row).Value = "=VLOOKUP(A2,sheet2!A:BO,67,FALSE)"
This question already has an answer here:
copy a certain range of one sheet to a certain specifed cell in other sheet
(1 answer)
Closed 9 years ago.
I am working in an organisation, need help.(consider a sheet having 200 entries from A1:H200)
Now My boss wants:
1) textbox
2) commandBox
These two should be use for searching.
After this, the searched cell's entire row should be copied and get pasted at a new sheet.
Now,as many times he searches the data should start after other.
Example:
A1:H1 then next from A2:H2 then fom A3:H3 & so on.
Please Help
You should find your answer on this thread.
copy a certain range of one sheet to a certain specifed cell in other sheet
Sheet1.Range("A5:B12").Copy Destination:=Sheet2.Range("A4")