Home |
Search |
Today's Posts |
#12
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Glad the problem is so simple. Just moving one line of code should fix
problem I'm also initializing the count just in case of problems. I also made a changge so the address printed in the msgbox contains all the addresses. Sub test() Dim cMarks As Shape Dim sCells As Range Dim cCount As Integer cCount = 0 address_string = "" For Each myRange In Selection.Areas if address_string = "" then address_string = myRange.address else address_string = address_string & "," & _ myRange.address end if myRows = myRange.Rows.Count LastRow = myRange.Row + myRows - 1 myCols = myRange.Columns.Count LastCol = myRange.Column + myCols - 1 Set LastCell = Cells(LastRow, LastCol) RLeft = myRange.Left RTop = myRange.Top RRight = LastCell.Left + LastCell.Width RBottom = LastCell.Top + LastCell.Width For Each cMarks In ActiveSheet.Shapes If cMarks.Type = 13 Then If cMarks.Top = RTop And _ cMarks.Top <= RBottom And _ cMarks.Left = RLeft And _ cMarks.Left <= RRight Then cCount = cCount + 1 cMarks.Delete End If End If Next cMarks Next myRange MsgBox "You have removed " & cCount & _ " check marks in highlighted cells " _ & address_string & "' of the Worksheet '" & _ ActiveSheet.Name & "'." End Sub "Please Help" wrote: Joel, This new code removes all the shapes in the cells selected. However, instead of removing all the shapes and showing the message all at once, removal is done based on each group of cells selected. For example, if I have selected 4 groups of cells (using Ctrl key), the macro will remove 4 times and show message four times for each group of cells. In addition, the counter (cCount) within the message is not counting correctly. It seems double after removing 2nd group of shapes (cells). Please help. Thank you very much for your help and patience. "Joel" wrote: Here is a simple modification to handle all seleted areas. Sub test() Dim cMarks As Shape Dim sCells As Range Dim cCount As Integer For Each myRange In Selection.Areas myRows = myRange.Rows.Count LastRow = myRange.Row + myRows - 1 myCols = myRange.Columns.Count LastCol = myRange.Column + myCols - 1 Set LastCell = Cells(LastRow, LastCol) RLeft = myRange.Left RTop = myRange.Top RRight = LastCell.Left + LastCell.Width RBottom = LastCell.Top + LastCell.Width For Each cMarks In ActiveSheet.Shapes If cMarks.Type = 13 Then If cMarks.Top = RTop And _ cMarks.Top <= RBottom And _ cMarks.Left = RLeft And _ cMarks.Left <= RRight Then cCount = cCount + 1 cMarks.Delete End If End If Next cMarks MsgBox "You have removed " & cCount & _ " check marks in highlighted cells " _ & myRange.Address & "' of the Worksheet '" & _ ActiveSheet.Name & "'." Next myRange End Sub "Please Help" wrote: Joel, Thank you again for your patience and your continuing help. I think we are almost there. When I try your latest code, I got no error message, and it removed the shapes. However, it does not remove all the shapes. For example, if I selected the cells B4:E9, B17, E18 and H11. It removed the shapes only in the cells B4:E9. There were total of 9 shapes, and cells B4:B9 has 6 shapes. I also tried it by selecting 2 large range (F4:I11 and D16:F22) of cells (instead of one large range and individual cells like the first example), and it deleted the shapes in the first range. Based on the testing, the macro only removes the shapes in the first cell or first group of cells. The shapes of the rest of selected cells (selecting using the Ctrl key) do not get removed. Thanks. "Joel" wrote: I modified the code to work off selected cells Sub test() Dim cMarks As Shape Dim sCells As Range Dim cCount As Integer Set myRange = Selection myRows = myRange.Rows.Count LastRow = myRange.Row + myRows - 1 myCols = myRange.Columns.Count Lastcol = myRange.Column + myCols - 1 Set LastCell = Cells(LastRow, Lastcol) RLeft = myRange.Left RTop = myRange.Top RRight = LastCell.Left + LastCell.Width RBottom = LastCell.Top + LastCell.Width For Each cMarks In ActiveSheet.Shapes If cMarks.Type = 13 Then If cMarks.Top = RTop And _ cMarks.Top <= RBottom And _ cMarks.Left = RLeft And _ cMarks.Left <= RRight Then cCount = cCount + 1 cMarks.Delete End If End If Next cMarks MsgBox "You have removed " & cCount & " check marks in highlighted cells " _ & myRange.Address & "' of the Worksheet '" & ActiveSheet.Name & "'." End Sub "Please Help" wrote: Joel, The range is pretty much any cell on the sheet, not just A1 and E5. Meaning, we could have shapes in cells A1, E5, G100:I110, etc. From your post, you asked me to change the MsgBox. Does it mean the new one will not reference the cells where we are removing the shapes? Is it possible to reference them in the Msgbox? Thanks. "Joel" wrote: sCells is not defined. You are going to have to set the range by changint the A1 and E5. from MsgBox "You have removed " & cCount & " check marks in highlighted cells " '" & sCells.Name & "' of the Worksheet '" & ActiveSheet.Name & "'." to MsgBox "You have removed " & cCount & " check marks in highlighted cells " & "' of the Worksheet '" & ActiveSheet.Name & "'." "Please Help" wrote: Joel, Thanks for your patience and your continuing help. I changed the code to ActiveSheet as you indicated below, and I am still getting the same error message. However, this time it pointed me to the MsgBox: MsgBox "You have removed " & cCount & " check marks in highlighted cells " '" & sCells.Name & "' of the Worksheet '" & ActiveSheet.Name & "'." Please help. Thanks. "Joel" wrote: from For Each cMarks In sCells.Shapes to For Each cMarks In ActiveSheet.Shapes "Please Help" wrote: Joel, Thanks for the code. When I run your code, I got the following error: Run-time error '91': Object variable or With block variable not set And it pointed me to the line "For Each cMarks In sCells.Shapes". I am just curious something. How does the code know the range for "sCells" because there is no reference for range for sCells? Thanks. "Joel" wrote: I forgot to post my code. All shapes have four location/size parameters Top, Left, Height, Width Sub test() Dim cMarks As Shape Dim sCells As Range Dim cCount As Integer RLeft = Range("A1").Left RTop = Range("A1").Top Rright = Range("E5").Left + Range("E5").Width RBottom = Range("E5").Top + Range("E5").Width For Each cMarks In sCells.Shapes If cMarks.Type = 13 Then If cMarks.Top = RTop And _ cMarks.Top <= RBottom And _ cMarks.Left = RLeft And _ cMarks.Left <= Rright Then cCount = cCount + 1 cMarks.Delete End If End If Next cMarks MsgBox "You have removed " & cCount & " check marks in highlighted cells " '" & sCells.Name & "' of the Worksheet '" & ActiveSheet.Name & "'." End Sub "Please Help" wrote: Joel, Thanks for your response. For what I need, I don't think we can use the shape measurement because we use many different shapes. However, the shapes are type 13 (msoLinkedPicture). Anymore idea? Thanks. "Joel" wrote: the problem is shapes do not sit in cell they sit ontop of cells. they also don't have a row or column. Both cells and shapes do have left, top, height, width which are pixel measurments. You can write code like the sample below to compartre shapes with cells. Note: The code below doesn't test the right side or botttom of the shape to see if it is inside the range. Wasn't sure if this is a requirement. The code only tests the left side of the shape and the top of the shape. "Please Help" wrote: Good morning, Please help with my code below. Basically, my code will remove the shapes in the cells selected (instead of all the shapes in the active worksheet) of the active worksheet, and it is not work. For example, if I select cells A1, B1, and C5:G10 and when I run the macro, the macro will only remove the shapes in those cells. Please note the cells selected will be done by users and may not be the same as referenced above. Thanks. Dim cMarks As Shape Dim sCells as Range Dim cCount as Integer sCells.Select For Each sCells In ActiveSheet For Each cMarks In sCells.Shapes If cMarks.Type = 13 Then cCount = cCount + 1 cMarks.Delete End If Next cMarks Next sCells MsgBox "You have removed " & cCount & " check marks in highlighted cells '" & sCells.Name & "' of the Worksheet '" & ActiveSheet.Name & "'." |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
split post code (zip code) out of cell that includes full address | Excel Discussion (Misc queries) | |||
Shorten code to apply to all sheets except a few, instead of individually naming them, and later adding to code. | Excel Programming | |||
Protect Sheet with code, but then code will not Paste error. How do i get around this. Please read for explainations.... | Excel Programming | |||
Modification in the CODE to HIDE rows and columns that start with ZERO (code given) | Excel Programming | |||
Excel code convert to Access code - Concat & eliminate duplicates | Excel Programming |