Loop through rows and edit them in VBA Excel2010 - excel

I am creating an interface with data entries.
These entries (1 per row) shall then be added to another entry in the same row.
Apparently it does not select the right rows and starts above row 4.
For Each row In Worksheets("A").Range("D4:D11") 'Start at row 4
.Cells(5, row) = wse.Cells(6, row) + wse.Cells(4, row)
'This is supposed to add entries from each looped row to cells in columns D and F into E.
Next row
Some of the entries even appear in columns A and B, while others appear in rows way below.
I am quite surprised that I was not able to find a solution for my problem, but maybe you can help me :)
Help is much appreciated. Thanks in advance!

In your case, row isn't what you think it is, it's a Range object.
You want something like:
For Each Rng In Worksheets("A").Range("D4:D11") 'Start at row 4
Worksheets("A").Cells(5, Rng.Row) = wse.Cells(6, Rng.Row) + wse.Cells(4, Rng.Row)
Next Rng

Related

Clearing multiple filtered columns using SpecialCells(x1CellTypeVisible)

I have a macro I set up which filters on column K and clears a second column e.g. column AA for these filtered amounts.
Range("AA5", Range("AA5").End(xlDown)).SpecialCells(xlCellTypeVisible).ClearContents
I have two questions:
Main question - how do I now update to include multiple columns which are not connected from row 5 downwards, e.g. AJ, AS, BB etc.?
Assuming my headers are in Row 4, will the above cause any issues when filtering for rows starting only after row 5, or is there any better practice?
I'm OK reading code from my college computing days, but bear in mind I'm not a techy person
A problem I had when simply having multiple lines of the same code was that it would be dynamic and therefore would remove some rows from the filter in column K once for example column AA was cleared, and thereby not clearing all the relevant cells in AJ or AS.
Taking into account that you will be clearing all visible cells in every 9th column, from row 5 to the last used row in the column, then this macro should work.
With ThisWorkbook.Sheets("Sheet3") 'change the sheet name as required
'For loop to start at col AA and loop to the last column, jumping to every 9th column
For i = 27 To .Cells(5, .Columns.Count).End(xlToLeft).Column Step 9
'set the range from row 5 to the last row and clear visible cell in the column
.Range(.Cells(5, i), .Cells(.Rows.Count, i).End(xlUp)).SpecialCells(xlCellTypeVisible).ClearContents
Next i 'loop to the next column
End With

Duplicate in column

I'm trying to write a code to solve this little issue that I have, but can't seem to get it. I have multiple columns in an excel spreadsheet and in one of those columns, there are duplicate values. What I want to do is to remove the second/duplicate value but also take the integer value in one of the other columns and add it to the row where the first value is and after that delete that "second" row. I tried with the .RemoveDuplicates command, but it just deleted the duplicate value and shifted the whole column up, so I can't add the values as I wanted.
Here's an example
I only need the duplicates removed from one of the columns, D, here we see that row 5 and 10 are similar in that column and what I want to do, is to add the numbers from column C in row 5 and delete row t´10, so I'll end up with this
I really hope any of you can help as I'm a bit lost. Thanks!
Without code, you could use the advanced copy to copy unique values into another range, sumif to get your total and index/match to bring in the other columns. Once you get that figured out, record it as a macro and clean it up.
Resume your data with Pivot Tables.
Your inputdata looks like this:
You could resume your data using Pivot Tables, and group the data by that 4th column and sum values in 3rd column. Something like this:
This way you could create a new datarange, where you have grouped your data as you wish, excluding innecesary rows. Try it!
Work from the bottom up if you are going to delete rows. See if there is a match to the value in column D above the row you are working on. If there is a match, sum the values in column C into the matched row and remove the row you're working on.
Sub words()
Dim i As Long, m As Variant
With Worksheets("sheet1")
For i = .Cells(.Rows.Count, "D").End(xlUp).Row To 2 Step -1
m = Application.Match(.Cells(i, "D").Value, .Range("D:D").Resize(i - 1, 1), 0)
If Not IsError(m) Then
.Cells(m, "C") = .Cells(m, "C").Value2 + .Cells(i, "C").Value2
.Cells(i, "D").EntireRow.Delete
End If
Next i
End With
End Sub

VBA Code - Copying cells from one sheet to another sheet in excel depending on matching condition

I have two sheets Sheet1 and Sheet2 in excel .Sheet 1 has Columns C1,C2,C3,C4,C5.Sheet 2 has Columns C1,C2,C3.Now I have to perform 3 operations.
1.) Delete all rows in Sheet1 where values of Column 1 is not found in Column 1 of Sheet2.
2.) Replace values of C2,C3 of Sheet 2 into C2,C3 of Sheet 1 (C4,C5 remains the same) where value of C1 of Sheet 1 matches C1 of Sheet2.
3.) Append C1,C2,C3 data of Sheet 2 into Sheet 1 where values of C1 in Sheet 2 is not found in C1 of Sheet1(C4,C5 will be blank).
I am able to write the VB code for operation 1.Please help me with operation 2 and operation 3.
Sub delete_selected_rows()
Dim rng1 As Range, rng2 As Range, rngToDel As Range, c As Range
Dim lastRow As Long
With Worksheets("Sheet1")
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
Set rng1 = .Range("A2:A" & lastRow)
End With
Set rng2 = Worksheets("Sheet2").Range("A:A")
For Each c In rng1
If IsError(Application.Match(c.Value, rng2, 0)) Then
'delete incidents which are not in process
If rngToDel Is Nothing Then
Set rngToDel = c
Else
Set rngToDel = Union(rngToDel, c)
End If
End If
Next c
If Not rngToDel Is Nothing Then rngToDel.EntireRow.Delete
End Sub
First, let my make two comments regarding your solution for step 1.
You could delete the rows on the spot if you stepped through the rows from the bottom to the top. (The row indexes only change for rows below the deleted one.)
IMO, it would be better to use rng2.Find instead of Applications.Match. This method of the range object returns the cell where a match is found and Nothing in case there is no match.
Now to step 2:
Using the notation as in your solution to step 1, you can get the row of the match in sheet2 using rng2.Find(c.Value).EntireRow. Then you can use its Cells property to get the second and third column.
Step 3:
You already know from step 1 how to find out that a row does not have a match in the other sheet. You just have to copy the values from the first three columns into the row after the last row of sheet1 for each row without a match in sheet1. (Best save the last row and then increment with each copied row.)
Above, I gave a simple solution to each step. However, if you have very large tables, this might be a bit slow. Basically, with this solution you are cycling through the rows three times, at each row, querying the worksheet for a value and searching all rows in the other table for a match.
An approach with better performance would be to load the entire ranges into two dimensional arrays using the Value property of the ranges, or better Value2. Then you can do something like a merge join to solve your three problems, i.e. you could sort both arrays by the first column using your favorite n*log(n) sorting algorithm and then step through both list in ascending order. While stepping through, you can save which rows to delete, update the appropriate rows, and append the additional rows. To enable this, you should keep track of the original rows while sorting the arrays. Finally, you would go through the rows marked for deletion from bottom to top and delete them. (You cannot delete right away since this messes up the row indexes for the rows below the deleted one.)
Yet another approach to implement your three steps, which combines ease of use and performance, is to write SQL queries against your sheets via ADODB.
Your first step would look something like this.
DELETE [sheet1$] WHERE [sheet1$].'header of first column' NOT IN (SELECT [sheet2$].'header of first column')
Step 2 would be an update statement and step 3 an insert into statement.

Insert blank rows in excel if row above is not empty

If A1 is not empty (contains actual data), is there a way to if-then so that I can insert a blank row under A1. This should apply to any cell/row as I have a spreadsheet with about 65000 rows and they want to have blanks to separate the rows that contain different identifiers. I looked on here, right before I signed up, and I saw some things about empty cells or making cells empty depending on other cells, but that doesn't seem to be what I'm looking for. Google wasn't too much help either.
thanks.
Is this what you want?
Sub helping()
Dim count As Long
For count = ActiveSheet.UsedRange.Rows.count To 1 Step -1
If Information.IsEmpty(Cells(count, 1)) = False Then Rows(count + 1).Insert
Next count
End Sub

How do I select a range of cells depending on number of rows in the sheet?

I currently have multiple data sheets with information in columns A-I in all of them, and I want to select a range of cells in each. Column A varies between 150 rows to 450 rows of data depending on the sheet. I want to select columns A-I but only up through the last row found in column A. I used the last row function:
LastRow = Range("A65536").End(xlUp).Row
How do I get the macro to select columns A-I up through the last row? I want it to function as if it was written:
Range("A1:ILastRow").Select
where the "LastRow" is the number of the last column.
Thanks,
James
... You're EXTREMELY close - just use this:
LastRow = Range("A65536").End(xlUp).Row
Range("A1:I" & LastRow).Select

Resources