I have a single workbook with multiple sheets. Sheet 1 uses cell A1 as a search box to find a specific sheet in the book. The sheets after Sheet 1 are named like this, SO123456 with different numbers following the SO. I have the following code on sheet 1 to open the corresponding sheet from the value typed into A1 of Sheet 1:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Range("A1"), Target) Is Nothing Then Exit Sub
Sheets(Target.Value).Activate
End Sub
When the corresponding sheet is opened, I want to open it to the next empty cell in column A. I have some code on the SOxxxxxx sheets that populates time in Column C when my data is entered in Column A. Here is that code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Columns("A:A")) Is Nothing Then
Target.Offset(0, 2).Value = Now
End If
End Sub
I'm not sure how to make sure the first empty cell in Column A is selected or where the code goes; sheet being opened or where the call is to open the sheet. I tried entering the following code below the Worksheet_Change block with no luck:
Range("A" & Rows.Count).End(xlup).Offset(1).Row
Cells(Rows.Count,1).End(xlup).Offset(1).Row
UsedRange.Rows.Count
I know next to nothing about VBA if you haven't already noticed.
Try this
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "A$1” Then Exit Sub
With WorkSheets(Target.Value)
.Activate
.Cells(.Rows.Count,1).End(xlup).Offset(1).Select
End With
End Sub
This should get you to the first empty cell in Column A
rowEmpty = Range("A" & 10000).End(xlup).Row +1
You can replace the number 10000 with a sufficiently large number that ensures there will be no data there. There are alternatives to that, but I think this one is easier to begin with.
With this code, typing a sheet name in cell A1 from sheet1 will activate the sheet you type, and the active cell will be the first one in blank in column A.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Range("A1"), Target) Is Nothing Then Exit Sub
Sheets(Target.Value).Activate
ActiveSheet.Cells(Sheets(Target.Value).Range("A1").End(xlDown).Row + 1, 1).Select
End Sub
Related
I was attempting to set up a way to jump to sheets in the same workbook by clicking on a cell and activating the sheet with the same value. My code works when the cell value is text. However, it does not work when the value is a number. The majority (99%) of the sheets will be only numbers (6 digits) for the sheet name. An example of a sheet name would be 110110. Here is my code I used:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
If Selection.Count = 1 Then
If Not Intersect(Target, Range("B4:B800")) Is Nothing Then
If Target.Value <> "" Then
Sheets(Target.Value).Activate
End If
End If
End If
End Sub
I receive:
Run-time error '9':
Subscript out of range
whenever I click on the
In Excel, you can refer to a sheet by the name or index.
In your case, you are referring by index (number) that does not exist in the Excel.
You can simple convert the number to string by using CStr():
Sheets(CStr(Target.Value)).Activate
Hy Experts, I am using an excel sheet that has multiple columns. Every column has validation rules. I want to remove these validation rules upon selecting any row in the entire sheet. When I select the row then I will put the value in G from drop down menu. on the basis of column G value the entire row validation rules should be cleared. The sheet looks like this.
enter image description here
The code that I am using is as under.
Sub RemoveDV()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Cells.Validation.Delete
Next ws
MsgBox ("All Validation Rules has been removed successfully")
End Sub
it working fine but it delete the entire worksheet. But I want to delete the validation only row that I am working on.
Thanks
Try this:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Debug.Print Target.Address
If Not Intersect(Target, Range("G:G")) Is Nothing And Target.Count = 1 Then
If Target.Value = "The value that will trigger the validation clearing" Then
Target.EntireRow.Validation.Delete
End If
End If
End Sub
This will trigger anytime you change a value in column "G" of the specified sheet and if the new value = the text you want.
EDIT:
Let say you store your values (values that will trigger the script) in a range on the same page you can adapt following code to your needs:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Arr: Arr = Application.Transpose(Range("$P$1:$P$12").Value) 'Values stored in "P1:P12"
If Not Intersect(Target, Range("G:G")) Is Nothing And Target.Count = 1 Then
If UBound(Filter(Arr, Target.Value)) > -1 Then
Target.EntireRow.Validation.Delete
End If
End If
End Sub
I am looking for a script that will pull data from last edited cell into Cell B1 of active sheet and also look up data from cell in column A and then display it in cell A1.
So far I've got this to pull last edited cell into B1 and it works fine but I cannot figure out how to then go back from that point to row A and display the other info.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("F13:W9910")) Is Nothing Then
ActiveSheet.Range("B1").Value = Target.Value
End If
End Sub
In the attached picture if I add any numbers in section called trays completed (in red) to display in B1 and then look up number in Sap column and display the number in cell A1
Something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("F13:W9910")) Is Nothing Then
Me.Range("B1").Value = Target.Value
Me.Range("A1").Value = Target.Entirerow.Cells(1).Value
End If
End Sub
Note when in a sheet code module you can refer to the worksheet using Me
Also be aware that Target might be >1 cell and your code might need to handle that.
This will take the corresponding value in A and place it in A1. I only added one line of code to yours.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("F13:W9910")) Is Nothing
Then
ActiveSheet.Range("B1").Value = Target.Value
ActiveSheet.Range("A1").Value = ActiveSheet.Range("A" & Target.Row)
End If
End Sub
So I have 2 sheets in my excel book, there are columns with dates as shown here http://prntscr.com/j4931e (this is in sheet 2) for each type of attendance (eg present or absent) I type into a cell I want it to update that same cell that is part of a column B in sheet 1 http://prntscr.com/j494s2. I hope you understand what I mean. Thanks.
Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets(2).Range(Target.Address) = Target
End Sub
Try this. There are two options as not 100% sure what you're after. The code should go in the sheet 2 module. You might want to restrict the range triggering it, e.g. columns B-F only?
Private Sub Worksheet_Change(ByVal Target As Range)
If target.column>1 then
Worksheets(1).Range("B" & target.row).Value = Target.Value
End if
End Sub
I have a macro so that when you highlight a row on sheet1, the macro takes all the info from this row and displays this by itself on sheet2. If you highlight a different row on sheet1, the info on sheet2 is changes to show the info from that row.
My problem is that if I change the info displayed on sheet2, it doesn't change the info on sheet1. Is there a way I could add this functionality?
I have the following code at the moment:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim myList
If Target.Address <> Target.EntireRow.Address Then Exit Sub
If Target.Rows.Count > 1 Then Exit Sub
myList = [{"B1","B2","B3","B4","B5","B6","B7","B8","B9","B10","B11","B12","B13","B14","B15"}] '<- adjust to your need
With Target.EntireRow
For i = 1 To UBound(myList)
Sheets("sheet2").Range(myList(i)).Value = .Cells(i).Value
Next
End With
End Sub
Any Help would be awesome! :)
After copying your sheet1 row to sheet2 you could also record the original row # that the values came from. Then you can add an additional macro that would compare the sheet2 values with the values in sheet1 - any changes could then be migrated over.
A possible basic flow:
copy sheet1 row to sheet2 (current macro)
copy sheet1 row # to sheet2 (ie one row down)
make changes on sheet2
copy sheet2 row to sheet1 row (use row # saved on sheet2) -> this assumes that no changes will be made to sheet1.
You are currently using a Worksheet_SelectionChange event macro to recognize when a full single row has been selected. You need a Worksheet_Change event macro for Sheet2 to recognize when values in the B1:B15 range have been changed and pass the changes back to Sheet1.
Because the Worksheet_Change is triggered on a change in values, you will need to disable the Application.EnableEvents property so that it is not triggered when you write the values from Sheet1's Worksheet_SelectionChange sub.
You are going to require a couple of public variables. One to remember the position that changes should be returned to and another to locate the target cells on Sheet2. These can only be made public in a module code sheet.
Book1 - Module1 (Code)
Option Explicit
Public Const sRNG As String = "B1:B15"
Public rRNG As Range
I've made a couple of small modifications to your original Worksheet_SelectionChange and added the disabling of event handling.
Book1 - Sheet1 (Code)
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells.Count = Columns.Count And Target.Rows.Count = 1 And _
CBool(Application.CountA(Target)) Then '<~~ one complete non-blank row
On Error GoTo bm_Safe_Exit
Application.EnableEvents = False
With Sheet2.Range(sRNG)
Set rRNG = Target.Cells(1, 1).Resize(.Columns.Count, .Rows.Count)
.Cells = Application.Transpose(rRNG.Value)
End With
End If
bm_Safe_Exit:
Application.EnableEvents = True
End Sub
The Worksheet .CodeName property was used to identify Sheet2 since this does not change if the worksheet is conventionally renamed.
It is a little unclear on how you were planning to identify the row to return the values to once they were changed. I've used a public range-type variable declared in Module1 to record the last location that values were transferred from Sheet1 to Sheet2. Changes on Sheet2 will return them to the last recorded location.
Book1 - Sheet2 (Code)
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range(sRNG)) Is Nothing Then
Debug.Print rRNG.Address(0, 0, external:=True)
On Error GoTo bm_Safe_Exit
Application.EnableEvents = False
rRNG = Application.Transpose(Range(sRNG).Value)
End If
bm_Safe_Exit:
Application.EnableEvents = True
End Sub
Note that the 'remembered' location is in memory only. Closing and reopening the workbook effectively 'zeroes' it. Do not make changes on Sheet2 unless you have freshly loaded values from Sheet1.