how to add a value to cell - vba

I am using the spreadsheet to do inventory- scanning parts in bin
I am trying to add a A2 Value next to the (Target.Row, 5)(row 6)
how do I make A2 or A9 or the next scan Value show up on Column F so if column E & F don't match I know the part is sitting on the wrong bin
I have tried .Value = Target.Worksheet.Cells(Target.Row, 1).Value but not working
Here is my complete code
Option Explicit
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Cells.Count > 1 Or IsEmpty(Target) Or Target.Column <> 1 Then Exit Sub
If Not SheetExists("WarehouseInventory") Then Exit Sub
Dim Result As Variant
Set Result = Sheets("WarehouseInventory").Cells.Range("E:E").Find(Target)
If Result Is Nothing Then
Target.Worksheet.Cells(Target.Row, 2) = "Data New Bin"
Else
Target.Worksheet.Cells(Target.Row, 2) = Result.Worksheet.Cells(Result.Row, 4)
Target.Worksheet.Cells(Target.Row, 3) = Result.Worksheet.Cells(Result.Row, 5)
Target.Worksheet.Cells(Target.Row, 4) = Result.Worksheet.Cells(Result.Row, 6)
Target.Worksheet.Cells(Target.Row, 5) = Result.Worksheet.Cells(Result.Row, 7)
End If
End Sub
Public Function SheetExists(SheetName As String) As Boolean
Dim ws As Worksheet
SheetExists = False
For Each ws In ThisWorkbook.Worksheets
If ws.Name = SheetName Then SheetExists = True
Next ws
End Function

this an example how you can achieve required result
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x&, y&
If Target.Column = 6 Then
y = 5: x = Target.Row
While Cells(x, y) <> "": x = x - 1: Wend
If Cells(Target.Row, 5) <> "" Then Cells(Target.Row, 6) = Cells(x, 1)
End If
End Sub
output
UPDATE
here your updated code, tested, works fine, insert it into "Cycle Count" sheet
Private Sub Worksheet_Change(ByVal Target As Range)
Dim x&, y&, Result As Range, ws As Worksheet
Application.EnableEvents = 0
If Target.Count > 1 Then
Application.EnableEvents = 1
Exit Sub
End If
If Not Intersect(Target, [A1:A99999]) Is Nothing Then
With ThisWorkbook
For Each ws In .Worksheets
If ws.Name = "WarehouseInventory" Then
Set ws = .Worksheets("WarehouseInventory")
Set Result = ws.[E:E].Find(Target.Value)
Exit For
End If
Next ws
If Result Is Nothing Then
Target.Offset(, 1).Value = "Data New Bin"
Application.EnableEvents = 1
Exit Sub
Else
Target.Offset(, 1).Value = ws.Range(Result.Address).Offset(, -1).Value
Target.Offset(, 2).Value = ws.Range(Result.Address).Value
Target.Offset(, 3).Value = ws.Range(Result.Address).Offset(, 1).Value
Target.Offset(, 4).Value = ws.Range(Result.Address).Offset(, 2).Value
y = 5: x = Target.Row
While .ActiveSheet.Cells(x, y) <> "": x = x - 1: Wend
Target.Offset(, 5).Value = .ActiveSheet.Cells(x, 1).Value
End If
End With
End If
Application.EnableEvents = 1
End Sub
sample file here http://www.filedropper.com/book1_5

What about Target.Worksheet.Cells(Target.Row, 6).Value = Target.Worksheet.Cells(Target.Row,1).Value ?
If you want only A2, in all rows substitute Target.Row with 2

Related

Limit the prefix vba function to dynamic range

enter link description hereI want to limit below vba code to the column A in dynamic range ONLY. Right now if I enter something outside the range, it shows error and disrupts other functions in Workbook_SheetChange.
I attach my file for easy ref.
Please HELP ! HELP
'Formate Column A
If Target.Column = 1 Then
Dim s As String
Dim arr As Variant
s = Target.Value
If s = "" Then
Target.NumberFormat = "General"
Else
With CreateObject("vbscript.regexp")
.Pattern = "[^0-9]"
.Global = True
.IgnoreCase = True
arr = Split(Application.Trim(.Replace(s, " ")), " ")
End With
Target.Value = arr
Target.Value = Target.Value * 1
Target.NumberFormat = """REQ0000000""General"
End If
'Excel file link : https://drive.google.com/file/d/13w-AbgY83g02qHGqBrHr_6N26UkNpWKr/view?usp=sharing
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call Module1.DeleteCheck
End Sub
Private Sub Workbook_Open()
Call Module1.CreateCheck
'Application.MoveAfterReturnDirection = xlToRight
'Application.MoveAfterReturn = True
End Sub
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Select Case Sh.Name
Case "Agents"
Exit Sub
Case Else
End Select
If Target.Column > 4 Or Target.CountLarge > 1 Then Exit Sub
If Target.Row = 1 Then Exit Sub
Application.EnableEvents = False
If InStr(1, Cells(Target.Row, "A"), "REQ") <> "" And Cells(Target.Row, "B") <> "" Then
Cells(Target.Row, "C") = ActiveSheet.Name
Cells(Target.Row, "C").Font.Name = "Times New Roman"
Cells(Target.Row, "C").Font.Size = 12
Cells(Target.Row, "C").HorizontalAlignment = xlRight
Cells(Target.Row, "D").ShrinkToFit = True
Cells(Target.Row, "A").Font.Name = "Times New Roman"
Cells(Target.Row, "A").Font.Size = 12
Cells(Target.Row, "A").HorizontalAlignment = xlLeft
Cells(Target.Row, "B").Font.Name = "Times New Roman"
Cells(Target.Row, "B").Font.Size = 12
Cells(Target.Row, "B").HorizontalAlignment = xlLeft
End If
'Formate Column A
If Target.Column = 1 Then
Dim s As String
Dim arr As Variant
s = Target.Value
If s = "" Then
Target.NumberFormat = "General"
Else
With CreateObject("vbscript.regexp")
.Pattern = "[^0-9]"
.Global = True
.IgnoreCase = True
arr = Split(Application.Trim(.Replace(s, " ")), " ")
End With
Target.Value = arr
Target.Value = Target.Value * 1
Target.NumberFormat = """REQ0000000""General"
End If
End If
'Set Cell Movement within The Range
'https://www.mrexcel.com/board/threads/set-movement-of-cells-in-dynamic-range-only.1172539/
If Target.CountLarge > 1 Then Exit Sub
Dim rng As Range
Set rng = Range("A1").CurrentRegion
If rng.Rows.Count > 1 Then
Set rng = Intersect(Target, rng.Offset(1, 0).Resize(rng.Rows.Count - 1, rng.Columns.Count))
Else
Set rng = Nothing
End If
If Not rng Is Nothing Then
If Target.Column = 2 And Not (IsEmpty(Target)) Then
Target.Offset(, 2).Select
Else
Target.Offset(, 1).Select`enter code here`
End If
End If
Application.EnableEvents = True
End Sub
Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
Select Case Sh.Name
Case "Agents"
Exit Sub
Case Else
End Select
If Target.Column <> 5 Or Application.CountA(Cells(Target.Row, 1).Resize(, 2)) < 2 Then Exit Sub
Cancel = True
Call Module3.SelectOLE3
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Select Case Sh.Name
Case "Agents"
Exit Sub
Case Else
End Select
If g_blnWbkShtSelChange Then Exit Sub
If Selection.Count = 1 Then
If Not Intersect(Target, Range("C1")) Is Nothing Then
g_blnWbkShtSelChange = True
Call Module1.CheckSheet
End If
End If
End Sub

Worksheet_Change - 3 column multiplication evaluate to

I'm new to vba and frustrated.
I have the following code :
Private Sub Worksheet_Change(ByVal Target As Range)
Dim VolRange As Range
Dim AffectVolRange As Range
Set VolRange = ActiveSheet.Range("AH:AK")
Set AffectVolRange = Intersect(Target, VolRange)
If Not AffectVolRange Is Nothing Then
Dim vRow As Variant
For Each vRow In AffectVolRange.Rows
With VolRange
Cells(vRow.Row, 37).Value = .Cells(vRow.Row, 34).Value * .Cells(vRow.Row, 35).Value *
.Cells(vRow.Row, 36).Value
End With
Next vRow
End If
End Sub
Initial value in columns 34,35,36, is null
You almos got it.
Take into account that your column indexes are relative to your new range, so 1, 2, 3, 4.
Also you need to disable the events with Application.EnableEvents = False to avoid recursive call to the function on the cells changed by the code event.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim VolRange As Range
Dim AffectVolRange As Range
Set VolRange = ActiveSheet.Range("AH:AK")
Set AffectVolRange = Intersect(Target, VolRange)
If Not AffectVolRange Is Nothing Then
Dim vRow As Variant
Application.EnableEvents = False
For Each vRow In AffectVolRange.Rows
With VolRange
.Cells(vRow.Row, 4).Value = .Cells(vRow.Row, 1).Value _
* .Cells(vRow.Row, 2).Value _
* .Cells(vRow.Row, 3).Value
End With
Next vRow
Application.EnableEvents = True
End If
End Sub

Forcing user to update another cell after updating first cell

I have a spreadsheet with a list of sales on it.
When a user sets the "Status" column to "Closed" I want to force them to enter a date in the "Closed Date" column.
So I have this;
A1 (Status), B1 (Closed Date)
Open, <blank>
Open, <blank>
Closed, 1/1/2018
Right click on the Sheet Tab --> View code and paste the code given below into the opened code window and save your workbook as Macro-Enabled Workbook.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
If Target.Column = 1 And Target.Row > 1 Then
If Target <> "" And LCase(Target.Value) = "closed" Then
With Target.Offset(0, 1)
.Select
On Error Resume Next
.Comment.Delete
On Error GoTo 0
.AddComment.Text "Please enter the Closed Date"
.Comment.Visible = True
End With
Else
On Error Resume Next
Target.Offset(0, 1).Comment.Delete
On Error GoTo 0
End If
ElseIf Target.Column = 2 And Target.Row > 1 Then
If LCase(Target.Offset(0, -1)) = "closed" And IsDate(Target) Then
Target.Comment.Delete
End If
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub
Dim x
Dim i As Long, lr As Long
Application.ScreenUpdating = False
Application.EnableEvents = False
lr = Cells(Rows.Count, 1).End(xlUp).Row
x = Range("A2:B" & lr)
If Target <> "" Then
Application.EnableEvents = True
Exit Sub
End If
For i = 1 To UBound(x, 1)
If LCase(x(i, 1)) = "closed" And Not IsDate(x(i, 2)) Then
Cells(i + 1, 2).Select
Exit For
End If
Next i
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub

Copy and Paste a range of data in sheet 1 to another sheet 2 by current date

I am very new to VBA. Basically I have the following table on sheet 1
table 1
This gets updated daily with # emails we receive depending country / reason for contact.
At the end of the day I would like to assign a macro button that will copy and paste the data to sheet 2 by current date.
sheet 2
I wrote a dynamic solution to this:
Option Explicit
Option Base 1
Type EmailData
us As Object
ca As Object
End Type
Public Sub RunDataMove()
Dim wsDataFrom As Worksheet
Dim wsDataTo As Worksheet
Dim eData As EmailData
Dim i As Long
With Application
.ScreenUpdating = False
.DisplayAlerts = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Set wsDataFrom = ThisWorkbook.Worksheets("DataFrom") 'Change Worksheet names
Set wsDataTo = ThisWorkbook.Worksheets("DataTo") 'Change Worksheet names
Set eData.us = CreateObject("Scripting.Dictionary")
Set eData.ca = CreateObject("Scripting.Dictionary")
With wsDataFrom
For i = 2 To .Cells.SpecialCells(xlCellTypeLastCell).Row
eData.us.Add .Cells(i, 3).Value, .Cells(i, 1).Value
eData.ca.Add .Cells(i, 3).Value, .Cells(i, 2).Value
Next i
End With
Call MoveDataByDate(wsDataTo, eData, DateAdd("d", 0, Date)) 'Change add days +/- if needed
With Application
.ScreenUpdating = True
.DisplayAlerts = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
MsgBox "Data for " & Date & " has been moved or updated"
End Sub
Public Sub MoveDataByDate(ByRef wsTo As Worksheet, ByRef eData As EmailData, ByVal eDate As Date)
Dim obj As Variant, i As Long, usCol As Long, caCol As Long, dCol As Long, keyName As String
With wsTo
For i = 1 To .Cells.SpecialCells(xlCellTypeLastCell).Column
If .Cells(1, i).Value = eDate Then
dCol = i
usCol = i
caCol = i + 1
Exit For
End If
Next i
If dCol = 0 And i <> 1 Then
usCol = i
caCol = i + 1
Else
usCol = 2
caCol = 3
End If
If .Cells(3, 1).Value = "" Then
i = 3
For Each obj In eData.us
.Cells(i, 1).Value = obj
i = i + 1
Next obj
.Cells.EntireColumn.AutoFit
End If
For i = 3 To .Cells.SpecialCells(xlCellTypeLastCell).Row
keyName = .Cells(i, 1).Value
If eData.us.exists(keyName) Then
.Cells(i, usCol).Value = eData.us(keyName)
End If
If eData.ca.exists(keyName) Then
.Cells(i, caCol).Value = eData.ca(keyName)
End If
Next i
.Cells(1, usCol).Value = eDate
.Range(.Cells(1, usCol), .Cells(1, caCol)).Merge
.Cells(2, usCol).Value = "US"
.Cells(2, caCol).Value = "CA"
With .Range(.Cells(1, usCol), .Cells(.Cells.SpecialCells(xlCellTypeLastCell).Row, caCol))
.ColumnWidth = 8
.HorizontalAlignment = xlCenter
End With
End With
End Sub

VBA Excel "Object Required" error

My codes gives me a Object Required 424 error on this line:
lngRow = .Cells(.Rows.Count, 1).End(xlUp).Row
My full code:
Private Sub Worksheet_Change(ByVal Target As Range)
' If Target.Count > 1 Then Exit Sub
' If Target.Column > 2 Then Exit Sub
Application.EnableEvents = False
If Target.Column = 6 Then
If Target.Offset(0, 1).Value <> "" Then
MsgBox "You must only fill in one of the two columns"
Target.ClearContents
GoTo ExitSub
End If
End If
If Target.Column = 7 Then
If Target.Offset(0, -1).Value <> "" Then
MsgBox "You must only fill in one of the two columns"
Target.ClearContents
GoTo ExitSub
End If
End If
Dim arrData() As Variant
Dim i As Long
Dim lngRow As Long
Dim myNum As Variant
Dim ws As Worksheet
myNum = Target.Value
If Target.Column = 6 Then
With BogieInspectionPoints 'this is a sheet name
lngRow = .Cells(.Rows.Count, 1).End(xlUp).Row
arrData = .Range("a1:b" & lngRow)
End With
End If
If Target.Column = 7 Then
With WagonInspectionPoints 'this is a sheet name
lngRow = .Cells(.Rows.Count, 1).End(xlUp).Row
arrData = .Range("a1:b" & lngRow)
End With
End If
For i = 1 To lngRow
If myNum = arrData(i, 1) Then
Cells(Target.Row, 8).Value = arrData(i, 2)
Exit For
End If
Next
ExitSub:
Application.EnableEvents = True
End Sub
It looks like those sheet variables aren't set.
You will need to add this at the top.
Dim BogieInspectionPoints as Worksheet
Dim WagonInspectionPoints as Worksheet
Set BogieInspectionPoints = ActiveWorkbook.Sheets("BogieInspectionPoints")
Set WagonInspectionPoints = ActiveWorkbook.Sheets("WagonInspectionPoints")
I was assuming there was other code. When you add this line all the With statements should process correctly using the code you posted.
What you're doing with the With statements is shorthanding the object. Instead of writing
BogieInspectionPoints.Range("A1")
'More code
You can write
With BogieInspectionPoints
.Range("A1")
End With
It keeps you from having to write the full object name out.