I have a group of cells B32 till R32 length and B32 to B51 breadth. I want to keep this block hidden at the start when the sheet is opened. I have named this block as 'Analysis'.
There is a button in the sheet. When the button is pressed, I want to unhide that block. I am new to Excel Vba. I would like to know the syntax/code for doing this operation.
Thanks in advance.
Ananda
You cant just hide an area like MattCrum has mentioned.
You have 3 choices as far as I am concerned
Now, just make sure you have something(data - not empty cells) in the Range 32:51 and your main sheet is either called Sheet1 or change Sheet1 in the code to suit your worksheets name
1)
in VBE ( Visual Basic Editor ) double click ThisWorkbook in the project explorer and paste this code
Private Sub Workbook_Open()
ThisWorkbook.Sheets("Sheet1").Rows(32 & ":" & 51).hidden = True
End Sub
Right click on the folder Modules and Insert a new Module, then paste this code
Sub unhide()
ThisWorkbook.Sheets("Sheet1").Rows(32 & ":" & 51).hidden = False
End Sub
Now, add a button on the spreadsheet, right click and assign macro called unhide to it.
Save changes and save your workbook as *.xlsm file
Notice when you open the workbook now, rows 32 to 51 are hidden. Clicking the button will unhide them.
2) You can change the font color to white to "hide" the contents.
Follow step 1, and replace
ThisWorkbook.Sheets("Sheet1").Rows(32 & ":" & 51).hidden = True
with this
ThisWorkbook.Sheets("Sheet1").Range("B32:R51").Font.Color = RGB(255, 255, 255)
and the code in the Module ( the unhide subroutine )with
ThisWorkbook.Sheets("Sheet1").Range("B32:R51").Font.Color = RGB(0, 0, 0)
Now, everything works similar to step 1 except your are "hiding"(changing) the font color instead of hiding rows. Not a great approach, but if it works for you then cool
3) Follow step 1 and replace the code under ThisWorkbook with
Option Explicit
Private Sub Workbook_Open()
Application.ScreenUpdating = False
Set ws = ThisWorkbook.Sheets("Sheet1")
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "hiddenSheet"
Set hs = ThisWorkbook.Sheets(Worksheets.Count)
hs.Visible = xlSheetHidden
ws.Range("B32:R51").Select
Selection.Copy
With hs
.Activate
.Range("B32").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End With
ws.Activate
ws.Rows(32 & ":" & 51).Delete
Application.ScreenUpdating = True
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call deleteHidden(Worksheets.Count)
End Sub
And the code in the Module1 with
Option Explicit
Public ws As Worksheet, hs As Worksheet
Sub unhide()
With hs
.Activate
.Rows("32:51").Select
Selection.Copy
End With
With ws
.Activate
.Rows("32:32").Select
Selection.Insert Shift:=xlDown
End With
End Sub
Sub deleteHidden(num&)
Application.DisplayAlerts = False
Worksheets(num).Delete
Application.DisplayAlerts = True
Set hs = Nothing
End Sub
Related
I found this code and it seems to be almost what I need. Is there a way to make it copy paste and delete the source instead of just showing a message box? I use worksheets that have links to another worksheet and if I use cut and paste I get a #REF error.
Thanks
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
ByVal Target As Excel.Range)
Public NewRange As String
Select Case Application.CutCopyMode
Case Is = False
'do nothing
Case Is = xlCopy
'do nothing
Case Is = xlCut
MsgBox "Please DO NOT Cut and Paste. Use Copy and Paste; then delete the source."
Application.CutCopyMode = False 'clear clipboard and cancel cut
End Select
End Sub
By using .PasteSpecial Paste:=xlPasteValues, you will paste the value and not the formula so the reference to the other sheet won't exist. This means you do not have to delete any references to other sheets.
For this to work properly, just update the "Sheet2" and "A1" references in Worksheets("Sheet2").Range("A1").PasteSpecial Paste:=xlPasteValues
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
Public NewRange As String
Select Case Application.CutCopyMode
Case Is = False
'do nothing
Case Is = xlCopy
'do nothing
Case Is = xlCut
MsgBox "Please DO NOT Cut and Paste. Use Copy and Paste; then delete the source."
Worksheets("Sheet2").Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False 'clear clipboard and cancel cut
End Select
End Sub
I have a short code that basically copies a selected row to another sheet, and deletes it from the first.
For some reason when I run this, it will run just fine, if I then select a different row, I get a pasteSpecial method of RangeClass failed error.
Sub Completed()
Dim trackerRow As Integer, compRow As Integer, answer As Integer
Application.ScreenUpdating = False
Application.CutCopyMode = False
trackerRow = ActiveCell.Row
compRow = Sheets("Completed").Cells(Rows.Count, "B").End(xlUp).Row
If trackerRow < 3 Then Exit Sub
If Cells(trackerRow, 2) = "" Then
MsgBox "This row is empty, please select the candidate you want to move.", vbExclamation, "Empty Row"
Else
answer = MsgBox("Are you sure you want to move " & Cells(trackerRow, 3).Value & "?", vbYesNo, "Confirm move?")
If answer = vbYes Then
'move row
Rows(trackerRow).EntireRow.Copy
With ThisWorkbook
With .Sheets("Completed")
.Activate
.Unprotect "HSBC2017"
.Cells(compRow + 1, 1).PasteSpecial xlPasteValues '**error line**.
.Protect "HSBC2017"
End With
With .Sheets("Tracker")
.Unprotect "HSBC2017"
.Rows(trackerRow).EntireRow.Delete
.Protect "HSBC2017"
.Activate
End With
End With
End If
End If
Application.ScreenUpdating = True
Application.CutCopyMode = False
End Sub
I can't work out why when I open the document it will work once, but you have to re-open to run a second time?
Each time you crash into a debug, you leave the destination Completed worksheet unprotected. Closing the workbook and reopening it leaves it unprotected.
Unprotecting an unprotected worksheet does not cancel the .CutCopyMode but unprotecting a protected worksheet does.
So in the first run-through with an unprotected destination worksheet there is something on the clipboard (and you have 'dancing ants' on the row-to-copy on the Tracker worksheet). Since everything runs through, the destination Completed worksheet is now protected and a second run-through will cancel the .CutCopyMode when the worksheet is unprotected. You can watch this by stepping through your code with F8 and see the 'dancing ants' disappear from the copy area on the Tracker worksheet as soon as you unprotect the Completed worksheet a second time.
Possible solutions (with a nod to comments from Peh and Rory):
Protect the worksheet with a password and UserInterfaceOnly.
with worksheets("Completed")
.Protect Password:="HSBC2017", UserInterfaceOnly:=True
end with
This only has to be done once and you can remove both the .Unprotect and .Protect code from future sub procedure operations. Only actions made manually by the user will be restricted.
Initiate the copy from the source worksheet after unprotecting the destination worksheet.
with worksheets("Completed")
.Unprotect Password:="HSBC2017"
worksheets("Tracker").Rows(trackerRow).EntireRow.Copy
.Cells(compRow + 1, 1).PasteSpecial xlPasteValues
.Protect Password:="HSBC2017"
end with
I have a checkbox which copies 5 cells of a address into 5 cells below if the project address is the same as the correspondence address.
here is the code I have:
Sub copy_address()
'
' copy_address Macro
'
Range("D5:G5").Select
Selection.Copy
Range("D11:G11").Select
ActiveSheet.Paste
Range("D6:G6").Select
Application.CutCopyMode = False
Selection.Copy
Range("D12:G12").Select
ActiveSheet.Paste
Range("D7:G7").Select
Application.CutCopyMode = False
Selection.Copy
Range("D13:G13").Select
ActiveSheet.Paste
Range("D8:G8").Select
Application.CutCopyMode = False
Selection.Copy
Range("D14:G14").Select
ActiveSheet.Paste
Range("F9:G9").Select
Application.CutCopyMode = False
Selection.Copy
Range("F15:G15").Select
ActiveSheet.Paste
SendKeys "{ESC}"
ActiveSheet.Range("D17").Select
End Sub
But what it does not do is undo the copy and paste when de-selected. In fact it carries out the copy and paste operation weather the box is ticked or un-ticked.
There is no mention in your code of any checkbox or its current state (aka .Value). Typically, after putting an ActiveX checkbox on the worksheet, you would use Design Mode and right-click it for the View Code command.
Once on the worksheet's code sheet in the VBE, modify the on-click handler to something like this.
Private Sub CheckBox1_Click()
If CheckBox1.Value Then
Range("D5:G8").Copy Destination:=Range("D11")
Range("F9:G9").Copy Destination:=Range("F15")
Else
Range("D11:G14").ClearContents
Range("F15:G15").ClearContents
End If
End Sub
Or you can try this if you want to keep the already assigned copy_address sub.
Sub copy_address()
'
' copy_address Macro
If ActiveSheet.Shapes("Check Box 1").ControlFormat.Value = 1 Then
ActiveSheet.Range("D5:G8").Copy Destination:=ActiveSheet.Range("D11:G14")
ActiveSheet.Range("F9:G9").Copy Destination:=ActiveSheet.Range("F15:G15")
Else
ActiveSheet.Range("D11:G15").ClearContents
End If
End Sub
Google hasn't given me quite what I want, I thought maybe SO might have the answer.
Instead of using a input box or user prompt that asks the user for info that will be stored in cell, I'm looking for some code to simulate clicking in the formula bar. I am currently using sendkeys method with "F2" to allow the user to enter info into the selected cell. It would be much easier to look into the formula bar, instead of a single cell in a sea of data.
Sub CopyTemplate()
'ActiveWorkbook.Save
Worksheets("HR-Cal").Activate
Dim rng As Range
Dim trng As Range
Dim tco As String
'Use the InputBox select row to insert copied cells
Set rng = Application.InputBox("select row to paste into", "Insert template location", Default:=ActiveCell.Address, Type:=8)
If rep = vbCancel Then
End If
startrow = rng.Row
' MsgBox "row =" & startrow
Range("AG2") = startrow
Application.ScreenUpdating = False
'copy template block
Range("C6").End(xlDown).Select
Range("AG1") = ActiveCell.Row
tco = "A5:AN" & Range("AG1")
Range(tco).Select
Selection.Copy
Range("A" & Range("AG2")).Activate
Selection.Insert Shift:=xlDown
Range("c100000").End(xlUp).Select
Selection.End(xlUp).Select
'select first value
Range("AG1:AG2").ClearContents
Application.ScreenUpdating = True
SendKeys "{F2}"
SendKeys "{BS}"
End Sub
when the codes runs this is what the user sees (col 2 col 2621)
I don't believe there is a single keypress to "activate" the formula bar. There is probably a way to do with multiple keypress events like <alt><tab><tab>...~nine years later and a couple of other keys~...<tab><tab>
The quicker and more direct way would be to turn off the "EditDirectlyInCell" setting:
Application.EditDirectlyInCell = False
This will bring the cursor to the formula bar when you sendkey F2
You could just turn this thing off on Workbook_Open() in the workbook's code:
Private Sub Workbook_Open()
Application.EditDirectlyInCell = False
End Sub
Perhaps on Workbook_BeforeClose() you could toggle that setting back on so you don't change their defaults:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.EditDirectlyInCell = True
End Sub
maybe this will help, i set the Application.EditDirectlyInCell to false, before entering cell editing, then call DoEvents before returning EditDirectlyInCell to true
Sub EditRange(ByVal Target As Range) 'so any change in target would not affect
'the caller's variable
Set Target = Target.Areas(1).Resize(1, 1) 'to make sure the target is single cell
Target.Worksheet.Activate 'to make sure the worksheet is active
Target.Activate 'activate the designated cell
Application.EditDirectlyInCell = False 'turn cell editing to false, any attempt _
to edit cell will be done in formula bar
SendKeys "{F2}" 'send F2 to start editing the cell
DoEvents 'make sure every command is fully executed
Application.EditDirectlyInCell = True 'return in cell editing to default value
End Sub
I am looking for a way to copy and paste text from one sheet to another when I tick a form control checkbox and to delete it when I uncheck it. At the moment the macro I have written does nothing, it doesn't come up with any errors it just doesn't work. What I have so far is:
Sub CheckBox3_Click()
Application.ScreenUpdating = False
If CheckBox3 = True Then
Sheets("Data Sheet").Activate
Range("B1").Select
Selection.Copy
Sheets("Sheet1").Select
Range("C1").Select
ActiveSheet.Paste
Application.ScreenUpdating = True
End If
If CheckBox3 = False Then
ActiveSheet.Range("C1").Select
Selection.Delete
End If
End Sub
Any help would be appreciated, thanks.
To overcome your Runtime error please change your code to
ActiveSheet.Range("B1").Select
and do the same for the target range C1
However, the much more elegant way is to get rid of Select and Activate all together by using VBA rather than "macro recording" ... work with Range objects which will simplify your code, you avoid messy screen jumps etc ...
Private Sub CheckBox1_Click()
Dim SrcRange As Range, TrgRange As Range
Set SrcRange = Worksheets("Data Sheet").[B1]
Set TrgRange = Worksheets("Sheet1").[C1]
If CheckBox1 Then
TrgRange = SrcRange
Else
TrgRange = ""
End If
End Sub