I trying to write a code where I want to look select range from Start to End in column A then fill the selected range in Column B with '1'. But my code only fill up bottom parts and missed out top parts. Refer to this photo. How do i make sure it go through every row and when it encounter Start, it will look for nearest End and fill up column B?
Sub Select()
Dim LastRowA As Long, i As Long
Dim findrow As Long, findrow2 As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRowA
findrow = Range("A:A").Find("Start", Range("A1")).Row
findrow2 = Range("A:A").Find("End", Range("A" & findrow)).Row
Range("A" & findrow & ":A" & findrow2).Offset(0, 1).Value = "1"
Next i
End With
End Sub
This will find Start, loop through until it finds End. Tested and working as requested.
Sub Select()
Dim LastRowA, i As Long
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRowA
If .Cells(i, 1) = "Start" Then
Do Until .Cells(i, 1) = "End"
.Cells(i, 2).Value = 1
i = i + 1
Loop
.Cells(i, 2).Value = 1
End If
Next i
End With
End Sub
Find is a precarious beast to use in VBA. It would be far simpler to just loop through the cells, keeping track of whether or not you're between Start and End:
Sub Select()
Dim LastRowA As Long, i As Long, b As Boolean
With ThisWorkbook.Worksheets("Sheet1")
LastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = 1 To LastRowA
If .Cells(i, 1).Value = "Start" Then
b = True
.Cells(i, 2).Value = 1
ElseIf .Cells(i, 1).Value = "End" Then
b = False
.Cells(i, 2).Value = 1
ElseIf b Then
.Cells(i, 2).Value = 1
End If
Next i
End With
End Sub
Related
I was able to get this much working but not able to get it to start from Range("B12:B500").
Sub findTwoEmptyCells()
Dim lastRow As Long, i As Long
Dim firstEmptyCell As Range
lastRow = Cells(Rows.Count, 2).End(xlUp).Row
For i = 1 To lastRow
If Cells(i, 1).Value = "" And Cells(i, 1).Offset(1, 0).Value = "" Then
Set firstEmptyCell = Cells(i, 1)
Exit For
End If
Next i
End If
firstEmptyCell.Value = "x"
End Sub
Thanks to #Dominique & #FaneDuru, i was able to get the result as per the below by combining both their ideas :
Sub findTwoEmptyCells()
Dim lastRow As Long, i As Long
Dim firstEmptyCell As Range
lastRow = Cells(Rows.Count, 12).End(xlUp).Row
For i = 12 To lastRow
If Cells(i, 2).Value = "" And Cells(i + 1, 1).Value = "" Then
Set firstEmptyCell = Cells(i, 1)
Exit For
End If
Next i
If firstEmptyCell Is Nothing Then
MsgBox ("There are no two empty cells in a row")
Exit Sub
End If
firstEmptyCell.Value = "x"
End Sub
please help i want to sort the name column such that each name starts after every blank cell.
I want it look something like this..pls help it's a pretty long column
Option Explicit
Sub SetNamePosition()
Dim arr As Variant
Dim i As Long: i = 1 ' for Loop
Dim j As Long: j = 1 ' for Array
Dim lastRow As Long: lastRow = Cells(Rows.Count, 1).End(xlUp).Row
Dim rngColB As Range: Set rngColB = Range("B2:B" & lastRow)
Dim rngNames As Range: Set rngNames = Range("C1") ' Temporary range
' Get column B names only
rngColB.SpecialCells(xlCellTypeConstants, 2).Copy
rngNames.PasteSpecial (xlPasteValues)
Application.CutCopyMode = False
Set rngNames = Range(rngNames, rngNames.End(xlDown))
' Load rngNames to array
arr = Application.Transpose(rngNames)
' Clear rng of column B and rngNames
rngColB.Clear
rngNames.Clear
' Insert names
For i = 2 To lastRow
' set name
Cells(i, 1).Offset(0, 1).Value = arr(j)
' find next cell
i = Cells(i, 1).End(xlDown).Row + 1
j = j + 1
Next i
End Sub
I's probably better to remove the empty ranges before making the array, but here's one way to distribute the names:
Loading the range ito an array, then go through the numbers and look for empty ranges.
This assumes that we are working with column "A" and "B" (1 and 2), starting at the top.
Sub test()
Dim arr As Variant
Dim lastRow As Long, i As Long, j As Long
lastRow = Cells(Rows.Count, "A").End(xlUp).Row
arr = Application.Transpose(Range("B2:B" & lastRow))
Range("B2:B" & lastRow).Clear
j = 1
For i = 2 To lastRow
Cells(i, 2) = arr(j)
j = j + 1
If j >= UBound(arr) Then Exit For
While arr(j) = "" And j < UBound(arr)
j = j + 1
Wend
While Not Cells(i, 1).Value = ""
i = i + 1
Wend
Next i
End Sub
Any leftover names will be removed
I am trying to change the cells under B column to blue if the sum for the corresponding row is less than 55000. Below is my code I have figured out to achieve that for one row. How could I modify it so that it works for the other rows if I have a lot of rows?
Dim rng As Range
Dim result As Long
Set rng = Sheets(2).Range("C2:N2")
result = Application.WorksheetFunction.Sum(rng)
If result < 550000 Then
Sheet2.Range("B2").Font.Color = vbBlue
Sheet2.Range("B2").Font.Bold = True
End If
With a loop:
With Sheet2
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
Dim i As Long
For i = 2 To lastRow
If Application.Sum(.Range("C" & i & ":N" & i)) < 550000 Then
.Cells(i, "B").Font.Color = vbBlue
.Cells(i, "B").Font.Bold = True
End If
Next
End With
EDIT:
If you want to do the same thing, but for columns instead of rows:
With Sheet2
Dim lastColumn As Long
lastColumn = .Cells(1, .Columns.Count).End(xlToRight).Column
For i = 3 To lastColumn
If Application.Sum(.Columns(i)) < 550000 Then
.Cells(1, i).Font.Color = vbBlue
.Cells(1, i).Font.Bold = True
End If
Next
End With
This code is running perfectly fine on inserting blank rows based on a cell value, but now I need to also fill out those new rows in range("E") saying "False". Not sure how to make this work.
Sub Procedure1()
Dim i As Integer
Dim LastRow As Long
LastRow = Sheets("Sheet1").Cells(Rows.Count, "F").End(xlUp).Row
For i = LastRow To 2 Step -1
a = Sheets("Sheet1").Cells(i, 8).Value
For j = 1 To a
Sheets("Sheet1").Rows(i + 1).Select
Selection.Insert Shift:=xlDown
Next
Next
Sheets("Sheet1").Cells(i, 1).Select
End Sub
Any thoughts?
You can avoid the inner loop using Resize.
And you can generally avoid Select.
Sub Procedure1()
Dim i As Long, j As Long
Dim LastRow As Long
Dim a
With Sheets("Sheet1")
LastRow = .Cells(Rows.Count, "F").End(xlUp).Row
For i = LastRow To 2 Step -1
a = .Cells(i, 8).Value 'best to check this is a number before going further
.Rows(i + 1).Resize(a).Insert Shift:=xlDown
.Range("E" & i + 1).Resize(a).Value = "False"
Next
End With
End Sub
I need some help for my code. I want to copy client's name on column C based on these 2 conditions if:
Macro find value = "ongoing" on Column G
Macro find value = "Istry" on column D
In other words if macro find "ongoing" and "istry" at same row, it will copy automatically the client's name associated with these 2 values asked on another sheet.
I wrote a code but when I tried to run it, I didn't get any result on my sheet.
Sub Ss()
Dim finalrow As Long, i As Long, rowpt As Long, colpt As Long
finalrow = ShSReturn.Range("D" & "G" & Rows.Count).End(xlUp).Row
rowpt = ShPPT.Cells(Rows.Count, 1).End(xlUp).Row
colpt = ShPPT.Cells(Rows.Count, 1).End(xlUp).Row
Call Entry_Point
For i = 7 To finalrow
If ShSReturn.Cells(i, 4).Value = "Istry" & ShSReturn.Cells(i, 7).Value = "Ongoing" Then
ShSReturn.Cells(i, 3).Copy
ShPPT.Cells(rowpt + 6, 12).PasteSpecial xlPasteValues
rowpt = rowpt + 1
colpt = colpt + 1
End If
Next i
End Sub
Making some assumptions here about your intent for this code here is a quick rewrite:
Sub Ss()
Dim finalrow As Long, i As Long, rowpt As Long, colpt As Long
'Determine how many rows we need to loop:
finalDRow = ShSReturn.Range("D" & Rows.Count).End(xlUp).Row
finalGRow = ShSReturn.RAnge("G" & Rows.Count).End(xlUp).Row
'Loop only through rows were both G and D have records
If finalDRow < finalGRow Then finalrow = finalDRow Else finalRow = finalGRow
'I don't know what these two are doing, but they will return the same exact number (the last row populated in column A of whatever worksheet object is in ShPPT
rowpt = ShPPT.Cells(Rows.Count, 1).End(xlUp).Row
colpt = ShPPT.Cells(Rows.Count, 1).End(xlUp).Row
Call Entry_Point
'Loop through rows 7 to whatever finalRow shakes out to be above
For i = 7 To finalrow
'If column D is "Istry" AND column G is "Ongoing" Then execute this code.
If ShSReturn.Cells(i, 4).Value = "Istry" AND ShSReturn.Cells(i, 7).Value = "Ongoing" Then
ShSReturn.Cells(i, 3).Copy
ShPPT.Cells(rowpt + 6, 12).PasteSpecial xlPasteValues
rowpt = rowpt + 1
colpt = colpt + 1
End If
Next i
End Sub
You can use a Filter.
Be sure to set the appropriate worksheet references.
As written, the code copies the entire row, but you can easily modify it if you only want a few fields to be copied over.
Option Explicit
Option Compare Text
Sub filterName()
Const strG = "ongoing"
Const strD = "lstry"
Dim rCopyTo As Range
Dim rData As Range
Dim lastRow As Long, LastCol As Long
With Worksheets("Sheet6")
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set rData = .Range(.Cells(1, 1), .Cells(lastRow, LastCol))
End With
Set rCopyTo = Worksheets("sheet7").Cells(1, 1)
Application.ScreenUpdating = False
rData.AutoFilter field:=4, Criteria1:=strD, visibledropdown:=False
rData.AutoFilter field:=7, Criteria1:=strG, visibledropdown:=False
rCopyTo.Cells.Clear
rData.SpecialCells(xlCellTypeVisible).Copy rCopyTo
rData.Worksheet.AutoFilterMode = False
Application.ScreenUpdating = True
End Sub