Preventing selection of previous cell in Excel after changing tabs - excel

and thanks for the answers
I am designing a LogBook using excel VBA and this is the problem:
I work on Sheet2 and let's say I left the editing on cell F2 and go to Sheet1 to do other stuff. After I come back to Sheet 2 the selection remains on cell F2 but I don't want this. Actually, I don't want any cell to be selected. Since the selection of a cell marks the borders of that cell it does not look good on my design. I want to show A1 to Z40 without any cell selected. Hope I could describe it.
Any suggestiions?

Change Selection When Returning to Tab
Copy the following code to the Sheet2 code module.
Option Explicit
Private Sub Worksheet_Activate()
' You have to activate (select) something:
Cells(Rows.Count, Columns.Count).Activate ' last cell on worksheet
' or e.g.:
'Range("AA1").Activate
With ActiveWindow
.ScrollColumn = 1
.ScrollRow = 1
End With
End Sub

Related

How to highlight active row in excel and then return to base background color in VBA

How do you highlight an active row in excel in VBA. and then when another row is selected, return that row to base background color, and highlight the new row.
Also how to clear all rows highlighted, using a clear button on the user form.
so there are tow question here, one to high light and unhighlight active rows, and the other to just clear all high lights by pressing a clear button on the form.
I know I can highlight a row using Ret.EntireRow.Interior.ColorIndex = 6 but i cant find code to unhighlight.
Thanks for your help.
You can use your 'clear all' functionality before changing the color of the row of the cell that you navigated to.
Open the VB Editor and right click --> view code on the worksheet that you want the row highlighting to take place.
Paste in this code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Me.Range("A1:XFD1048576").Interior.ColorIndex = 0
Target.EntireRow.Interior.ColorIndex = 6
End Sub
This code operates as follows: whenever a user changes his or her selected cell(s) on the sheet, the code will first clear the existing highlighting away in the entire sheet and then will apply new highlighting to the row of the target cell the user has moved to.
This line of code:
Worksheets("YourSheetName").Range("A1:XFD1048576").Interior.ColorIndex = 0
Will clear the colors from all cells in the worksheet.
You may want to limit the Range("A1:XFD1048576") to the usable range on your workbook as this will increase performance. On my machine I see a very subtle, but still noticeable, delay in the colors when I move the cells (because I am clearing all cells in the sheet instead of just the ones I want). If you do this, you probably wouldn't want to use the .EntireRow attribute, instead you would have to enumerate how far along the workbook you want the row to be highlighted.
Update
Try this code below, which eliminates the need to clear the entire worksheet. I used .ColorIndex=xlNone instead of setting it to 0 which should preserve your table formatting. I tested in Excel 2010 and I formatted some data as a table, it highlights the correct row and unhighlights the other row as well as leaving the table formatting in tact.
Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
Static rr
If rr <> "" Then
With Rows(rr).Interior
.ColorIndex = xlNone
End With
End If
r = Selection.Row
rr = r
With Rows(r).Interior
.ColorIndex = 6
.Pattern = xlSolid
End With
End Sub
The trick is using Static. This allows the variable to continue to exist after termination of the procedure, so it remembers the last row it highlighted and then performs the un-highlight action accordingly.
The procedure first checks to see that rr is set, if it is not then it moves on, if it is then rr represents the row that was previously highlighted.
This can be done without changing the base background color,
In 2 steps,
Set up a conditional formatting rule that highlights an entire row if a certain formula is true.
In the formula field, enter this formula:
=OR(CELL("row")=CELL("row",A1))
Write a macro that recalculates the selected cell(s) when a new selection is made.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Calculate
End Sub
Hit Alt + F11 to get back to Excel and you'll have the active cell's row highlighted with the format you chose, without changing the base colors of the cells.
For detailed explanation visit,
highlighted the entire row of the active cell.

How do I change the style of all rows in the selected range?

I want to create a macro that will highlight all rows of a selected range of cells. E.g. If I select cells A1 and B3 I want the macro to highlight rows 1 and 3. Currently I have the following macro which is able to highlight a row from a single cell, but I don't know how to expand it to highlight the rows of all selected cells:
Sub Macro1()
ActiveCell.EntireRow.Style = "Good"
End Sub
I would suggest this as your sub.
Sub Macro1()
If TypeName(Selection) = "Range" Then
Selection.EntireRow.Style = "Good"
End If
End Sub
ActiveCell is only going to return the top left cell within the Selection. See this. Also, thanks to PatrickK for the suggestion about checking the typename, I am embarrassed to say I was unaware of the TypeName function before now.

Unmerge cells and distribute contents in Excel (for mac) 2011

I have a spreadsheet with a large amount of data in. About half the cells are merged horizontally with other cells and contain names e.g. John Doe.
Does anyone know how to write a macro to unmerge the cells while distributing the value of the cell to all the cells that were previously merged?
Cheers
Jack
EDIT: The reason I am doing this is to check to see if two adjacent cells are equal i.e. is A1 = A2. But I run into problems when either cell is merged. If anyone knows a way around this problem without separating the cells and copying the data that would be even better!
The idea I provide below is tested for Excel 2010 VBA Win7. However, being not sure I hope it should work as for Mac, too (as this is rather set of standard properties and methods of Range object). If this doesn't work please let me know to delete my answer.
This simple code will work for selected area however it's quite easy to change it to any other range. Some other comment inside the code below.
Sub Unmerging_Selection()
Dim tmpAddress As String
Dim Cell As Range
'change Selection below for any other range to process
For Each Cell In Selection
'check if cell is merged
If Cell.MergeCells Then
'if so- check the range merged
tmpAddress = Cell.MergeArea.Address
'umnerge
Cell.UnMerge
'put the value of the cell to
Range(tmpAddress) = Cell
End If
Next
End sub
And the picture presenting before and after result:
I was able to get the solution from KazJaw to work on a mac with one edit, changing Cell.UnMerge to
ActiveSheet.UsedRange.MergeCells = False, as provided by Ron Debruin here: http://www.rondebruin.nl/mac/mac027.htm.
Sub Unmerging_Selection()
Dim tmpAddress As String
Dim Cell As Range
'change Selection below for any other range to process
For Each Cell In Selection
'check if cell is merged
If Cell.MergeCells Then
'if so- check the range merged
tmpAddress = Cell.MergeArea.Address
'umnerge
ActiveSheet.UsedRange.MergeCells = False
'put the value of the cell to
Range(tmpAddress) = Cell
End If
Next
End sub

VBA and Excel columns

In the Excel-sheet for the A column write procedure, which with MsgBox function returns the column, A1 drawers from the first address of an empty drawer?
This is not an answer. I am trying to help you ask your question. Do not worry if your English is poor. If you say enough we can guess what you mean. One sentence is not enough.
Below I have tried to use short, simple sentences. I hope you can understand me. Are my guesses correct? If not, I hope this helps you write a clearer question.
Is this homework? A first exercise with Excel? Have you translated it with a dictionary? "Drawer" is an English word but there are no drawers in Excel. Do you mean "cell"? A1 is a cell. B5 is a cell. You put things in a drawer. You put things in a cell. This is the sort of mistake English - Xxxxxx dictionaries make.
Create and open a new Excel workbook. Click Alt+F11. On the right at the top you will see a grey area. On the right at the bottom you will see a white area labelled "Immediate". Down the left you will see something like:
VBAProject (Your excel file)
Microsoft Excel Objects
Sheet1 (Sheet1)
Sheet2 (Sheet2)
Sheet3 (Sheet3)
ThisWorkbook
If you left-click Sheet1 and then right-click, you will see a menu something like:
View Code
View Object
--------------------
VBProject Properties
: :
: :
Click View Code. The grey area will turn white. Here you can enter code for Sheet 1. Is this what you mean by "in the Excel-sheet"?
I am not going to put code against Sheet1. I am going to use a Module.
Go to the Toolbar and click Insert. In the Menu, click Module.
The window down the left will now look like:
VBAProject (Your excel file)
Microsoft Excel Objects
Sheet1 (Sheet1)
Sheet2 (Sheet2)
Sheet3 (Sheet3)
ThisWorkbook
Modules
Module1
"Module1" will be grey because it is selected.
I think you have been asked to: "Find the first empty cell in Row 1 and display its column number with MsgBox." There are many ways of doing this. Which is the simpliest? I do not know. Here are two ways:
Option Explicit
' Both these routines work on the ActiveSheet. That is, the worksheet
' you can see when you switch to Excel. If this code had been against
' Sheet1, it would have worked on Sheet 1 even if another sheet had been
' active.
Sub FindFirstEmpty1()
' This routine uses Offset. Range("A1").Offset(RowOffset, ColOffset) says
' I want to look at the cell which is RowOffset rows down from A1 and
' ColOffset columns right from A1.
Dim ColOffset As Long
ColOffset = 0
Do While True
If Range("A1").Offset(0, ColOffset).Value = "" Then
Call MsgBox("The first empty column is " & ColOffset + 1, vbOKOnly)
Exit Sub
End If
ColOffset = ColOffset + 1
Loop
End Sub
Sub FindFirstEmpty2()
' This routine uses Cells(Row, Column). The columns are numbered: A=1, B=2,
' C=3 and so on. Cells(Row, Column) lets me look at any cell in the
' worksheet.
Dim ColCrnt As Long
ColCrnt = 1
Do While True
If Cells(1, ColCrnt).Value = "" Then
Call MsgBox("The first empty column is " & ColCrnt, vbOKOnly)
Exit Sub
End If
ColCrnt = ColCrnt + 1
Loop
End Sub

Transfer values from one sheet of a workbook to another by clicking on the cell

I have 5,000 part numbers contained on one sheet of an Excel workbook (Part Numbers), ranging from cell A1:A5000. I want to find a way to click on any one of those part numbers and have it automatically populate into cell D7 on another sheet of the same workbook (Price Sheet). What is the easiest way to accomplish this task?
To do it that way, you will have to write VBA code to catch every SheetSelectionChange event to see if the new selection range is in your cells A1:A5000. And then if it is, execute the VBA code to fill OtherSheet!D7.
If I recall correctly, the VBA code to do this would look something like this:
Private Sub WorkSheet_SelectionChange(ByVal Target As Range)
'Check if it is in the range A1:A5000
If Target.Column = 1 And Target.Row <= 5000 Then
'get the target worksheet
Dim TargetWS As Worksheet
Set TargetWS = Sheets("OtherSheetsName")
'copy the value of the just selected cell to D7 on the target WS
TargetWS.Cells(7, 4).Value = Target.Value
End If
End Sub
(Oops, forgot about the need for "SET" in VBA.)
You can do this without VBA:
Select the partnumbers A1:A5000 and type PartNumbers in the Name Box (to the left of the formula bar) and press carriage return (OartNumbers should now be visible in the Name Box whenever you select a1:a5000
Now go to cell D7 on Price Sheet, and use Data Validation-->List and enter =PartNumbers in the Source box
Now you can select any of the 5000 part numbers from the dropdown in cell D7

Resources