I am attempting to write a macro that will hide a sheet once the reference cell on a "Home" sheet is highlighted Green.
Attempted If Statement:
Dim Archive As String
Archive = ActiveSheet.Name
If Sheets("Home").Range("$A:$1).Value = Archive And Sheets("Home") _
.Range("$A:$1).Interior.Color = vbGreen Then
ActiveSheet.Hide
End If
The sheet and cell I am attempting to reference have the same name. The cell is Hyperlinked to the sheet. Both the Sheet and the cell with hyperlink are created by another macro.
I cannot identify a specific cell in the If statement because the cell will be different depending on the sheet I want to hide.
Hope this helps you out,
You can create a module and paste the following method over there.
Sub checkRef(ByVal shtName As String)
Dim archive As String
archive = shtName
If Sheets("Home").Range("A1").Value = archive And Sheets("Home").Range("A1").Interior.Color = vbGreen Then
ActiveSheet.Visible = False
Else
MsgBox "Activesheet not referenced to hide"
End If
End Sub
You can edit the above as per your need.
Then on the Worksheet_Activate event of the worksheet you want to hide call the above method as given below.
Private Sub Worksheet_Activate()
checkRef (ActiveSheet.Name)
End Sub
Then you can check the result by changing the value of Range("A1") of Home sheet to Name of the sheet you want to hide and also change the interior color to vbGreen.
You must be able to hide your sheet.
Related
Hope you can help me.
I want to copy and paste a cells value based on when you click a hyperlink on that cell.
So for example, I have a sheet called Form1, I want to click on an ID in column A it will then copy the value of that cell and paste it to B2 in sheet1 and then take me to sheet 1.
Currently I have a macro that allows me to click on an active cell and then press a button which then does what is mentioned above. I just think a hyperlink press would be more user friendly and would result in less errors.
Here is the code I have at the moment:
Sub Rectangle13_Click()
ActiveCell.Copy Destination:=Sheets(“Sheet1”).range(“B2”)
Worksheets(“Sheet1”).Activate
End Sub
Any help would be appreciated! Thank you
Worksheet FollowHyperLink
Copy this code to the sheet module of worksheet Form1. From there, run the second sub to convert the A column to hyperlinks.
Click away.
Option Explicit
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Not Intersect(Columns("A"), Target.Range) Is Nothing Then
Me.Parent.Worksheets("Sheet1").Range("B2").Value = Target.Range.Value
End If
End Sub
' Run this to create hyperlinks out of the values in column 'A'
' which will point to cell 'B2' in 'Sheet1'. You can then reformat
' the cells (remove underline, change font color, ...).
Private Sub CreateHyperlinks()
Dim lRow As Long: lRow = Range("A" & Rows.Count).End(xlUp).Row
Dim cell As Range
For Each cell In Range("A2:A" & lRow).Cells
cell.Hyperlinks.Add cell, "", "Sheet1!B2", , CStr(cell.Value)
Next cell
End Sub
It is inconvenient to use Hyperlik, I think. If you try changing the cell value, you cannot simple click it and write something... But if you want that, you can create such a hyperlink in the next way:
Sub testAddHyperlink()
Dim DestSh As Worksheet
Set DestSh = Sheets("Sheet1") 'you can use any sheet name here
ActiveSheet.Hyperlinks.Add Range("A1"), Address:="", SubAddress:="'" & DestSh.name & "'" & "!A1"
'it will keep the existing cell value
End Sub
Then, please copy the next event code in the sheet code module (where the hyperlink exists):
Option Explicit
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim rngCopy As Range, shDest As Worksheet
Set shDest = Sheets("Sheet1") 'you may use here any sheet name
shDest.Range("B2").value = Target.Parent.Value 'the sheet where the hyperlink targets, is already activated...
End Sub
If you want to use the BeforeDoubleClick approach, paste this code into your Form1 worksheet object in the VBA editor ...
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Cells.Count = 1 And Target.Cells(1, 1).Column = 1 Then
Cancel = True
With ThisWorkbook.Worksheets("Sheet1")
.Range("B2") = Target.Value
.Activate
End With
End If
End Sub
... naturally, this is a base example and you may need to modify it accordingly. For example, when you double click, you may want to ignore the first row if it's a header and not invoke the core parts of the logic.
That will then do what you want.
Any ideas how I can carry out the following using VBA:
'Input a Value INTO Cell = [P4]'
'in Worksheet Titled = [P3] '
'WHERE ROW = [M4] AND Column = [N4]'
Basically when I click a button I want the formula that is in [Cell:P4] to be populated into another worksheet based on that worksheet having the title of [Cell:P3] and only in the Row and column where both the row has a number as stated in [Cell:M4] and the column has a number as stated in [Cell:N4].
So first you need to know how to hook up your macro to a button. See this official documentation or this one from excelcampus.com.
Basically, create a rectangle or any form you want your button to have, then right-click the form and click on "assign macro...". There are other more complicated ways, but this should be enough.
So now about the macro itself I will give you a few hints.
You want to copy a formula into another cell. The basic approach in VBA:
Sub copyFormula()
Cells(1, 3).Formula = Cells(1, 2).Formula 'this will copy the formula in B1 to C1
End Sub
But we want to make this code a little safer. Lets write code that identifies the workbook and the worksheets that are involved.
Option Explicit
Sub copyFormula()
Dim wb As Workbook
Set wb = ThisWorkbook
Dim ws_source As Worksheet
Dim ws_target As Worksheet
Set ws_source = wb.Worksheets("Sheet1") 'replace Sheet1 with name of your sheet
Set ws_target = wb.Worksheets("Sheet2") 'replace Sheet2 with name of your sheet
ws_target.Cells(1, 3).Formula = ws_source.Cells(1, 2).Formula
End Sub
At this point you will need to worry about references in your formula (I am talking about the excel formula now, not the VBA code). Hint: Try using $ signs to lock cell references (not an immediate concern for this method) and also reference the sheet of the cell you are pointing to like this: "=Sheet2!A1"
Okay, now lets modify the code a little more. You want to address the sheet that has a variable name, namely the name that is written in Cell P3 (I assume of the source worksheet). We can do this.
All we have to do is read the value of P3 and then replace our static "Sheet2" in the target worksheet definition with said variable.
Dim targetName As String
targetName = ws_source.Cells(3, "P").Value
Set ws_target = wb.Worksheets(targetName)
Try writing an invalid name into cell P3 and you will see why this is dangerous business as it is. I will leave it to you to take precautions that the entered value in P3 will not break your code :)
I do not really understand what you mean with the last part talking about rows and columns. Show us what you have tried, then we can help improve your code.
Was this what you had in mind? The function must be installed in the code sheet of the tab on which you have the command button.
Private Sub CommandButton1_Click()
' 027
On Error Resume Next
With Worksheets(Cells(3, "P").Value)
.Cells(CLng(Cells(4, "M").Value), CLng(Cells(4, "N").Value)) _
.Formula = Cells(4, "P").Formula
End With
If Err Then
MsgBox "There is an error in the specs." & vbCr & _
"Check the tab's name.", vbInformation, _
"Execution suspended"
Cells(3, "P").Select
End If
End Sub
I have an Excel workbook with a table of contents tab that has hyperlinks to other tabs and different locations on those tabs i.e. names and regions.
My question is, how can I get it to where no matter what hyperlink I click on, it will go to that tab and the cell will always appear in the upper left corner instead of the lower right. Excel by default makes the target cell upper left in the active window, but this is not the case when the target cell is in the active window.
I am trying to make it easier for the end user so they will not have to keep scrolling to that section to get it in view. Thank you in advance.
Slight Addition to: Excel-Hyperlink-Jump-to-cell-and-scroll-window
Place this code in your Table of Contents sheet. Any Hyperlink on your ToC sheet will go to the tab, and scroll to the row and column of the hyperlinks destination.
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
ActiveWindow.ScrollRow = ActiveCell.Row
ActiveWindow.ScrollColumn = ActiveCell.Column
End Sub
When the user clicks a link and it sends them to a sheet, the cell that is linked to becomes the ActiveCell. If you include this in every cell that will be linked to, it will auto-scroll to the desired cell when the link is clicked.
Private Sub Worksheet_Activate()
Application.Goto ActiveCell, True
End Sub
The only caveat would be that for occasions where a user might instead click on a cell manually with no intention of scrolling to that cell, it will do so next time the sheet is activated
If you understand how to use VBA with events, this should do what you want.
It includes a defense against sheets with spaces in them.
Sub JumpToPostion()
Dim WS As Worksheet, CellAddress As String
Set WS = Sheet2
CellAddress = "z100"
Application.Goto Range(FixSheet(WS.Name) & "!" & CellAddress), Scroll:=True
End Sub
Private Function FixSheet(txt As String) As String
If InStr(1, txt, " ") > 0 Then
FixSheet = "'" & txt & "'"
Else
FixSheet = txt
End If
End Function
If you put this in the sheet activate event R40 will be the top left cell so perhaps you can work with that?
Private Sub Worksheet_Activate()
Application.Goto Range("R40"), True
End Sub
To take a stage further, this would work if your hyperlinked cells all contained sheet name ! cell address , e.g. Sheet2!R40. The code goes in the sheet module.
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Dim v
v = Split(Replace(Target.SubAddress, "'", ""), "!")
Application.Goto Sheets(v(0)).Range(v(1)), True
End Sub
Insert -> Add HyperLink -> Same document (you can specified any cell)
If you mean Anchor in WorkSheet :-)
I have a Function that is attempting to change the current active sheet ("Sheet1") to (Sheet2) and then assign a value to a cell in Sheet2.
However the ActiveSheet.name doesn't change and the assignment fails. see the code snippet. The function is called from sheet1.
Dim oldwksheet as String
oldwksheet = ActiveSheet.Name 'value is Sheet1
Sheets("Sheet2").Activate
Sheets("Sheet2").Select
oldwksheet = ActiveSheet.Name 'value still is Sheet1"
Sheets("Sheet2").Range("F2") = Now() 'this fails.
end function
Why? What am I doing wrong.
You need a sub and not a function.
A function can only return a value to the cell in which it resides. It can't select or activate worksheets or deposit values in arbitrary cells.
I couldn't say why, but you could use a Function
assuming your function's named after "MyFunction" then place in relevant worksheet code pane the following code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 Then If Left(Target.Formula, 12) = "=MyFunction(" Then Sheets("Sheet2").Range("F2") = Now()
End Sub
this way, whenever you type =MyFunction() in any cell of the relevant worksheet, the current date and time is being placed in cell F2 of Sheet2
Is it possible to let Excel automatically select the first empty cell in column A, whenever I open the document?
I have got the following to find the the first empty line:
ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Value + 1.Count, 1).End(xlUp).Value + 1
In order to get Excel to select the first empty cell in column A when you open your workbook, you need to place the following code in the ThisWorkbook module.
Private Sub Workbook_Open()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Cells(ws.Rows.Count, 1).End(xlUp).Offset(1, 0).Select
End Sub
This will select the first empty cell in the ActiveSheet. If you got multiple sheets in your workbook and you want to select the first empty row in a specific sheet, say Sheet1, you should change the second line of code to:
Set ws = ActiveWorkbook.Sheets("Sheet1")
You can do that.
You need write VBA(macro) program to realize.
Code you need is as follow
Private Sub Workbook_Open()
ActiveWindow.Range("A65536").End(xlUp).Offset(1,0).Select
End Sub
Meaning of code is:
"Private Sub Workbook_Open()" is predefined name subroutine which will be executed when the workbook be opened.
"ActiveWindow.Range("A65536").End(xlUp)" will find last cell with data in A column ("last cell")
"ActiveWindow.Range("A65536").End(xlUp).Offset(1,0)" will move to cell next to "last cell", that will be first blank cell.
ActiveWindow.Range("A65536").End(xlUp).Offset(1,0).Select will select tha first blank cell.
I assumed that you use Excel 2003 or older, OR number of rows with data in your worksheet is less than 65536.
If you use Excel 2007 or newer and you have rows with data in your worksheet more than 65536, please modify 65536 to the value large enough to cover rows in your worksheet.