Replace values in rows after certain GAP of row numbers - excel

I need to replace the value in a certain row that is repeated after a certain amount of rows. I have multiple Excels I need to do this.
Current data:
7th row: IN
16th row: IN ( comes after 9 rows)
25th row: IN ( comes after 9 rows)
I need these values to be replaced by OUT.
I did some research and seems like I could use macros but I am not familiar with macros.
Can anyone please help with macros with a loop or suggest any other ideas?

A simple excel formula can work per sheet, and of course a macro can work
The excel solution, starting in B2
=IF(AND($A1 = "IN",$A1 = $A2),"Out",$A2)
This formula will replicate your original column, with the fix. then a simple copy & paste as values of column B to A should work
A VBA solution, for which you need to select the relevant column:
Sub fixOut()
Dim cell As Object
For Each cell In Selection
If cell = "IN" AND cell = cell.OffSet(-1, 0) Then cell = "Out"
Next cell
End Sub

For Excel Office 365, create a macro going under View -> Macro -> View Macros -> You input a macro name and then press Create button.
A text editor screen should appear, the macro should be the following:
Sub test_macro()
Dim searching_string As String
searching_string = "IN"
replacing_string = "OUT"
searching_column = "A"
minimum_distance_to_be_modified = 3
previous_found_row = -1
row_number = 10000
For i = 1 To row_number
If Range(searching_column + CStr(i)).Value = searching_string Then
If i - previous_found_row <= minimum_distance_to_be_modified And previous_found_row <> -1 Then
Range(searching_column + CStr(i)).Value = replacing_string
End If
previous_found_row = i
End If
Next
End Sub
Set your searching_string, searching_column, minimum_distance_to_be_modified, replacing_string and you should be fine!
I did a test with the settings that you find in the snippet and this was the result:
Hope that this is going to help you.

I solved my problem via macros and VB code.
Sub Macro1()
'
' Macro1 Macro
'
Dim i As Integer
i = 7
Do While i <= Cells(Rows.Count, 1).End(xlUp).Row
Cells(i, 1).Value = "Out"
i = i + 9
Loop
End Sub
Add this code to new macros and run the macros if anyone is having a similar problem to mine.

You could try:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long
Dim arr As Variant
'Change target worksheet if needed
With ThisWorkbook.Worksheets("Sheet1")
'Find the last row of column A
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
'Set in arr the range with the data starting from row 1 to last row
arr = .Range("A1:A" & LastRow)
For i = LBound(arr) To UBound(arr) - 1
'Check if the current and the next Arr(i) are both "IN"
If arr(i, 1) = "IN" And arr(i + 1, 1) = "IN" Then
'Set the next arr(i) to OUT
arr(i + 1, 1) = "OUT"
End If
Next i
'Print the values
.Range("A1:A" & UBound(arr)).Value = arr
End With
End Sub

Related

Transferring a long list of data to specific sheets based on selection in first cell of each row

I'm trying to automate a list of data to be transferred to specific sheets by adding new rows, all within the same workbook.
Ideally I wouldn't like to limit the number of rows of data to be transferred as there could be a large amount of data coming in. I have tried the following code but it only works for a single row of data, can anyone help improve this code to transfer multiple rows?
Sub AddValues()
'Dimension variable and declare data type
Dim i As Single
'Save row number of cell below last nonempty cell
i = Worksheets("" & Range("A2")).Range("B" & Rows.Count).End(xlUp).Row + 1
'Save input values to selected worksheet
Worksheets("" & Range("A2")).Range("B" & i & ":P" & i) = _
Worksheets("Form").Range("B2:P10").Value
'Clear input cells
Worksheets("Form").Range("B2:P10") = ""
'Stop macro
End Sub
Sample output of what I hope it do:
Try this one:
EDITED. That one is the last version, fixin the shifting problems from the previous one.
I just add a few comments so in the future you can modify it easily.
Option Explicit
Sub AddValues()
'Dimension variable and declare data type
Dim i As Long 'Counter
Dim j As Long 'Counter
Dim NextRow As Long 'Last Row used
'Save input values to selected worksheet
For i = 2 To Worksheets("Form").UsedRange.SpecialCells(xlCellTypeLastCell).Row 'For Each Row on Sheet "Form"
'If there is no item still copied (Problem with merged cells)
If Worksheets(Range("A" & i).Value2).Cells(1048576, 2).End(xlUp).Row < 7 Then
For j = 2 To Worksheets("Form").UsedRange.SpecialCells(xlCellTypeLastCell).Column
Worksheets(Range("A" & i).Value2).Cells(7, j).Value2 = Worksheets("Form").Cells(i, j).Value2
Next j
'If there is just one item still copied (Problem with merged cells)
ElseIf Worksheets(Range("A" & i).Value2).Cells(1048576, 2).End(xlUp).Row = 7 Then
For j = 2 To Worksheets("Form").UsedRange.SpecialCells(xlCellTypeLastCell).Column
Worksheets(Range("A" & i).Value2).Cells(8, j).Value2 = Worksheets("Form").Cells(i, j).Value2
Next j
'For all remaining scenarios
Else
NextRow = Worksheets(Range("A" & i).Value2).Cells(1048576, 2).End(xlUp).Row
For j = 2 To Worksheets("Form").UsedRange.SpecialCells(xlCellTypeLastCell).Column
Worksheets(Range("A" & i).Value2).Cells(NextRow + 1, j).Value2 = Worksheets("Form").Cells(i, j).Value2
Next j
End If
Next i
'Clear input cells from B2 cell until the last Cell (not required to pay attention to the range)
Range(Cells(2, 2), Worksheets("Form").UsedRange.SpecialCells(xlCellTypeLastCell)) = ""
'End macro
End Sub
Hope it helps!

How to fix a sum formula that changes ranges

Sub formu
Range(“d” & Rows.count).end(xlUp).offset(2,0).formular1c1 = “=sum(R[-9]c:r[-1]c)”
End sub
Currently this is my formula but it changes on a weekly basis so I t could be R[-14]c:r[-1]c the next week or R[-6]c:r[-1]c the next week. How do I get my formula to change weekly so I don’t have to manually re sum the cell?
Say the following would be your current weeks data:
The following code would add a formula in the cell below the last cell:
Sub formu()
Dim lr As Long
With Formulas
lr = .Cells(.Rows.Count, 4).End(xlUp).Row
.Cells(lr + 1, 4).FormulaR1C1 = "=sum(R[-" & lr & "]c:r[-" & lr - (lr - 1) & "]c)"
End With
End Sub
Whereas the sheet reference is the sheet codename containing the data. Output:
The formula in that cell is currently:
=SUM(R[-10]C:R[-1]C)
You can find a sheet's CodeName in the project explorer and give it a meaningfull name to reference directly :)
First of all, is important to know if your weekly report always put the information in te same order. For example all the numbers start in the Range("A2"). If that the case i recomend you to work whit TagNames. Asuming that the data you need to sum start in the Range("A2")
Dim Col as integer
Dim sRow, eRow as long 's = start, e = end
Col = Range("A2").Column 'Col = 1
sRow = Range("A2").Row 'sRow = 2
eRow = Range("A2").Rnd(xlDown).Row 'eRow = row of the last cell with information to sum
Range(Cells(sRow,Col),Cells(eRow,Col)).Name = "range2Sum" 'Yo assign a tagname
'The you can use what you have done
Range(“d” & Rows.count).end(xlUp).offset(2,0).formula = “=sum(range2Sum)” 'use that tagname that is visible in excel

How to use Rows.Count function if there are blank cells in between data

I am trying to write a code that adds in data from my excel sheet if the item the user selects is equal to the range in J. This works perfectly if the range in J is filled in with all the data, but how do I get the row to still count all the way through the last filled cell if there are blanks in between? I attached a picture to show what I mean.
.
I would want to count the rows all the way down to the last "Gold". Right now it only counts to the second.
Private Sub cboName_Click() 'only get values that are assigned
Dim j As Integer, k As Integer, i As Integer
Me.lstProvider.Clear
i = 0
Worksheets("Biopsy Log").Select
For j = 1 To Range("J2", Range("J1").End(xlDown)).Rows.count
If Range("J2", Range("J2").End(xlDown)).Cells(j) = Me.cboName.Value Then
If Range("C2", Range("C2").End(xlDown)).Cells(j) = "Assigned" Then
With Me.lstProvider
.AddItem
For k = 0 To 5
.List(i, k) = Range("A" & j + 1).Offset(0, k)
Next
End With
i = i + 1
End If
End If
Next
End Sub
Instead of For j = 1 To Range("J2", Range("J1").End(xlDown)).Rows.count use Range("J" & Rows.Count).End(xlUp).Row (assuming GOLD is in column J). The code does the opposite of xlDown. It goes down to the last row of the sheet (Rows.count) and moves up until it find the first non-blank cell.
Instead of using xlDown, try to use xlUp from the bottom to get the last row for correct range:
Dim sht As Worksheet
Set sht = Worksheets("Biopsy Log")
For j = 1 To sht.Range("J" & sht.Rows.Count).End(xlUp).Row
If sht.Range(...)
Qualifying Range calls with an explicit Worksheet object makes your code more robust.

Excel Loop Column A action column B

I'm currently looking for a code to improve my Dashboard. Actually, I need to know how to use a loop in a column X who will affect a column Y (cell on the same line).
To give you an example:
Column A: I have all Production Order (no empty cell)
Column B: Cost of goods Sold (Sometimes blank but doesn't matter)
I actually pull information from SAP so my Column B is not in "Currency".
The action should be:
If A+i is not empty, then value of B+i becomes "Currency".
It's also for me to get a "generic" code that I could use with other things.
This is my current code...
Sub LoopTest()
' Select cell A2, *first line of data*.
Range("A2").Select
' Set Do loop to stop when an empty cell is reached.
Do Until IsEmpty(ActiveCell)
ActiveCell.Offset(0, 1).Style = "Currency"
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Another example, getting Last Row, in case your data contains any blank rows.
Sub UpdateColumns()
Dim wks As Worksheet
Dim lastRow As Long
Dim r As Long
Set wks = ActiveSheet
lastRow = ActiveSheet.Cells.SpecialCells(xlLastCell).Row
For r = 2 To lastRow
If wks.Cells(r, 1) <> "" Then
wks.Cells(r, 2).NumberFormat = "$#,##0.00"
End If
Next r
End Sub
I can see I was a little slower than the others, but if you want some more inspiration, heer is a super simple solution (as in easy to understand as well)
Sub FormatAsCurrency()
'Dim and set row counter
Dim r As Long
r = 1
'Loop all rows, until "A" is blank
Do While (Cells(r, "A").Value <> "")
'Format as currency, if not blank'
If (Cells(r, "B").Value <> "") Then
Cells(r, "B").Style = "Currency"
End If
'Increment row
r = r + 1
Loop
End Sub
Try the following:
Sub calcColumnB()
Dim strLength As Integer
Dim i As Long
For i = 1 To Rows.Count
columnAContents = Cells(i, 1).Value
strLength = Len(columnAContents)
If strLength > 0 Then
Cells(i, 2).NumberFormat = "$#,##0.00"
End If
Next i
End Sub
Explanation--
What the above code does is for each cell in Column B, so long as content in column A is not empty, it sets the format to a currency with 2 decimal places
EDIT:
Did not need to loop
Here's a really simply one, that I tried to comment - but the formatting got messed up. It simply reads column 1 (A) for content. If column 1 (A) is not empty it updates column 2 (B) as a currency. Changing active cells makes VBA more complicated than it needs to be (in my opinion)
Sub LoopTest()
Dim row As Integer
row = 1
While Not IsEmpty(Cells(row, 1))
Cells(row, 2).Style = "Currency"
row = row + 1
Wend
End Sub

Hide rows according to range of cells

Good day, I would love to ask you a question.
I have two colls with numbers and I need to compare first coll (longer) with second coll (shorter) and if there is a match, hide the row where the match occurs.
I have this so far:
Sub RowHide()
Dim cell As Range
Dim CompareCells As Range
Set CompareCells = Range("I2:I18")
For Each cell In Range("A2:A200")
If cell.Value = CompareCells Then
cell.EntireRow.Hidden = True
End If
Next
End Sub
My problem is that I don't know how to set value of CompareCells to start comparing. I'll appreciate every advice.
You have to set 2 separate ranges and compare them. If you want every cell compared with the one on the same line (A1 with B1, A2 with B2, etc) then consider using:
for i = 1 to something
set cell1 = range("A" & i)
set cell2 = range("B" & i)
if cell1.value = cell2.value then
'Do this, and do that!
cell1.entirerow.hidden = true
end if
next i
try this:
Sub RowHide()
Dim Longer As Range
Dim i As Double
i = 2 'Initial row
For Each Longer In Range("A2:A200")
If Longer.Value = Cells(i,2).Value Then
Longer.EntireRow.Hidden = True
End If
i = i + 1
Next
End Sub
PS:
Cells(RowIndex, ColumnIndex).Value: returns the value of the Row And Column.
ColumnIndex => Column A = 1, Column B = 2, an so on...
I looked into both of yours ideas and converted them into one and I finally get it working.
Here is my final code:
Sub RowHide()
Dim i As Integer
Dim j As Integer
For i = 2 To 197
Set FirstRange = Range("A" & i)
For j = 2 To 18
If FirstRange.Value = Cells(j, 8).Value Then
FirstRange.EntireRow.Hidden = True
End If
Next j
Next i
End Sub
Only modification if someone wants to use it is that you have to change numbers in for cycles according to number of rows in columns.
Thanks to both of you for your advices.

Resources