Ctrl+'Click' in Excel (and then offset the cells selected) - vba

I am having issues with writing a code in macro that will select multiple cells given those I've selected with Ctrl+ Click.
What I mean by this is, when I use Ctrl+ Click to select multiple cells, I want a macro to then select those cells plus the 5 cells to the right. However, I'm unsure what the code would be for Excel to know what cells I've selected from Ctrl+ Click.
Any suggestions/help would be greatly appreciated.

I think you would need to loop through the areas of the current selection to resize the selection if you selected multiple not continuous cells:
Option Explicit
Public Sub SelectPlusFiveColumns()
Dim FinalRange As Range
Dim Area As Range
For Each Area In Selection.Areas 'loop through areas
If FinalRange Is Nothing Then
Set FinalRange = Area.Resize(ColumnSize:=6)
Else
Set FinalRange = Union(FinalRange, Area.Resize(ColumnSize:=6)) 'resize each area and collect all areas in FinalRange
End If
Next Area
FinalRange.Select 'select all resized areas
End Sub
Note: Selection of overlapping areas will be combined to one area.

Related

Delete Picture From Cell

I have an excel file which contains 1000+ pictures. The pictures are embeded in Column J of each row.
I have a userform which allows user to update a picture. What I want is to delete the picture that is present in the cell before updating a new picture.
The code that I found and tried to use:
Dim curPic As Shape
For Each curPic In Worksheets("Sheet1").Shapes
If Not Application.Intersect(curPic.TopLeftCell, PicCell) Is Nothing Then
curPic.Delete
End If
Next curPic
The thing is since I have a 1000+ pic, it checks each and every picture and I get a "Not Responding" on the file.
Is there a way to search only in a particular cell since I know the cell location.
Assumption: the top left corner of the shape is within the particular cell.
This routine takes the cell you want to check - and then compares the topLeftCell-Address of each shape against the address of your cell.
If they match, shape will be deleted.
What is different to your code: I am comparing the addresses of the ranges and am not intersecting ranges. I suppose this will be faster but haven't tested it.
Furthermore the for-next-loop is exited as soon as the shape has been found. That means the routine will be faster for the first cells in your column but slower for the last cells.
Option Explicit
Sub deleteShapeInRange(rgCell As Range)
Dim shp As Shape
For Each shp In ActiveSheet.Shapes
If shp.TopLeftCell.Address = rgCell.Address Then
shp.Delete
exit for
End If
Next
End Sub

Excel Macro to Change Active Cell

I have created a form in Excel. Based on how the user completes one cell, changes which cells they fill out next (for example: When filling out A10 if they answer "X" they will move to B1, if they answer "Y" they will move to B3.)
In order to guide the user through the form, I created a complex set of conditional formatting rules which will "highlight"(background fill) the next cell they need to fill out. Once they complete the cell the formatting on that cell goes away and switches to the next cell.
I have the conditional formatting working exactly how I want. My question is: Is there a way to have the active cell follow this same path. Either by setting up the same formula rules that guide the conditional formatting or is there a way to have a macro auto set the active cell to the "highlighted" cell from the conditional formatting?
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
Const NEUTRAL = 16777215
Dim r As Range
For Each r In Cells.SpecialCells(xlCellTypeAllFormatConditions)
If r.DisplayFormat.Interior.Color <> NEUTRAL Then
r.Select
Exit For
End If
Next
End Sub
Note: you can edit NEUTRAL at the top if non-highlighted cells have a different color than pure white.
Note: this assumes that the cell you want the selection to jump to is the only cell on the sheet that is highlighted with conditional formatting.

Excel Macro Leaves Faux Selection lines?

I am using the following macro to "reset" a form. It leaves selection lines on the areas on Range(C3:C4) and Rows(32:37). However, they don't seem to be "real" selection lines. If I click somewhere else on the sheet, they don't disappear. If I make a selection that includes these areas and then click out, they do disappear. I want it to stop!
For Each cl In Range("B12:B16")
cl.Value = 0
Next cl
For Each rw In Range("B26:B37")
rw.EntireRow.ClearContents
Next rw
'Clear Vendor/PO
Range("C3:C4").ClearContents
First off you don't need to loop:
range("B12:B16").Formula = 0
Rows("26:37").Clearcontents
Range("C3:C4").ClearContents
Can you post a screenshot of the issue you are experiencing, i can't really understand that they stay sort of selected, is there some conditional formatting on them that marks them a grey colour if empty or something?

Excel VBA code (assigned to a button) to hide/unhide rows based on cell values across multiple sheets

this is my first post here and additionally I also have completely no knowledge on VBA whatsoever... so please excuse my ignorance ;-)
I'm working on a price list which has a quantity column. The same files has multiple worksheets with multiple currencies. What I need to achieve is to create two buttons on each sheet to hide / unhide all rows where the quantity cell equals zero.
So for example you want to select certain items from the list, so you enter the quantity into appropriate cells (quantity column) and press the button to hide all other rows for which the quantity equals zero.
Now, I found the code for this somewhere already, but it only works on a first sheet and when I copy the sheet (to create another currency) with the buttons and press the button it will still apply the changes (hide / unhide rows) to the first sheet. This code is below:
Public Sub HideRows()
Dim cell As Range
For Each cell In Range("BOQ")
cell.EntireRow.Hidden = (cell.Value = 0 And cell.Value <> "")
Next cell
End Sub
and to unhide:
Public Sub UnhideRows()
Dim cell As Range
For Each cell In Range("BOQ")
If (cell.Value = 0 And cell.Value <> "") Then cell.EntireRow.Hidden = False
Next cell
End Sub
I would be extremely grateful if anyone could propose a proper script to do that separately on multiple sheets. Also to avoid the issue when after a print preview the script runs like a 100 times slower.
Thanks in advance.
Range("BOQ") refers to a range on the first sheet.
So no matter which sheet is selected, the macro will affect that range on sheet 1.
To make the code flexible to the sheet you're on, consider changing it to something like:
Activesheet.Range("A2:A10")

VBA for clear value in specific range of cell and protected cell from being wash away formula

I have data from like A1:Z50 but I want to delete only A5:X50 using VBA
(I think it will be a lot faster than dragging the whole cell or using clickA5+shift+clickX50+delete).
How can I do this ?
And then, how to lock the cell to prevent it from getting fixed or cleared ?
You could define a macro containing the following code:
Sub DeleteA5X50()
Range("A5:X50").Select
Selection.ClearContents
end sub
Running the macro would select the range A5:x50 on the active worksheet and clear all the contents of the cells within that range.
To leave your formulas intact use the following instead:
Sub DeleteA5X50()
Range("A5:X50").Select
Selection.SpecialCells(xlCellTypeConstants, 23).Select
Selection.ClearContents
end sub
This will first select the overall range of cells you are interested in clearing the contents from and will then further limit the selection to only include cells which contain what excel considers to be 'Constants.'
You can do this manually in excel by selecting the range of cells, hitting 'f5' to bring up the 'Go To' dialog box and then clicking on the 'Special' button and choosing the 'Constants' option and clicking 'Ok'.
Try this
Sheets("your sheetname").range("A5:X50").Value = ""
You can also use
ActiveSheet.range
Not sure its faster with VBA - the fastest way to do it in the normal Excel programm would be:
Ctrl-G
A1:X50 Enter
Delete
Unless you have to do this very often, entering and then triggering the VBAcode is more effort.
And in case you only want to delete formulas or values, you can insert Ctrl-G, Alt-S to select Goto Special and here select Formulas or Values.