Filling out inserted cell with text - excel

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

Related

Excel Rows and columns having the same value should form as a table

Input:
Output:
Sub Macro3()
'
' Macro3 Macro
'
' Keyboard Shortcut: Ctrl+t
'
j = 1
a = Worksheets("Allocation").Cells(Rows.Count, j).End(xlUp).row
For i = 2 To a
If Worksheets("Allocation").Cells(i, 2).Value > 0 Then
Worksheets("Allocation").Rows(i).Copy
Worksheets("Output").Activate
b = Worksheets("Output").Cells(Rows.Count, 1).End(xlUp).row
Worksheets("Output").Cells(b + 1, 1).Select
ActiveSheet.Paste
Worksheets("Allocation").Activate
j = j + 1
End If
Next
Application.CutCopyMode = False
ThisWorkbook.Worksheets("Allocation").Cells(1, 1).Select
End Sub
To go through your table like that, you'd need to work in two for loops, 1 for the rows and 1 for the columns.
Sub getAllOnes()
Dim lColumn As Long, lastrow As Long, lRowO As Long
Dim wsA As Worksheet, wsO As Worksheet
Dim sCopy As String
Set wsA = ActiveWorkbook.Worksheets("Allocation")
Set wsO = ActiveWorkbook.Worksheets("Output")
lRowO = 2 'start row of your Output
lastrow = wsA.Range("A" & Rows.Count).End(xlUp).Row
lColumn = wsA.Cells(1, Columns.Count).End(xlToLeft).Column
For j = 2 To lColumn 'looping through columns
For i = 2 To lastrow 'looping through your rows
If Cells(i, j).Value2 > 0 Then 'assuming you only have 1's in the table, this should suffice
wsO.Cells(lRowO, 1).Value2 = wsA.Cells(i, 1).Value2 'using values makes sure you get the value but don't need to keep switching between sheets or copying
wsO.Cells(lRowO, 2).Value2 = wsA.Cells(1, j).Value2
lRowO = lRowO + 1
End If
Next i
Next j
End Sub
This outta do it. If anything is unclear, let me know.

Selecting range based on specific text then fill with text

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

Multiple If Functions

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

Insert row xlUP not working

I found the code below over here: Insert row below based on cell value excel macro
It works but, like the poster in the other message, I want the new row to be inserted below the existing row (here the row with a "2" in it), rather than above. I've tried changing Shift:=xlDown to xlUp but that has no effect. What am I missing something?
Sub BlankLine()
Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Col = "C"
StartRow = 1
BlankRows = 1
With ActiveSheet
For R = LastUsedRow() To StartRow + 1 Step -1
If .Cells(R, Col) = "2" Then
.Cells(R, Col).EntireRow.Insert Shift:=xlDown
End If
Next R
End With
End Sub
To insert the row below R use R + 1. Is this what you are trying?
Dim R As Long, LastRow As Long
LastRow = LastUsedRow()
With ActiveSheet
For R = LastRow To 2 Step -1
If .Range("C" & R).Value = 2 Then _
.Range("C" & R + 1).EntireRow.Insert Shift:=xlDown
Next R
End With

I need to insert rows according to the condition

I need to insert rows according to the condition that the cell in DQ column is non-blank then I have to insert a new row, and also paste the row data in the new row data.
The problem is that I am not able to insert a row above the matched column and also I don't know how to copy the text.
Following is the code that I have:
Sub Macro()
nr = Cells(Rows.Count, 5).End(xlDown).Row
For r = 4 To nr Step 1
If Not IsEmpty(Cells(r, 121).Value) Then
Rows(r + 1).Insert Shift:=xlDown
Rows(r + 1).Interior.ColorIndex = 16
End If
Next
End Sub
For this you will have to use a reverse loop. I quickly wrote this code and it is not tested. Let me know if you get any error.
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long, r As Long
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'~~> Get the last row which has data in Col DQ
lRow = .Cells(.Rows.Count, 121).End(xlDown).Row
'~~> Reverse Loop
For r = lRow To 4 Step -1
If Not IsEmpty(.Cells(r, 121).Value) Then
.Rows(r + 1).Insert Shift:=xlDown
.Rows(r + 1).Interior.ColorIndex = 16
End If
Next
End With
End Sub
I actually found the answer in this forum itself. Pasting the code and the link. Thanks a lot people.
Insert copied row based on cell value
Sub BlankLine()
Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Col = "DQ"
StartRow = 3
BlankRows = 1
LastRow = Cells(Rows.Count, Col).End(xlUp).Row
Application.ScreenUpdating = False
With ActiveSheet
For R = LastRow To StartRow + 1 Step -1
If .Cells(R, Col) <> "" Then
.Cells(R, Col).EntireRow.Copy
.Cells(R, Col).EntireRow.Insert Shift:=xlDown
.Cells(R, Col).EntireRow.Interior.ColorIndex = 4
End If
Next R
End With
Application.ScreenUpdating = True
End Sub

Resources