VBA reading cell value not formula

ClaireLou

New Member
Joined
Nov 14, 2020
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi

I have the code attached, its an auto sheet event script

How do I get the vba code to read the value and not the formula? I need it to move the rows depending on the status in column A and that changes based on the values in other columns.
I’ve put the code I’m using below. It works perfectly if I actually type in the phrase I’m using.
Thanks for any help 😁
Claire

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
Application.ScreenUpdating = False
Dim Lastrow As Long
Dim Lastrowa As Long
Lastrow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1
Lastrowa = Sheets("Sheet3").Cells(Rows.Count, "A").End(xlUp).Row + 1
If Target.Value = "To be Booked" Then
Rows(Target.Row).Copy Destination:=Sheets("Sheet2").Rows(Lastrow)
Application.EnableEvents = False
Rows(Target.Row).Delete
Application.EnableEvents = True
Exit Sub
End If
Application.EnableEvents = True
If Target.Value = "Booked" Then
Rows(Target.Row).Copy Destination:=Sheets("Sheet3").Rows(Lastrowa)
Application.EnableEvents = False
Rows(Target.Row).Delete
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End If
End Sub
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows
Change events do not get triggered by a formula.
Does the formula in col A look only at that sheet, or does it depend on values on another sheet?
 

ClaireLou

New Member
Joined
Nov 14, 2020
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Change events do not get triggered by a formula.
Does the formula in col A look only at that sheet, or does it depend on values on another sheet?
Hi
It only looks at the one sheet
Thanks
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows
Ok, how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim KyCell As Range
   
   If Target.CountLarge > 1 Then Exit Sub
   Set KyCell = Range("A:A")
   On Error Resume Next
   Set KyCell = Union(KyCell, KyCell.Precedents)
   On Error GoTo 0
   If Not Intersect(Target, KyCell) Is Nothing Then
      Select Case Range("A" & Target.Row).Value
         Case "To be Booked"
            With Target.EntireRow
               .Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
               .Delete
            End With
         Case "Booked"
            With Target.EntireRow
               .Copy Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1)
               .Delete
            End With
      End Select
   End If
End Sub
 

ClaireLou

New Member
Joined
Nov 14, 2020
Messages
10
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Ok, how about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   Dim KyCell As Range
  
   If Target.CountLarge > 1 Then Exit Sub
   Set KyCell = Range("A:A")
   On Error Resume Next
   Set KyCell = Union(KyCell, KyCell.Precedents)
   On Error GoTo 0
   If Not Intersect(Target, KyCell) Is Nothing Then
      Select Case Range("A" & Target.Row).Value
         Case "To be Booked"
            With Target.EntireRow
               .Copy Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)
               .Delete
            End With
         Case "Booked"
            With Target.EntireRow
               .Copy Sheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1)
               .Delete
            End With
      End Select
   End If
End Sub
It’s not doing anything. Now when I change the target cell manually, it’s not changing either
 

Dossfm0q

Banned User
Joined
Mar 9, 2009
Messages
570
Office Version
  1. 2019
Platform
  1. Windows
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A:A")) Is Nothing Then
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub


Application.ScreenUpdating = False
Dim Lastrow As Long
Dim Lastrowa As Long
Lastrow = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1
Lastrowa = Sheets("Sheet3").Cells(Rows.Count, "A").End(xlUp).Row + 1
If Target.Value = "To be Booked" And Target.HasFormula = False Then

Rows(Target.Row).Copy Destination:=Sheets("Sheet2").Rows(Lastrow)
MsgBox Rows(Target.Row).Address
Exit Sub

Application.EnableEvents = False
Rows(Target.Row).Delete
Application.EnableEvents = True
End If
Application.EnableEvents = True
If Target.Value = "Booked" And Target.HasFormula = False Then
Rows(Target.Row).Copy Destination:=Sheets("Sheet3").Rows(Lastrowa)
Application.EnableEvents = False
Rows(Target.Row).Delete
End If
Application.EnableEvents = True
Application.ScreenUpdating = True
End If
End Sub
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

It’s not doing anything
As VBA is case sensitive are the col A values exactly "Booked" and "To be Booked"?

@Dossfm0q
The OP has stated that col A has a formula, therefore that code will not work.
 

ClaireLou

New Member
Joined
Nov 14, 2020
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I think it might be the entry of multiple cells at once. Do you guys know a better code, even one that I have to run manually, that will move the row based on the output in column A? Booked & To be booked still need to go to separate sheets and blank to stay on the original. Thanks
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows
What is the name of the sheet that has the data?
 

Watch MrExcel Video

Forum statistics

Threads
1,127,653
Messages
5,626,101
Members
416,161
Latest member
David1966Lewis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top