How to change values of selected cells? - excel

I need to know how to, after selecting an amount of cells, use a button to apply its lines of code only on those selected cells.
i've tried selecting and just clicking on the button but didn't work.
if there's another option to allow me to do the same it's also a welcome idea
This is the code that i want to apply, but only on cells selected with my mouse, and not really pre-selected cells like the ones shown in the code.
If Range("h3").Value >= 0 Then
Range("bk3").Value = True
Else
Range("bk3").Value = False
End If
If Range("h3").Value >= 0 Then
Range("j3").Value = Range("j3").Value & " | " & VarNUMCB
Else
End If
If Range("h3").Value >= 0 Then
Range("l3").Value = Now
Else
End If

This will loop selected cells and alter them:
Set selectedRange = Application.Selection
For Each cell In selectedRange.Cells
'do something with the cell, like:
cell.value = cell.value + 1
Next cell

With Selection
If Range("h3").Value >= 0 Then
Range("bk3").Value = True
Range("j3").Value = Range("j3").Value & " | " & VarNUMCB
Range("l3").Value = Now
Else
Range("bk3").Value = False
End If
End With
If you are selecting a cell in column h, then this code uses offset from the selected cell.
With Selection
If .Value >= 0 Then
.Offset(, 55).Value = True
.Offset(, 2).Value = Range("j3").Value & " | " & "Yes"
.Offset(, 3).Value = Now
Else
.Offset(, 55).Value = Falsee
End If
End With
If you are selecting a range of cells in column h then you would have to put the above code within a For Each Cell in Selection loop
For Each cel in Selection
If cel.Value >= 0 Then
cel.Offset(, 55).Value = True
cel.Offset(, 2).Value = Range("j3").Value & " | " & "Yes"
cel.Offset(, 3).Value = Now
Else
cel.Offset(, 55).Value = False
End If
Next cel

Related

Checking a Range for Cell Colors Based On A Certain Row

This code should check a column for numbers greater than 7, and change the value of another cell. It does this fine. But then, if that is true (it finds one greater than 7), I want to check a range of cells in THAT row and change cell colors. I'm not sure how to replace H3:AL3 with H(that row):AL(that row).
I hope I am making sense. Any help is greatly appreciated. Thanks!! :)
For Each Cell In Range("F3:F66")
If Cell.Value > 7 And Cell.Offset(0, -2).Value = "Running" Then
Cell.Offset(0, -2).Value = "DEAD"
For Each DailyEarningsCell In Range("H3:AL3")
If DailyEarningsCell.Interior.ColorIndex = 2 Then
DailyEarningsCell.Interior.ColorIndex = 1
End If
Next DailyEarningsCell
ElseIf Cell.Value > 7 And Cell.Offset(0, -2).Value = "Ended/Running" Then
Cell.Offset(0, -2).Value = "ENDED/DEAD"
For Each DailyEarningsCell In Range("H3:AL3")
If DailyEarningsCell.Interior.ColorIndex = 2 Then
DailyEarningsCell.Interior.ColorIndex = 1
End If
Next DailyEarningsCell
End If
Next Cell
You refer to the .Row property of the range object which you are inferring with Cell.
So:
For Each Cell In Range("F3:F66")
If Cell.Value > 7 And Cell.Offset(0, -2).Value = "Running" Then
Cell.Offset(0, -2).Value = "DEAD"
For Each DailyEarningsCell In Range("H" & Cell.Row & ":AL" & Cell.Row)
If DailyEarningsCell.Interior.ColorIndex = 2 Then
DailyEarningsCell.Interior.ColorIndex = 1
End If
Next DailyEarningsCell
ElseIf Cell.Value > 7 And Cell.Offset(0, -2).Value = "Ended/Running" Then
Cell.Offset(0, -2).Value = "ENDED/DEAD"
For Each DailyEarningsCell In Range("H" & Cell.Row & ":AL" & Cell.Row)
If DailyEarningsCell.Interior.ColorIndex = 2 Then
DailyEarningsCell.Interior.ColorIndex = 1
End If
Next DailyEarningsCell
End If
Next Cell
Removing the repetition:
For Each cell In Range("F3:F66").Cells
If cell.Value > 7 Then
With cell.Offset(0, -2)
Select Case .Value
Case "Running": .Value = "DEAD"
Case "Ended/Running": .Value = "ENDED/DEAD"
End Select
End With
'Here `Range` is *relative* to `EntireRow`
For Each DailyEarningsCell In cell.EntireRow.Range("H1:AL1").Cells
If DailyEarningsCell.Interior.ColorIndex = 2 Then
DailyEarningsCell.Interior.ColorIndex = 1
End If
Next DailyEarningsCell
End If '>7
Next cell

Can't figure out problem with Excel VBA code

I am fairly new to writing code in excel VBA. Most of this code is some I have tried to replicated based on what other people have wrote. The problem I am having is I have a quantity counter and when a barcode is scanned into the cell (A4) it will add the barcode to a new cell (Starts at C8 and goes down) and if this barcode is already scanned once and is scanned again it will add one to the quantity. Now I am trying to add a date and time next to it as a barcode is scanned. This works but has an issue I can't figure out. The barcode must be scanned twice for the date to appear in the proper cell. This is an issue because it raises the quantity up one more than it should. Please help.
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Address
Case "$A$4"
If Target.Value <> "" Then
On Error Resume Next
Set xitem = Range("C8", Range("C" & Rows.Count) _
.End(xlUp)).Find(Range("A4").Value)
With xitem.Offset(0, -1)
.Value = .Value + 1
.Offset(0, 1).Select
End With
With xitem.Offset(0, 1)
.Value = Date & " " & Time
.NumberFormat = "m/d/yyyy h:mm AM/PM"
End With
On Error GoTo 0
If xitem Is Nothing Then
With Range("C" & Rows.Count).End(xlUp).Offset(1, 0)
.Value = Target.Value
.Offset(0, -1) = 1
End With
End If
Range("A4") = ""
Range("A4").Select
End If
Adds quantity
Case "$C$4"
If Target.Value <> "" Then
On Error Resume Next
Set xitem = Range("C8", Range("C" & Rows.Count).End(xlUp)) _
.Find(Range("C4").Value)
With xitem.Offset(0, -1)
.Value = .Value - 1
End With
If xitem Is Nothing Then MsgBox Target & " cannot be found " _
& "and cannot be removed."
Range("C4") = ""
Range("C4").Select
On Error GoTo 0
End If
Removes quantity (I am going to add an out time to this just trying to get the initial scan time in first)
Case "$E$4" 'find
If Target.Value <> "" Then
On Error Resume Next
Set xitem = Range("C8", Range("C" & Rows.Count).End(xlUp)) _
.Find(Range("E4").Value)
If xitem Is Nothing Then
MsgBox Target & " was not found."
Range("E4").Select
End If
Range("E4") = ""
xitem.Select
On Error GoTo 0
End If
End Select
End Sub
This is what I am using to take me directly to a barcode that has already been scanned.
Sorry if this post is badly formatted never posted before. Any and all help with this issue is appreciated. A photo of the spread sheet is also attached.
You are repeating some things within your code which you only need to do once, like the Find() for example.
Here's one alternative approach:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Adding As Boolean, Finding As Boolean, Subtracting As Boolean
Dim f As Range, v
Select Case Target.Address(True, True)
Case "$A$4": Adding = True
Case "$C$4": Subtracting = True
Case "$E$4": Finding = True
Case Else: Exit Sub
End Select
v = Trim(Target.Value)
If Len(v) = 0 Then Exit Sub
Set f = Me.Range("C8").Resize(1000, 1).Find(v, lookat:=xlWhole)
If Adding Then
If f Is Nothing Then
'not found: add as new row
Set f = Me.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)
f.Value = v
End If
f.Offset(0, 1).Value = f.Offset(0, 1).Value + 1
doDate f.Offset(0, -1)
Target.Value = ""
ElseIf Subtracting Then
If f Is Nothing Then
MsgBox v & " not found for subtraction!"
Else
f.Offset(0, 1).Value = f.Offset(0, 1).Value - 1
doDate f.Offset(0, -1)
Target.Value = ""
End If
Else 'finding
If Not f Is Nothing Then
f.EntireRow.Select
Target.Value = ""
Else
MsgBox v & " not found."
End If
End If
If Adding Or Subtracting Then Target.Select
End Sub
Sub doDate(c As Range)
With c
.NumberFormat = "m/d/yyyy h:mm AM/PM"
.Value = Now
End With
End Sub

excel "insert line" causing error with target.offset

I have this simple bit of code that automates some dates and stuff when adding line items to a sheet. It works well, but when I insert a line in to the spreadsheet [right-click the line name > insert] an error occurs.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim plusWeek
plusWeek = Now() + 7
For Each cell In Target
'========adds closed date, deleted date if status degenerates=========
If cell.Column = 13 And cell = "Closed" Then
Target.Offset(0, -2) = Format(Now(), "yyyy-mm-dd")
End If
If cell.Column = 13 And cell = "In-Progress" Then
Target.Offset(0, -2) = ""
End If
If cell.Column = 13 And cell = "Open" Then
Target.Offset(0, -2) = ""
End If
'========adds date added if date is embty and description is not empty========
If cell.Column = 8 And IsEmpty(Target.Offset(0, 1)) And Not IsEmpty(Target.Offset(0, 0)) Then
Target.Offset(0, 1) = Format(Now(), "yyyy-mm-dd")
Target.Offset(0, 2) = Format(plusWeek, "yyyy-mm-dd")
Target.Offset(0, 5) = "Open"
End If
'========deletes date added if description is empty========
'If cell.Column = 8 And IsEmpty(Target.Offset(0, 0)) Then
' Target.Offset(0, 1) = ""
'End If
Next cell
End Sub
if I paste a line, add a line or delete a line, error 1004 occurs. The debugger highlights this line, but I can't understand where the error comes from.
If cell.Column = 8 And IsEmpty(Target.Offset(0, 1)) And Not
IsEmpty(Target.Offset(0, 0)) Then
Something like this should work:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range, rng As Range
Dim plusWeek
plusWeek = Now() + 7
Set rng = Application.Intersect(Target, Me.Range("H:H,M:M"))
If rng Is Nothing Then Exit Sub
On Error GoTo haveError '<< make sure events don't get left turned off
Application.EnableEvents = False '<< turn events off
For Each cell In rng.Cells
'========adds closed date, deleted date if status degenerates=========
If cell.Column = 13 Then
Select Case cell.Value
Case "Closed": cell.Offset(0, -2) = Format(Now(), "yyyy-mm-dd")
Case "In-Progress", "Open": cell.Offset(0, -2) = ""
End Select
End If
'========adds date added if date is embty and description is not empty========
If cell.Column = 8 And IsEmpty(cell.Offset(0, 1)) And Not IsEmpty(cell) Then
cell.Offset(0, 1) = Format(Now(), "yyyy-mm-dd")
cell.Offset(0, 2) = Format(plusWeek, "yyyy-mm-dd")
cell.Offset(0, 5) = "Open"
End If
'========deletes date added if description is empty========
'If cell.Column = 8 And IsEmpty(Target.Offset(0, 0)) Then
' Target.Offset(0, 1) = ""
'End If
Next cell
haveError:
Application.EnableEvents = True
End Sub

Trying to VBA Script some embedded if commands

What I am trying to do is get my macro to search the data in Column "E". If the cell value contains "string", then I would like to offset by one column to the left, verify if, in the new selected cell, cell value contains "". If the new selected cell value is "" then background color is 19, if it contains "*" then background color is -4142.
Here is the code I have so far:
Sub Set_Background_Color ()
lRow = Range("E" & Rows.Count).End(xlUp).Row
Set MR = Range("E2:E" & lRow)
For Each cell In MR
If cell.Value = "X" Then cell.Offset(, -1).Interior.ColorIndex = 19
Next
End Sub
I can't seem to figure out how to embed a new If statement after the Offset and before the .Interior.ColorIndex
I have tried this mess but you will see immediately that it does not work.
If cell.Value = "X" Then
ElseIf cell.Offset(, -1).Value = "" Then cell.Interior.ColorIndex = 19
Else: cell.Interior.ColorIndex = -4142
Any help is greatly apreciated!
So close!
Sub Set_Background_Color ()
Dim lRow As Long
Dim MR As Range
Dim cel As Range
lRow = Range("E" & Rows.Count).End(xlUp).Row
Set MR = Range("E2:E" & lRow)
For Each cel In MR
If cel.Value = "string" Then
If cel.Offset(, -1).Value = "" Then
cel.Offset(, -1).Interior.ColorIndex = 19
ElseIf cel.Offset(, -1).Value = "*" Then
cel.Offset(, -1).Interior.ColorIndex = -4142
End If
End If
Next
End Sub
If by contains "*" you mean "has any content" then:
If cell.Value = "X" Then
cell.Interior.ColorIndex = IIf(Len(cell.Offset(0, -1).Value) = 0, 19, xlNone)
End If

Compare Four Columns with two column return?

I been having an issue. What I'm trying to accomplish is compare four columns, if the cells match then return two cells in the same row.
For an example I'm comparing both A&B to D&E with an output of F&G on the same row. The destination doesn't matter much as I can change it.
What I have done only compares two columns, which works, but it also adds other cells that shouldn't apply to that particular line.
Sub Add_XY()
For Each cell In ThisWorkbook.Sheets("Data").UsedRange.Columns("K").Cells
Dim offs As Long: offs = 2 ' <-- Initial offset, will increase after each match
compareValue = cell.Value & "-" & cell.Offset(, 1).Value
ThisWorkbook.Sheets("Data").Range("K6").Value = compareValue
If Not compareValue = "-" Then
For Each compareCell In ThisWorkbook.Sheets("P&T Data").UsedRange.Columns("AI").Cells
'For Each compareCell In ThisWorkbook.Sheets("Data").UsedRange.Columns("A").Cells
If compareCell.Value & "-" & compareCell.Offset(, 1).Value = compareValue Then
ThisWorkbook.Sheets("Data").Range("K6").Value = compareCell.Value & "-" & compareCell.Offset(, 1).Value 'test return value
cell.Offset(, offs).Value = compareCell.Offset(, 5).Value
cell.Offset(, offs + 1).Value = compareCell.Offset(, 6).Value
offs = offs + 4 ' <-- now shift the destination column by 4 for next match
Else
End If
Next compareCell
End If
Next cell
End Sub
Working with the data entered exactly as shown in your picture.
Sub Test()
For Each cell In ThisWorkbook.Sheets("Data").UsedRange.Columns("A").Cells
compareValue = cell.Value & "-" & cell.Offset(0, 1).Value
If Not compareValue = "-" Then
For Each compareCell In ThisWorkbook.Sheets("Data").UsedRange.Columns("A").Cells
If compareCell.Offset(0, 3).Value & "-" & compareCell.Offset(0, 4).Value = compareValue Then
cell.Offset(0, 8) = cell.Offset(0, 5)
cell.Offset(0, 9) = cell.Offset(0, 6)
Else
End If
Next compareCell
End If
Next cell
End Sub

Resources