Excel VBA deleting cells that contain certain String Values [duplicate] - vba

This question already has answers here:
Deleting Duplicate Visible Rows
(2 answers)
Code not deleting every non numeric row
(1 answer)
VBA For loop not exiting
(3 answers)
Delete specific rows from selection in for each loop
(1 answer)
Excel ListObject Table - Remove filtered / hidden rows from ListObject table
(1 answer)
Closed 4 years ago.
I am trying to write a code that goes row by row checking to see if a certain column in that row contains the string values "Pick-Up" or "Pickup", and if it does delete that entire row. The code I currently have runs, and deletes some of the rows containing said values. But not all of them.
I have tried the AutoFilter method, and for my purposes I do not believe that it will work.
Any help would be greatly appreciated. Thanks!
Sub Delete()
Dim x As Long, y As Long
Dim testString As String
y = Worksheets("Data").Range("A1", Range("A1").End(xlDown)).Rows.Count
For x = 2 To y
testString = Worksheets("Data").Cells(x, 27).Value
testString = Trim(testString)
If testString Like "*Pick-Up*" Or testString Like "*Pickup*" Then
Rows(x).EntireRow.Delete
Else
x = x + 1
End If
Next x
End Sub

Related

Is there a limit to how many cells I can assign to an array? [duplicate]

This question already has answers here:
Arrays in Excel VBA. At some point it puts NA instead of the value
(1 answer)
Best workaround for VBA Transpose array length limit?
(3 answers)
Why is my array breaking the column when I use TRANSPOSE to paste it into a worksheet?
(2 answers)
Closed last year.
I am trying to assign all the values from a column to an array using VBA.
Dim all_datapoints() As Variant
With ActiveSheet
'get number of datapoints in the desired column
n = .Cells(1, 4).End(xlDown).Row
'assign column values to array
all_datapoints = Application.Transpose(.Range(.Cells(1, 4), .Cells(n, 4)))
End With
The value of n is about 240,000 but the array all_datapoints() only contains 42,783 datapoints. I looked over my datasheet and there are no N/A, Error, or missing values in this column (should just be a column of strings). I am not sure why it refuses to get the rest of the data.
Even if I include ReDim all_act_molecules(1 to n) before assigning the range, it limits to only grab the first 42,783 values. Help!

For loop from 1 to incrementing value [duplicate]

This question already has answers here:
Change length of For loop while in the loop
(3 answers)
Closed 4 years ago.
I'm trying to run "for loop" certain times and occasionally add an extra row in the range being under the loop. Let's say, that example has originally 15 rows, and during the loop, the condition is true 2 times, so it should add +2 to the total number of rows. However, the code doesn't seem to execute the loop for those added rows and exits right after passing the value of orc=15.
Code below:
sub loop_to_orc()
dim i as integer, orc as integer
dim operations as range
set operations = range(cells(1, 1), cells(, 1).end(xldown))
orc = operations.rows.count
for i = 1 to orc
if cells(i,1)>0 then
rows(i+1).insert
end if
orc = operations.rows.count
next i
end sub
Where am I wrong? Is there any method to actually run the loop for added rows?
You could introuduce one additional variable:
Dim rowsAdded As Long
rowsAdded = 1
And then use it like this:
for i = 1 to orc
if cells(i,1)>0 then
rows(i+rowsAdded).insert
i = i - 1
rowsAdded = rowsAdded + 1
end if
next i
This way, you won't increment i when rows is inserted, but you select i in a loop, so we have to add rowsAdded.

How do you delete multiple columns (possibly non-consecutive) based on an array of number? [duplicate]

This question already has answers here:
workaround named range character limit
(3 answers)
Closed 5 years ago.
Suppose you have an array of numbers, and they are the column numbers that you would like to delete. A smart idea is to convert them to letters and then concatenate them and delete all the columns, as shown here by #Siddharth Rout. But there is a problem, it seems there is an upper limit of string inside range, so say
str = "AB:AB,CJ:CJ,CZ:CZ,NJ:NJ,NK:NK,NL:NL...",
Len(str)=300, 'Just about 50 columns, not too many indeed, there are 16384 columns in Excel 2010!!!
Chances are you will get an error if you use Range(str).Delete Shift:=xlToLeft, how to solve this problem?
Another option
Option Explicit
Public Sub DeleteColumns()
Dim i As Long, arr As Variant
arr = Split("3-5-7", "-") 'ascending order
Application.ScreenUpdating = False
With Sheet1
For i = UBound(arr) + 1 To LBound(arr) + 1 Step -1
.Cells(Val(arr(i - 1))).EntireColumn.Delete
Next
End With
Application.ScreenUpdating = True
End Sub
(slow for a large number of columns)

Can't copy formula? [duplicate]

This question already has answers here:
How do I put double quotes in a string in vba?
(5 answers)
Closed 5 years ago.
I can't operate the code without it stating the sub or function is not defined in the formula I'm trying to paste down column v.
Sub SEALANTSCHEDULIZER()
' 1. Unhide All Columns
Columns.EntireColumn.Hidden = False
' 2. Clear All color highlights
ActiveSheet.UsedRange.Interior.ColorIndex = 0
' 3. Drag formulas in all columns down to last row (if necessary) – use column A (ID) as row count
LastRowColumnA = cell(Rows.count, 1).End(xlUp).Row
Range("V2:V" & LastRowColumnA).Formula = "=IF(OR(W2="Yes", AE2="Yes"), "Yes", "No")"
End Sub
You need to double the " around the text strings.
"=IF(OR(W2=""Yes"", AE2=""Yes""), ""Yes"", ""No"")"

Excel VBA Loop through alphabet [duplicate]

This question already has answers here:
Excel column number from column name
(7 answers)
Function to convert column number to letter?
(28 answers)
Closed 8 years ago.
I currently have a excel file with a couple of columns and many rows, each cell holds information. Currently my macro has a loop within a loop to go through each row and column. The loop for the row is no problem, i just keep increasing the number by a step of 1, but when it comes to the columns i have made my own sub routine. this subroutines changes the numbers in the loop to letter and stores that letter in a variable.
For firstloop = 3 To number
For column = 1 To 3
columnconvert
'some code in here
next
next
and this code is the new subroutine i made:
Sub columnconvert()
Select Case column
Case "1"
columnletter = "Q"
Case "2"
columnletter = "R"
Case "3"
columnletter = "S"
End Select
End Sub
i was wondering, is there an easier way to "CONVERT" the loop number to a letter? could i loop through with letters instead of numbers?