For Loop delete row, loop is skipping row [duplicate] - excel

This question already has answers here:
VBa conditional delete loop not working
(4 answers)
Closed 4 years ago.
I have the following For Loop. It is supposed to delete lines with all the . 's.
It is deleting the lines, but when there are rows are like the picture, the code is skipping one of them and deleting one of them.
For row_num = 8 To max_row
page_title = Sheets("Stack").Range("C" & row_num + 1).value
If page_title = " ....................................................................................................................................................................................................................................................................................................." Then
Sheets("Stack").Range("C" & row_num + 1).EntireRow.delete
End If
Debug.Print page_title
Next row_num
Can anyone help with deleting sequential rows that contain .'s ?

This is beacuse the logic of the routine. For example, if you delete row 5, now row 6 will be row 5 and so on, so row 6 will not be read. Insted of delete the row, try setting the values of the column as Empty
Sheets("Stack").Range("C" & row_num + 1).EntireRow = Empty
And you will see all lines will be read.
After you finish, you can tell excel/vba to roganize your matriz according to one column, so empty rows will be organized at the end, is that to say, will become part of the rest empty spacies of the sheet

Related

Looping Through a Range to Remove N/As [duplicate]

This question already has answers here:
Excel VBA: Delete rows if value not equal to a value?
(2 answers)
For Loop not fully cycling in excel VBA [duplicate]
(1 answer)
For Each loop won't delete all rows with specific values
(2 answers)
Closed 1 year ago.
I have a set of data where I'm trying to remove the N/As and shifts the data to the left. Here's my piece of code:
For Each Cell In Range
If Cell.Value = "NA" Then
Cell.Delete Shift:=xlToLeft
End If
Next Cell
However when I have two consecutive NAs, the first NA delete will shift the second NA into the first cell, where the loop is not moving onto the next cell, so as an end result I would have to run the macro multiple times to get all the NAs deleted. I tried to write a Do Until loop on top of it saying something like "do until Range.value <> "NA"", and it's erroring out. What's the best way around this?
Thanks advance for the help!

Macro only running through first result [duplicate]

This question already has answers here:
Excel VBA Delete Rows
(1 answer)
Faster way to delete rows 40k+ rows at once
(2 answers)
Closed 3 years ago.
The below code only seems to work on the first result. I have to re-run it for the other results to be removed. Could anyone take a look and tell me why please? Thanks
I've tried copy and pasting the code several times to compensate but it gives me an error about duplicate code
Dim cell As Range
For Each cell In [AE1:AE2000]
If cell.Value = "REMOVE" Then Range(cell.Offset(0, -5).Address & ":" & cell.Offset(0, 0).Address).Delete Shift:=xlUp
Next cell
One thing you must do when deleting rows is loop backwards; otherwise you may skip rows, which is what is happening to you by the sounds of it. You have to use a counter when looping backwards, such as
Sub x()
Dim r As Long, cell As Range
Set cell = Range("AE1:AE2000")
For r = cell.Count To 1 Step -1
If UCase(cell(r).Value) = "REMOVE" Then
Range(cell(r).Offset(0, -5), cell(r)).Delete Shift:=xlUp
End If
Next r
End Sub

Merging text in two columns

How to locate and merge text in two columns on my spreadsheet and delete the second one?
The first one has the title Postnr and the second one has the title Postort. I want to merge the text in these columns with two spaces between the original text.
Example:
| Postnr | Postort |
| 752 65 | Gothenburg |
Result after I run the code:
Postaddress
752 65 Gothenburg
My code to find and select Postnr
Dim rngPostnr As Range
Set rngPostnr = Range("A1:Z1").Find("Postnr")
Range(rngPostnr, rngPostnr.End(xlDown)).Select
I understand how to do it in the sheet, but I want a macro since I do this many many times a day.
I don't want to locate and mark these columns manually since my sheets have many columns.
I need a macro that locates the columns and concatenates them and removes them and make a new column with the concatenated values, preferably with the header Postaddress.
I understand you dont know where the column is located (but its in the first row).
First iteration on the first row will find you the columns, then iterate over the values per the found column numbers, build the new string and write it in column 10 (or any other column). You can add further logic per your needs, such as, when to stop iterating (when one lastRow is bigger than the other, etc...)
Here is the basic working code that you can expend later:
Sub findAndConcat()
'last column with data
lColumn = Cells(1, Columns.Count).End(xlToLeft).Column
'find Postnr and Postort columns
For i = 1 To lColumn
If Cells(1, i) = "Postnr" Then
PostnrColumn = i
'Convert from column number to column letter
PostnrColumnName = Split(Cells(, PostnrColumn).Address, "$")(1)
End If
If Cells(1, i) = "Postort" Then
PostortColumn = i
PostortColumnName = Split(Cells(, PostortColumn).Address, "$")(1)
End If
Next i
Dim PostnrValue As String
Dim PostortValue As String
Dim newString As String
'last row with data per column
PostnrLastRow = ActiveSheet.Cells(Rows.Count, PostnrColumnName).End(xlUp).Row
PostortLastRow = ActiveSheet.Cells(Rows.Count, PostortColumnName).End(xlUp).Row
'Iterating the columns rows and building the new concatinated string
For i = 2 To PostnrLastRow
PostnrValue = Cells(i, PostnrColumn).Value
PostortValue = Cells(i, PostortColumn).Value
newString = PostnrValue & " " & PostortValue
ActiveSheet.Cells(i, 10).Value = newString
Next i
End Sub
Here is the result:
If you want to do that inside the sheet, then on the formula barn you should use concatanate like the following: =CONCATENATE(A2;" ";B2)
If you want to achieve that in vba you have to use something like the following:
ActiveCell.Offset(0, 3).Value = ActiveCell.Value & " " & ActiveCell.Offset(0, 1).Value
Where activecell (Postnr) is the first cell, and the next cell (offset 0,1|Postort) is the second part you want to concatenate and the result is inserted further down into a new column.
Edit: Here is the results to understand the example given:
Edit 2: Because you question is not clear enough I will add another way. Maybe you want to "add" the second column to the first (merge the columns into one).
You can do that using vba only.
You have to do something like that:
ActiveCell.Value = ActiveCell.Value & " " & ActiveCell.Offset(0, 1).Value
I understand how to do it in the sheet, but I want to have macro that does it since I do this many many times a day.
I also understand your vba but for this to work I need to mark the cells or does it locate the postnr and postadress columns by itself. I dont want to have to locate and mark these columns myself since this is time consuming because my sheets have many many columns. Do you understand what I mean?
I need like a macro that locates the columns and concatenates them and removes them and make a new column with the concatenated values, preferably with the header Postaddress

Macro to insert line break in excel rows in specific column [duplicate]

This question already has answers here:
Add line breaks in cell to 1000 rows of data
(3 answers)
Closed 6 years ago.
I am naïve to macros and I need a macro button to enter line break at the end of every data in the row. I have around 1000 rows of data in specific columns. I need to apply this to selected columns. I am currently using ALT+ENTER, but it is time consuming.
Any help would be much appreciated.
I am currently using below code
Sub Macro
Dim Stem As Variant
Stem = ThisWorkbook.Worksheets ("Sheet1").Range("C2")
Range ("K2").Select
Range("K2").FormulaR1C1 = Stem & Chr(10) & ""
End Sub
Above code copies only C2 data and paste in K2 and apply formula. But I need all data in column C2:C to be copied and pasted in K2:K.
Thanks
If I understand that correctly, you want to loop through the copied records in column K and add a line break?
In that case you can use this (this will copy all columns starting from 1) and loop through cells in K starting from 2, you can change that if needed:
Sub copyAndNewLine()
'copy column C to K
Columns("C").Copy Destination:=Columns("K")
'loop through all cells in K and add new line
For i = 2 To Cells(Rows.Count, "K").End(xlUp).Row
Cells(i, "K").Value = Cells(i, "K").Value & vbCrLf
Next i
End Sub

Copying rows from one table to another [duplicate]

This question already has answers here:
Copy every nth line from one sheet to another
(8 answers)
Closed 7 years ago.
Suppose I have a table in excel containing 1000 rows and 10 columns.
How can I copy every 7th row from this table to a new table whose first row will be this 7th row, second row will be that table 14th row and so on.
I have never done these kind of things in excel before.
How to do it?
If you want to stick with plain Excel (no VBA). Add two columns at the end of your table. The first being a count of the line, the second flagging if the line count is divisible by 7 - I used the formula =IF(MOD(D4,7)=0,"Divisible by 7", "-").
Then filter the table on the 'Mark every 7th item' column, and copy and paste to new table.
You need a macro. Press alt + F11
Basically you run that macro that goes like this
sub Copyer()
dim I as integer
Dim K as integer
I = 7
K = 1
while (Activesheet.Range("A" & I ).Value <> "")
DestinationSheet.Range("A" & K ).Value = Activesheet.Range("A" & I).Value
K = K + 1
I = I + 7
Loop
End Sub
Code may need some grooming but that's the idea

Resources