VBA: statement in if Then loop fails - excel

I have a sheet with Columns A to P.
In columns B i have customer names. Want to find rows with substring “ABC -“ and copy the content of the cell in column B to Column G on the same row.
My code fails on this:
For I= 1 to finalrow
If Left(Cells(I,2).Value,5) = “ABC -“ Then
Rownumber= ActiveCell.Row
Range("B" & Rownumber).Select
Range("B" & Rownumber).Copy
Range("G" & rownumber).Select
ActiveSheet.Paste
Range("G" & rownumber).Select
End if
Next I

This one works as expected, writing the values from column "B" to column "G":
Sub TestMe()
Dim i As Long
For i = 1 To 10
With ThisWorkbook.Worksheets("Sheet1")
Dim myCell As Range
Set myCell = .Cells(i, "B")
If Trim(Left(myCell.Value, 5)) = "ABC -" Then
.Cells(i, "G").Value = myCell.Value
End If
End With
Next i
End Sub
Try to avoid .Select and .Activate - https://stackoverflow.com/a/35864330/5448626
Use Trim()
Using . and referring the worksheet is always a good practice
.Cells(i, "B") improves readability
“ probably should be "

For I = 1 To finalrow
With Cells(I, 2)
If .Text Like "ABC -*" Then .Offset(0, 5) = .Value
End With
Next I

For I = 1 to finalrow
If Left(Cells(I,2).Value,5) = "ABC -" Then
Cells(I,7).Value = Cells(I,2).Value
End if
Next I

Related

Selection of Continued filled Cells and Calculation of MAX,MIN,AVG

Hope You are all Safe
I'm trying to calculate MAX, MIN and AVG Values of filled cells which are continued without blank cell (As you can see it in the left side of the sample image ).
I'm facing problem in selecting these randomly placed cells and calculate the above values and also "From" and "To" values of respective range.
Please let me know how to do it. So far I've constructed following code
Dim Cel As Range
Dim lastrow As Long
Dim destSht As Worksheet
Set destSht = Worksheets("Final")
With Worksheets("Source")
lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
For Each Cel In .Range("C2:C" & lastrow)
If .Cells(Cel.Row, "C") <> "" Then
Cel.Offset(0, -1).Copy Destination:=destSht.Cells(destSht.Rows.Count, 1).End(xlUp).Offset(0, 1)
'It will give "From" Column
'' Plz suggest for "To" Column
Range("G5").Select
ActiveCell.FormulaR1C1 = "=MAX(RC[-4]:R[4]C[-4])" 'It will give values "MAX" Column
Range("H5").Select
ActiveCell.FormulaR1C1 = "=MIN(RC[-5]:R[4]C[-5])" 'It will give values "MIN" Column
Range("I5").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(RC[-6]:R[4]C[-6])" 'It will give values "AVG" Column
End If
Next
Did some quick, which should work.
I don't know what you want to do in the "Final" worksheet, so haven't focused on that line.
Logic is to have one big loop (For i...) that go through the whole Column C. When a value is found in column C (If .Cells(i, "C") <> "" Then), we perform a "small loop" (For j = i To lastrow + 1) to check next empty cell to decide the "small group" range. When that range is decided we perform the To, From, MAX, MIN and AVG formulas, which has to be dynamic.
Option Explicit
Sub trial()
Dim lastrow As Long
Dim destSht As Worksheet
Dim i As Long, j As Long
Set destSht = Worksheets("Final")
With Worksheets("Source")
lastrow = .Range("B" & .Rows.Count).End(xlUp).Row
For i = 2 To lastrow + 1 'loop whole range (column C)
If .Cells(i, "C") <> "" Then 'If column C is not empty then
For j = i To lastrow + 1 'Loop "group" range to find next empty cell. Start from current loop i to last row and add one row to get to next empty cell.
If .Cells(j, "C") = "" Then 'When next empty cell is found (i.e. end of small group range) then apply formulas
.Cells(i, "E").Value = .Cells(i, "B").Value 'From
.Cells(i, "F").Value = .Cells(j - 1, "B").Value 'To
.Cells(i, "G").Formula = "=MAX(C" & i & ":C" & j - 1 & ")" 'MAX
.Cells(i, "H").Formula = "=MIN(C" & i & ":C" & j - 1 & ")" 'MIN
.Cells(i, "I").Formula = "=AVERAGE(C" & i & ":C" & j - 1 & ")" 'AVG
Exit For
End If
Next j
End If
Next i
End With
End Sub
Result:

Repeat column contents by "n" rows based on column value

I would like to repeat ID number based on the "number" number. For example:
to
I have tried the following so far..
Sub MySub()
Do While B2 = n
CurrentSheet.Range("a1:c1").EntireRow.Resize(n).Insert
Loop
End Sub
It probably doesn't make much sense, as I am fairly new!
If you wanted to list the data in column D, you could use this
Sub x()
Dim r As Range
For Each r In Range("A2", Range("A" & Rows.Count).End(xlUp)) 'loop through A
Range("D" & Rows.Count).End(xlUp)(2).Resize(r.Offset(, 1).Value).Value = r.Value 'duplicate number of times in B
Next r
End Sub
If you want to insert into your existing data
Sub x()
Dim r As Long
For r = Range("A" & Rows.Count).End(xlUp).Row To 2 Step -1
If Cells(r, 2) > 1 Then
Cells(r + 1, 1).EntireRow.Resize(Cells(r, 2).Value - 1).Insert shift:=xlDown
Cells(r + 1, 1).Resize(Cells(r, 2).Value - 1) = Cells(r, 1).Value
End If
Next r
End Sub

Create a checkpoint in a foreach statement

I am writing a code that put an X in a cell depending on a offset cell value, for exemple if the offset cell has a value of 3, it will put an X in the cell and decrement the offset cell value, i want to save the location of that cell and start the next for each with it.
For Each Cell In plage
If (Cell.Offset(0, 1).Value <> 0) Then
If (Cell.Value <> "X") Then
Cell.Offset(0, 1).Value = Cell.Offset(0, 1).Value - 1
Cell.Value = "X"
Checkpoint = Cell.Address
Exit For
Else
Cell.Value = ""
GoTo NextStep
End If
Exit For
Else
Cell.Value = ""
End If
NextStep:
Next Cell
The problem i am having with the current code is it start the loop all over again while i want it to keep till the end of the lines, until all offset value are equal to 0.
Try the below (there are notes on the code). If you face difficulties let me know.
Option Explicit
Sub test()
'In this example we assume that the data you want to loop appear in Column A
Dim i As Long, Lastrow As Long
Dim Checkpoint As Variant
With ThisWorkbook.Worksheets("Sheet1") '<- Change sheet name if needed
Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row '< -Fins the lastrow of the column you want to loop
For i = 2 To Lastrow ' < -Start looping from row 2 to Lastrow fo the column
If .Range("A" & i).Offset(0, 1).Value <> 0 Then '<- You are looping
If .Range("A" & i).Value <> "X" Then
.Range("A" & i).Offset(0, 1).Value = .Range("A" & i).Offset(0, 1).Value - 1
.Range("A" & i).Value = .Range("A" & i).Value & "X"
Checkpoint = .Range("A" & i).Address
Else
.Range("A" & i).Value = ""
End If
Else
.Range("A" & i).Value = ""
End If
Next i
End With
End Sub
Is plage a range?
If so, you could update it to start from the checkpoint and include all cells up to some lastCell for example.
Something like:
set plage=thisWorkbook.Worksheets("Your Worksheet").Range(checkpoint,lastCell)
That way the next For-Each should start from your checkpoint.
BTW if I understand correctly what you'e trying to do, I would suggest you replace cell.value="" with cell.clearContents

Copy data from multiple columns into a single column

I have 3 columns A, B, C and I want to make a column D with values in A, B, C but it should include ">=", "<=" signs as well. The script I am working on does help me loop around columns and copy its data to a new column. Can anyone help me figure out how I can add those special characters at the beginning of the numbers in the cells?
Thanks for any help.
Sub Try()
With ActiveWorkbook.Sheets("Sheet1")
For rw = 1 To .Rows.Count
If (.Rows(rw).Columns("A:A").Value <> "") Then
.Rows(rw).Columns("A:A").Copy .Range("D" & rw)
End If
Next rw
.Columns("A:A").Delete
End With
End Sub
With data in cols A through C, in D1 enter:
=IF(A1<>"",">="&A1,IF(B1<>"","<="&B1,C1))
and copy down:
EDIT#1:
To do this with VBA:
Sub PopulateFormulas()
Dim N As Long, s As String
s = "=IF(A1<>"""","">=""&A1,IF(B1<>"""",""<=""&B1,C1))"
N = Range("A1").CurrentRegion.Rows.Count
Range("D1:D" & N).Formula = s
End Sub
Probably not the most elegant solution (and you probably don't even need VBA for this, a formula would most likely suffice), but this does the trick:
Sub Test()
arr = Array(">=", "<=", "")
With ActiveWorkbook.Sheets("Sheet1")
For cl = 1 To 3
For rw = 2 To .Cells(ActiveSheet.Rows.Count, "C").End(xlUp).Row
If .Cells(rw, cl).Value <> "" Then
.Cells(rw, 4).Value = arr(cl - 1) & .Cells(rw, cl).Value
End If
Next rw
Next cl
End With
'If you still need to delete those columns at the end-
'ActiveWorkbook.Sheets("Sheet1").Columns("A:C").Delete xlShiftLeft
End Sub
This worked for me:
Sub macro_test()
k = 1
For k = 1 To 3
t = 2
lr = ActiveSheet.Cells(100000, k).End(xlUp).Row
Do Until t > lr
If Cells(t, k).Value = “” Then GoTo continue
If k = 1 Then Cells(t, 4).Value = ">=" & Cells(t, k).Value
If k = 2 Then Cells(t, 4).Value = "<=" & Cells(t, k).Value
If k = 3 Then Cells(t, 4).Value = "" & Cells(t, k).Value
continue:
t = t + 1
Loop
Next
End Sub
try this
Sub main()
Dim iCol As Long, cell As Range, signs As Variant
signs = Array(">=", "<=", "")
For iCol = 1 To 3
For Each cell In Columns(iCol).SpecialCells(xlCellTypeConstants, xlNumbers)
cell.Value = signs(iCol - 1) & cell.Value
Next
Next
End Sub
if your columns A, B and C not empty cells content is not a numeric one only, then you could use:
For Each cell In Columns(iCol).SpecialCells(xlCellTypeConstants)
while if it's some formula, then you could use:
For Each cell In Columns(iCol).SpecialCells(xlCellTypeFormulas)

VBA - looking through each record

Struggling a bit with this code, I haven't ever had to reference one column and copy and paste to another tab in VBA so here goes..
I have an excel document with a table on it similar to below:
I need my code to look in column A find the first name, in this case, Nicola. I then want it to look at column B and check to see if she has the word "Internet" appear in any of the records stored against her, as she does the code will ignore her and move down to the next name on the list, in this case, Graham. It will then look to column B and check if he has the word "Internet". As he doesn't, the code needs to copy the Information from column A & B in relation to this persons name and paste the information into another sheet in the workbook.
Sub Test3()
Dim x As String
Dim found As Boolean
Range("B2").Select
x = "Internet"
found = False
Do Until IsEmpty(ActiveCell)
If ActiveCell.Value = x Then
found = True
Exit Do
End If
ActiveCell.Offset(1, 0).Select
Loop
If found = False Then
Sheets("Groupings").Activate
Sheets("Groupings").Range("A:B").Select
Selection.Copy
Sheets("Sheet1").Select
Sheets("Sheet1").Range("A:B").PasteSpecial
End If
End Sub
Any help would be greatly appreciated.
Thanks
Paula
Private Sub Test3()
Application.ScreenUpdating = False
Set sh1 = Sheets("Groupings") 'data sheet
Set sh2 = Sheets("Sheet1") 'paste sheet
myVar = sh1.Range("D1")
Lastrow = sh1.Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To Lastrow '2 being the first row to test
If Len(sh1.Range("A" & i)) > 0 Then
Set myFind = Nothing
If WorksheetFunction.CountA(sh1.Range("A" & i, "A" & Lastrow)) > 1 Then
If Len(sh1.Range("A" & i + 1)) = 0 Then
nextrow = sh1.Range("A" & i).End(xlDown).Row - 1
Else
nextrow = nextrow + 1
End If
Set myFind = sh1.Range("B" & i, "B" & nextrow).Find(What:=myVar, LookIn:=xlFormulas, LookAt:=xlWhole)
Else
nextrow = Lastrow
Set myFind = sh1.Range("B" & i, "B" & nextrow).Find(What:=myVar, LookIn:=xlFormulas, LookAt:=xlWhole)
End If
If myFind Is Nothing Then
sh1.Range("A" & i, "B" & nextrow).Copy
sh2.Range("A" & sh2.Range("B" & Rows.Count).End(xlUp).Row + 1).PasteSpecial xlPasteValues
Application.CutCopyMode = False
End If
End If
Next
End Sub
I don't clearly see the structure of your data, but assuming the original data is in Worksheet Data, I think the following is going to do what you want (edited to search for two conditions).
Private Sub Test3()
Dim lLastRow as Long
Dim a as Integer
Dim i as Integer
Dim sText1 As String
Dim sText2 As String
sText1 = Worksheets("Data").Cells(1, 5).Value 'search text #1, typed in E1
sText2 = Worksheets("Data").Cells(2, 5).Value 'search text #2, typed in E2
lLastRow = Cells(Rows.Count, 1).End(xlUp).Row
a = 1
For i = 2 To lLastRow
If (Worksheets("Data").Cells(i, 1).Value <> "") Then
If (Worksheets("Data").Cells(i, 2).Value <> sText1 And Worksheets("Data").Cells(i + 1, 2).Value <> sText1 And Worksheets("Data").Cells(i, 2).Value <> sText2 And Worksheets("Data").Cells(i + 1, 2).Value <> sText2) Then
Worksheets("Groupings").Cells(a, 1).Value = Worksheets("Data").Cells(i, 1).Value
Worksheets("Groupings").Cells(a, 2).Value = Worksheets("Data").Cells(i, 2).Value
Worksheets("Groupings").Cells(a, 3).Value = Worksheets("Data").Cells(i + 1, 2).Value
a = a + 1
End If
End If
Next
End Sub

Resources