Conditional If statement to set cells equal to 0 - excel

I have a spreadsheet that is over 6000 rows and 300 columns. I need to know how to write code in vba that will allow me to read cells in a column and if says "no" then it sets the 3 cells to the right of it equal to zero. There is no error when I debug it, but the error is in the cell.Offset line. Thoughts?
Thank you in advance
Sub Macro1()
Dim rng As Range
Dim cell As Object
With Sheets("Sheet1")
Set rng = .Range("C1:C6000")
For Each cell In rng
If cell.Value = "no" Then
cell.Offset(0, 1).Value = 0
Exit For
End If
Next
End With
End Sub

Borrowing chuff's code:
Sub SetTo0IfNo()
Dim rng As Range
Dim lastRow As Long
Dim cell As Range
With Sheets("Sheet1")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set rng = .Range("A1:A" & lastRow)
For Each cell In rng
If cell.Value = "no" Then
'cell.Offset(0, 3).Value = 0
cell.Range("B1:D1").Value = 0
End If
Next
End With
End Sub

The following code should do the job. Using a For/Next loop, it reads each of the cells in Sheet 1 from A1 to the last cell in column A that has data. If the current cell has a value of "no", then it sets the value of the cell three columns to the right to the value 0.
Sub SetTo0IfNo()
Dim rng As Range
Dim lastRow As Long
Dim cell As Variant
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
With Sheets("Sheet1")
lastRow = .Range("A" & .Rows.Count).End(xlUp).Row
Set rng = .Range("A1:A" & lastRow)
For Each cell In rng
If cell.Value = "no" Then
cell.Offset(0, 3).Value = 0
End If
Next
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
To set a range of cells to the right of the column A cells to 0, you would use slightly different syntax that still relies on the offset function. For example, to set the three cells immediately to right to 0, replace the above code line cell.Offset(0,3).Value = 0 with the following code.
Range(cell.Offset(0, 1), cell.Offset(0, 3)).Value = 0
This approach is necessary because, unlike the worksheet OFFSET function which can return a reference to a range of cells, the VBA OFFSET can refer only to a single cell.

Related

Range of cell value based on another range value

I am trying to populate column C values on my sheet based on a value in column A. So if column A value is 1, then i want the value in corresponding cell in column C to be 10, else do nothing.
Sub test()
For Each cell In Sheets("Report").Range("A9:A" & LastRow)
If cell.Value = 1 Then
cell.Offset(0, 2).Value = 10
End If
Next cell
End Sub
Would appreciate any help and show me a better way if there is.
thanks
Try below sub.
Sub test()
Dim sh As Worksheet
Dim cel As Range
Dim LastRow As Long
Set sh = ThisWorkbook.Worksheets("Report")
LastRow = sh.Cells(sh.Rows.Count, 1).End(xlUp).Row
For Each cel In sh.Range("A9:A" & LastRow)
If cel.Value = 1 Then
cel.Offset(0, 2).Value = 10
End If
Next cel
Set sh = Nothing
End Sub

VBA to check if values from one sheet are found in another sheet

I want to develop a macro to check if values from one column in a sheet are found as substrings in the columns of another sheet.
So, I want to check each cell of my first column, and if it's not empty, compare it to every cell in the second sheet, of columns 1 and 3.
For this I used a "for each" loop, and then a "for" loop, with i from 1 to Rows.Count.
This is where it gets tricky as I'm not sure if it's the right way to approach this. Also, the parameters of the instr() function to check if values are found as substrings don't seem to match, as I get a "type mismatch" error when trying to run the code.
Sub test()
Set Wks1 = Worksheets("Sheet1")
Set Wks2 = Worksheets("Sheet2")
Dim i As Long
Dim rng As Range, cell As Range
Set rng = Wks1.Range("C3:O69")
For Each cell In rng
If Not (IsEmpty(cell.Value)) Then
For i = 1 To Wks2.Rows.Count
If (InStr(Cells(i, 1), cell.Value, 1) <> 0) Or (InStr(Cells(i, 3), _
cell.Value, 1) <> 0) Then
Cells(i, 4) = "String contains substring"
End If
Next i
End If
Next cell
End Sub
That should work
Sub test()
Dim i As Long
Dim rng As Range, cell As Range
Dim lastRow as Long
Set Wks1 = Worksheets("Sheet1")
Set Wks2 = Worksheets("Sheet2")
Set rng = Wks1.Range("C3:O69")
lastRow = Wks2.Cells(Rows.Count, 1).End(xlUp).row
For Each cell In rng
If Not (IsEmpty(cell.Value)) Then
For i = 1 To lastRow
With Wks2
If (InStr(.Cells(i, 1), cell.Value) <> 0) Or (InStr(.Cells(i, 3), cell.Value) <> 0) Then
.Cells(i, 4) = "String contains substring"
End If
End With
Next i
End If
Next cell
End Sub

If the first characters of a cell are GUF then Remove GUF if not leave it blank

I am still new to coding so i apologise if i dont understand everything.
I need to check each cell of D3:D5000 if they start with GUF. Then remove the GUF from it. Else dont do anything.
This is what ive been trying to use but im getting an error Do ohne Loop:
Sub RemoveGUFfromcellsstartingwithGUF()
Range("D3").Select
Selection.End(xlDown).Offset(1, 0).Select
ActiveCell = "end"
Range("B1").Select
Do Until ActiveCell = "end"
If ActiveCell = "GUF*" Then
ActiveCell.Value = Mid(Cell, 4, 999999)
End If
ActiveCell.Offset(1, 0).Select
End Sub
Thanks for any help/suggestions
Firstly, when you are looping through cells, it's best to use For each cell in cells, no need to change selection then.
Firstly, set a range in which you want it to run.
Sub RemoveGUFfromcellsstartingwithGUF()
dim first_cell as Range
dim last_cell as Range
dim rng as Range
set first_cell = ActiveSheet.Range("D1") 'first cell of your range
set last_cell = ActiveSheet.Range("D5000") 'last cell of your range
set rng = Range(first_cell, last_cell) 'range from first_cell to last_cell
For Each cell in rng.cells 'looping through cells of the range
'What you do here will be done to every cell.
if left(cell.value, 3) = "GUF" then cell.value = Mid(cell.value,4)
Next cell
End Sub
I hope this helps.
Is this what you are trying?
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long, i As Long
Set ws = Sheet1 '<~~ Change this to the relevant sheet
With ws
'~~> Find last row in Col D
lRow = .Range("D" & .Rows.Count).End(xlUp).Row
'~~> Loop through cell in Col D
For i = 3 To lRow
If .Range("D" & i).Value Like "GUF*" Then
.Range("D" & i).Value = Mid(.Range("D" & i).Value, 4)
End If
Next i
End With
End Sub

How to paste in successive rows

Sub NSV_LINK()
Dim cell As Range
Dim Rng As Range
Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
For Each cell In Rng
If cell.Value = "Hemanta" Then cell.EntireRow.Copy Sheets(2).Cells(1, 1)
Next cell
End Sub
In the code above, I want the macro to copy and paste values in successive rows in sheet 2. However, I have hard coded the destination cell i.e, the value gets pasted at A1. How do I write the cell destination, so that the values get pasted in successive rows? Cells(i, 1)...Something like this. And then i takes a range from, let's say 1 to 20. How do I write this in code?
you need a counter and you have to increment it
Sub NSV_LINK()
Dim cell As Range, Rng As Range, r As Long
Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
r = 1
For Each cell In Rng
If cell.Value = "Hemanta" Then
cell.EntireRow.Copy Sheets(2).Cells(r, 1)
r = r + 1
End If
Next cell
End Sub
you can adapt the same technique you already used in Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp)) to make destination range dynamic:
Sub NSV_LINK()
Dim cell As Range, Rng As Range
Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
For Each cell In Rng
If cell.Value = "Hemanta" Then cell.EntireRow.Copy Sheets(2).Cells(Rows.Count, 1).End(xlUp).Offset(1) ' make destination range dynamic to target sheeet column A first not empty cell after last not empty one
Next cell
End Sub

How to select cells between two values in column and draw the chart from selected elements

I have a problem. I need to write the macro which select the values in column E.
Values of selected items should be between values in cell T2 and U2.
After selection, macro should draw the chart.
I tried 3 ways:
First Approach:
Sub wykres1()
Dim rng As Range
Dim cell As Range
Set rng = Range("E1", Range("E65536").End(xlUp))
For Each cell In rng
If cell.Value > "T2" and cell.value < "U2" Then Cell.Select
With Selection
ActiveSheet.Shapes.AddChart2
End With
Next cell
End Sub
Wykres1 Doesn't work, because the line with if is highlighted on red.
Second Approach:
Sub wykres2()
Dim rng As Range
Dim cell As Range
Set rng = Range("E1", Range("E65536").End(xlUp))
For Each cell In rng
If cell.Value > ActiveSheet.Cell(2,20).Value and cell.value < ActiveSheet.Cell(2,21).Value Then Cell.Select
With Selection
ActiveSheet.Shapes.AddChart2
End With
Next cell
End Sub
Wykres2 Doesn't work, because the line with if is highlighted on red.
Third Approach:
Sub wykres3()
Dim rng As Range
Dim cell As Range
Set rng = Range("E1", Range("E65536").End(xlUp))
For Each cell In rng
If cell.value > -35 And cell.value < -32 Then cell.Select
With Selection
ActiveSheet.Shapes.AddChart2
End With
Next cell
End Sub
Wykres3 freeze after run. When I remove the part with draw chart, the
macro select one cell not the range with selected values. And here I
put the values in macro (-35) (-32) - but I'm interested in possibility
to put values from cells (T2) (U2).
As I mentioned - I need to create macro which select the cells in column E with values between values in cells T2 and U2. After selection macro must draw the chart.
Thank You for Your help.
Try this (Untested). avoid the use of .Select. Work with objects. You may want to see How to avoid using Select in Excel VBA
Sub wykres1()
Dim rng As Range, cell As Range
Dim lRow As Long, i As Long
Dim ws As Worksheet
'~~> Change as applicable
Set ws = Sheet1
With ws
'~~> Find last row in Col E
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
'~~> Loop though the range
For i = 1 To lRow
If .Range("E" & i).Value > .Range("T2").Value And _
.Range("E" & i).Value < .Range("U2").Value Then
With .Range("E" & i)
'
'~~> Do Something
'
End With
End If
Next i
End With
End Sub
As I mentioned - I need to create macro which select the cells in column E with values between values in cells T2 and U2. After selection macro must draw the chart.
You can store each range found above in one range object and then use that. See this example
Sub wykres1()
Dim rng As Range, cell As Range
Dim lRow As Long, i As Long
Dim ws As Worksheet
Dim Obj As ChartObject
'~~> Change as applicable
Set ws = Sheet1
With ws
'~~> Find last row
lRow = .Range("E" & .Rows.Count).End(xlUp).Row
'~~> Liip though the range
For i = 1 To lRow
If .Range("E" & i).Value > .Range("T2").Value And _
.Range("E" & i).Value < .Range("U2").Value Then
'~~> Store the cell in a range object
If rng Is Nothing Then
Set rng = .Range("E" & i)
Else
Set rng = Union(rng, .Range("E" & i))
End If
End If
Next i
'~~> Once you have the range, create a chart and assign range
If Not rng Is Nothing Then
With .ChartObjects.Add(Left:=100, Width:=375, Top:=75, Height:=225)
.Chart.SetSourceData Source:=rng
.Chart.ChartType = xlColumnClustered
End With
End If
End With
End Sub

Resources