How to run VBA macros in all sheets - vba

I am new to VBA, and I have a sheet which has VBA macros assigned to shapes. It works fine on the same sheet, but when I copy and paste the shapes in the next sheet or create a new shape and assign the same macro it does not work. Kindly help me. Below is the code.
ActiveSheet.Shapes("CTSnext").Fill.ForeColor.SchemeColor = Black
Thanks for your response !!
However, Let me explain the issue.
In Sheet 1 I have 4 shapes Named
1) Cancelling the service
2) Next
3) Not interested
4) Reset
And all 3 shapes (2,3,4) will be in forecolor as white so it will be invisible.
When user clicks on "Cancelling the service" "Next" will appear (Meaning the fore color will change to black). And when "Next" is clicked "Not interested" will appear (Meaning the fore color will change to black). And when user clicks on "Reset" both "Next" & "Not interested" will turn into invisible (Meaning the fore color will change to white). Until now everything works fine.
Now i want to have a shape in Sheet 2 which says "Reset". So when user clicks on it the same function (Meaning the VBA of the "Reset" button in Sheet 1) should happen.
Below are the code.
For Cancel
ActiveSheet.Shapes("Next").Fill.ForeColor.SchemeColor = Black
For Next
ActiveSheet.Shapes("Notinterested").Fill.ForeColor.SchemeColor = Black
For Reset
ActiveSheet.Shapes("Next").Fill.ForeColor.SchemeColor = 1
ActiveSheet.Shapes("Notinterested").Fill.ForeColor.SchemeColor = 1
I hope i made myself clear, also i don't know to how to attach the excel file to this page so it will be helpful for you all to understand my need.
Thanks is advance.

Your line of code refers to one specific named shape (CTSnext). You replace that by shape number if you have only one in the sheet Shapes(0).
If you have several shapes on the current sheet and you want the macro to run on all shape, you can use a loop like
for each shp in ActiveSheet.Shapes
shp.Fill.ForeColor.SchemeColor = Black
Next shp

Following on from iDevlop's answer to loop through each Worksheet as well as each shape.
Dim wks as Worksheet
For Each wks in Me.Worksheets
For Each shp in wks.Shapes
shp.Fill.ForeColor.SchemeColor = Black
Next shp
Next wks

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

Disable a cell in Excel using VBA

I am trying to do few calculations and update the result of the same in a cell upon clicking the Calculate button (Active X button).
When the Excel sheet is opened, I want few of the cells to be disabled and greyed out. To implement the same, I have written the below lines of code under the Workbook_Open() Sub. All the conditions written under the sub are working fine, except, the statement that I have written to disable the cell (written the locked function to disable the cell).
After some surfing, I came to understand that sheet needs to be protected if a cell needs to be disabled/locked. So, I added an extra line in the code to protect the sheet. However, this stopped the result to be updated in a cell.
Upon clicking the calculate button, excel states that the "Sheet is protected". So, is there anyway to enable/ disable the cell using VB Script and without protecting the sheet please?
Private Sub Workbook_Open()
Dim b1 As Variant
Set b1 = Sheets("Calculation Tool").CommandButton22
b1.Enabled = False
Range("B4:C4") = ""
Range("E4:O4") = ""
Range("E9:F9") = ""
'This is the code written to disable the cells N4 and O4
Sheets("Calculation Tool").Range("N4:O4").Locked = True
Sheets("Calculation Tool").Protect
End Sub
You cannot "disable" a worksheet cell per-se; it's not like a textbox control.
You can, however:
set the cell's fill color to gray (Ctrl+1 to enter Format Cells > Fill tab)
lock the cell to prevent changes (Ctrl+1 to enter Format Cells > Protection tab)
protect the worksheet (Review tab > Protect Sheet) so the cell locking takes effect.
If you need to change the locked cell you can programmatically unprotect the worksheet, make the change, and then re-protect it. Also note that there are several options available when protecting the worksheet.
Alternatively, you could instead use text boxes, and then disable/lock it like you would other controls.
See Also: Lock or unlock specific areas of a protected worksheet
(Based on your "greyed-out" description, I believe you were thinking of textboxes on Microsoft Access Forms.)
try this code for a range:
Range("A1:B2").Locked = True

Excel full screen mode cant write to cell

I have a problem with excel in full screen mode. When I chose another sheet (via macro) and go back to previous sheet (via macro) I cant write something to cell selected cell. Any advice?
My code for fullscreen:
Private Sub Workbook_Open()
Application.DisplayFullScreen = True
Application.CommandBars("Full Screen").Visible = False
End Sub
My macro code for select sheet:
Private Sub skok()
Sheets("Sheet 2").Select
End Sub
I can testify that this problem is happening to my workbook also!
To improve visibility, I make the workbook go fullscreen with the DisplayFullscreen=True in the same sub.
Later, the user can move to Sheets 2 with macro in a command button. On sheet 2 there is a button "Save & Return" which runs a macro like
Sheets("Sheet1").select
But when we return to Sheet1 the cells seem to freeze. But the command buttons still work.The situation can be 'unlocked' when I randomly select another sheet below on the sheet tab. Then returning to Sheet1 I can then enter in the cells!
One strange thing is that although the sheet seem freeze, I can still copy & paste text in a cell using the Ctrl+C & Ctrl+V on keyboard.
I have the same problem, and it is an Excel bug! We can still copy/paste values in cells, write in cells by VBA code, but we cannot edit cells' value anymore by double-clicking the cell (or F2). The cell edition is frozen! (It is like unchecking the option "Allow editing directly in cells" (Options->Advanced options-> (Sector)"Editing options".)
To create the bug:
Click on an ActiveX control while you are in full screen mode (Application.DisplayFullScreen = True)
...and the bug is there now you cannot edit cells anymore!
The bug stays as long as the formula bar is not physically visible on your screen (fullScreen or not) and will STOP (as long as you do not recreate the bug...) as soon as the formula bar is displayed (staying in fullScreen and executing "Application.DisplayFormulaBar = True" won't work)
The bug will "temporarily" stop (with hidden formula bar) if you activate another sheet but will start again if you click on any other ActiveX control in any sheets (fullScreen or not)...
The answer is a good lesson in form. Try to avoid .Activate and .Select. If you always specify your ranges you can avoid situations like this with multiple workbooks. Use the format:
Workbooks("myBook.xlsx").Sheets("Sheet1").Range("A1").Value = "whatever"
or:
with Workbooks("myBook.xlsx").Sheets("Sheet1")
.Range("A1").Value = "A"
.Range("A2").Value = 2
.Range("A3").Value = "Blah"
End With

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?

change font within a shape ppt

I'm automatically generating a powerpoint slide through VBA, User Forms, and Excel. You run the VBA script in excel, fill out the data, the data goes into cells in excel, then the VBA script pulls the data and puts it into textbox shapes in the slide.
My problem is I want to use different font sizes at different times, so for example 28 pt font for one part and 14 pt for the rest. The problem is that any property changes I make to the textbox applies to all of the text within the shape.
My current workaround is sloppy and is to just generate another textbox over the original and insert spacing in the original so it looks like the larger text is "in" the textbox while it's actually just sitting over a few empty lines set aside.
You can format specific substrings within a string, but it's very cumbersome, for example assuming shp is an object variable representing your textbox:
Sub foo()
Dim shp As Shape
Set shp = ActivePresentation.Slides(1).Shapes("TextBox 3")
shp.TextFrame.TextRange.Text = "Hello, world!"
shp.TextFrame.TextRange.Characters.Font.Size = 14 'applies uniform font to entire shape
shp.TextFrame.TextRange.Characters(1, 5).Characters.Font.Size = 28
End Sub
Example output:
The difficulty of course is working with mixed formats, and I do not think there is any easy solution. It will be up to you to determine what formats you need to "capture", and what subsequently implement the appropriate conditional logic to transfer those formats to the PowerPoint shapes.
One possible alternative -- and this is the route that I would go if I were you -- would be to copy the cell from Excel, and use this method to paste in to PowerPoint. I believe this will create a table consisting of a single cell, in the PowerPoint slide. You will have to make adjustments for size/position, but this should be an order of magnitude easier than trying to capture every possible variation of font formatting:
Sub foo2()
Dim shp As Shape
Dim xl As Object
'Get Excel and copy a specific cell
Set xl = GetObject(, "Excel.Application")
xl.Workbooks("Book35").Sheets("Sheet2").Range("B4").Copy
'Paste that cell in to PowerPoint as a table, preserving formats:
ActivePresentation.Slides(1).Select
Application.CommandBars.ExecuteMso "PasteSourceFormatting"
End Sub
Example output, as copied from the Excel cell:
No need to change the font in excel to reflect in Word. You can do it directly. Just paste the below mentinoed line in Word VBA : -
Activedocument.Shapes("sam").TextFrame.TextRange.Words(1).Font.Size = 28