Change Axis Label font size in a funnel chart (Excel 2016) - excel

I want to change font size on axis xlCategory. I am running the code like below but it is failing. I am stuck in finding workarounds. Chart is "Funnel" type 123. On the other types of chart code is running fine.
With ActiveChart.Axes(xlCategory).TickLabels.font
.size = 12
.Name = "Arial"
End With
I get the error:
"object doesn't support this action"
I was also trying to run the othe code but also without any success.
ActiveChart.Axes(xlCategory).Select
With Selection.Format.TextFrame2.TextRange.font
.size = 12
End With
I get the error:
"method textframe2 of object chartformat failed"

This is a very peculiar bug!
I guess it has to do with the fact that the XlCategory axis becomes vertical when you are using the "Funnel" chart type:
I've tried all the standard VBA ways I could think of and this doesn't seem to work. I even tried to see if we could save a funnel chart as a template and then use VBA to apply this template, but "Save as Template" is not available for some reason...
So, the reason why I'm explaining all this instead of writing a solution is because there is a workaround, but this kind of thing should only be used as a last resort. And if you haven't guessed it, I'm talking about using the SendKeys method.
Disclaimer: As always, using SendKeys is somewhat tricky and can lead to unexpected results. I would recommend reading some articles to get yourself familiar with it before using it. This article would be a good starting point.
Furthermore, to avoid any issues, don't run a macro that has the SendKeys method using the Run button in the VBE since the keystroke must be sent to the Excel window and not the VBE window. I suggest launching the macro using a shortcut to launch your macro. For example, you could use the Application.OnKey method by runnning a macro similar to this:
Sub SetShortcut()
'Set the shortcut for the macro to be CTRL+ALT+m
Application.OnKey "^%m", "NameOfYourMacro"
End Sub
An example using Sendkeys
The idea is to use Sendkeys to send the sequence of keystrokes that would give us the result we want. Here, we want to change the size and the font name of the XlCategory axis. By using the correct Alt key shortcut sequence we can achieve this. I'm not sure if the sequence is the same for all versions of Excel, but with Office 365, if we wanted to change the font name, it would be:
Type Alt - to initiate the Alt Key shortcut sequence.
Type "H" - to get the Home tab
Type "FF" - to select the Font Name text box
Type "Arial" and then press Enter - to change the font to Arial.
For this to apply to the XlCategory axis, we need to make sure that it is already selected before we run the macro. If it is selected, the following code should do the trick:
Sub ChangeCategoryAxisFont()
'Font Name
Application.SendKeys Keys:="%", Wait:=True
Application.SendKeys Keys:="H", Wait:=True
Application.SendKeys Keys:="FF", Wait:=True
Application.SendKeys Keys:="Arial", Wait:=True
Application.SendKeys Keys:="~", Wait:=True 'Press Enter
'Size
Application.SendKeys Keys:="%", Wait:=True
Application.SendKeys Keys:="H", Wait:=True
Application.SendKeys Keys:="FS", Wait:=True
Application.SendKeys Keys:="12", Wait:=True
Application.SendKeys Keys:="~", Wait:=True 'Press Enter
'Restore Numlock (which gets disabled when your macro runs at least one SendKeys command)
Application.SendKeys Keys:="{NUMLOCK}", Wait:=True
End Sub
If it is not already selected (even if the chart is selected), you might have another problem: if you include ActiveChart.Axes(xlCategory).Select in your macro, the options available in the ribbon won't be updated correctly and the macro above won't work. To let the ribbon update, we would need to end the execution of any macros for a brief second and proceed with our macro after.
For instance, you could use the Application.OnTime method to schedule the macro to run one second later, like this:
Sub PreSelectAxisAndRunMainMacro()
ActiveChart.Axes(xlCategory).Select
Application.OnTime Now + TimeSerial(0, 0, 1), "ChangeCategoryAxisFont"
End Sub
I know what you might think: "All this just to make some font changes?!". Yeah, I know, but at least if you become familiar with how to use the SendKeys method, you'll be able to use this in other contexts that require it...

Related

How to screenshot excel macro dialog box and print in workbook using vba SendKeys function

I am working on screenshotting a dialog box in my macro that is then printed onto the workbook to record the specific dialog inputs. I am running into an issue where I use this code:
Application.SendKeys "(%{1068})"
Application.Wait (Now() + TimeValue("00:00:05"))
DoEvents
Sheets("Summary Report").Range("B5").PasteSpecial
Application.CutCopyMode = True
and it's printing the entire screen, instead of just the dialog box that holds the inputs. This code is put in right after prompting the dialog box/window to appear, so I assumed that doing the
Application.SendKeys "(%{1068})" would function the same as alt+printscreen so only the box is copied and then pasted using:
Sheets("Summary Report").Range("B5").PasteSpecial
I see that SendKeys technically has a printscreen syntax, but it can't be used on individual applications according to the excel syntax page https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/sendkeys-statement
Any info or suggestions would be greatly appreciated!

Excel VBA - SendKeys "{F2}" - keeps toggling num-lock

After using setting a cell to select and then using SendKeys "{F2}" (So that the cell is ready for input) it likes to toggle num-lock every time this is ran.
I have seen people say to use SendKeys "{NUMLOCK}", True after the SendKeys "{F2}" but tat did nto help. even tried adding Application.Wait Now + TimeValue("00:00:01") between the two comands, but it still keeps toggling.
I have also read its a bug with Excel/VBA.
Is there a better solutions? I need to select the cell and then force it into edit mode in order for some validation checks to work properly, else it bypasses the check if it just selects the cell, the could continue on, forcing it into edit make it so they have to make a change if not and they leave the cell the validation kicks in again.
I also found this code and tried, but that doe not work, it just keeps toggling
Dim wshshell As Object
Set wshshell = CreateObject("WScript.Shell")
wshshell.SendKeys "{NUMLOCK}"
Set wshshell = Nothing
Even tried this and replaced with F2 but it still toggled numlock.
I need numlock to stay on at all times. is there another way to invoke the edit cell instead of F2?

Creating an image from a userform of exported charts

I have written a code that charts various data. The charts are then exported as .gif files. The image files are then loaded as pictures into image controls in a userform as shown below.
How do I go about exporting the whole userform (preferably without the 3 buttons on the right) as an image for use in a report?
Currently I am using manual printscreen but it is quite time consuming..
Chart Example
Currently I am manually using printscreen but it is quite time consuming..
Is there a way to access the alt-printscreen button perhaps?
The chart creation code itself isn't really relevant but I can share if required.
I expect an image file that I can then use within a word document.
Preferably cropped to remove the 3 buttons on the right of the userform.
Steven,
In the below sub, I used the SendKeys method to send Alt+PrintScreen ("%{1068}") and Ctrl+V ("^v") on the current application. The SendKeys method simulates the key presses of a keyboard, so it would replicate what you're doing right now.
Sub PrintTheScreen()
Application.SendKeys ("%{1068}")
DoEvents
Application.SendKeys ("^v")
DoEvents
End Sub
Reference for this method can be found here
Having the sendkeys applied to the userform will simulate your button press. By then, you could set a word application object and paste the printed screen, for example.
Sub CreatingWDDoc()
Dim appWD As Word.Application
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
appWD.Documents.Add
appWD.SendKeys ("^v")
' Change the string to the path you wish to use for seving the file
appWD.SaveAs (“”)
appWD.Quit
End Sub
Keep in mind that to use such functions, you need to have the word library referenced in your project, under Tools >> References. More information can be found here.

How to sendkeys unicode character VBA - without clipboard

I need to insert chatacter 'ü' to the web page with VBA via SendKeys.
Application.SendKeys "ü" does nothing
Then I tried to send ALT+code combination:
Application.SendKeys "%(0252)" does nothing for ALT+0252
Application.SendKeys "%(129)" does nothing for ALT+129
I cannot (don't want to) use clipboard and paste it. It is a nice workaround but not applicable in this case.
No, API doesn't work.
Any ideas?

How to copy-paste from DOS application to Excel using SendKeys?

I'm trying to simulate this action:
activate another application,
send keystroke ctrl+c,
go back to Excel,
send key stroke ctrl+v
and have that value in a cell.
It's a DOS window style application, so keystroke is the only way to manage it.
I managed to activate and to input keystrokes such as ENTER into that DOS style application, but when I try ctrl+C it is not seen to do anything.
I tried simulating it in Excel VBA with:
Range("E7").Select
SendKeys "^c"
Range("G7").Select
SendKeys "^v"
The E7 value is not copied, but G7 (paste destination) is highlighted as if it was selected for copying.
Note: I am not trying to copy things from Excel to Excel, but to execute keystrokes using Excel.
I ran into the same issue today.
I solved it by waiting a bit after sending CTRL-C to the application. It seems nothing is copied if you immediately execute another script line.
SendKeys "^c"
WScript.Sleep 100 ' wait a bit after CTRL+C (otherwise nothing is copied)
In fact, the same issue seems to happen when switching to another app and sending CTRL+V.
Again, I solved it by waiting a bit:
AppActivate "Microsoft Excel"
WScript.Sleep 100 ' wait a bit after making window active
SendKeys "^v"
The problem is that copying and pasting takes some time and VBA is not waiting for it.
First you need to specify second argument of send keys method which is "Wait". Set it to true. Thus VBA execution is waiting for until sending keys is completed.
Secondly you need to wait until process of copying data to clipboard is completed. "Wait" in sendkeys is not doing it because it's not about sendking keys by VBA but it's about Windows working with clipboard. To do it please use my function IsCopyingCompleted.
Here's how final can look like:
SendKeys "^a", True 'Select all
SendKeys "^c", True 'Copy
Do
DoEvents
Loop Until Me.IsCopyingCompleted()
YourSheet.Paste
Function IsCopyingCompleted() As Boolean
'Check if copying data to clipboard is completed
Dim tempString As String
Dim myData As DataObject
'Try to put data from clipboard to string to check if operations on clipboard are completed
On Error Resume Next
Set myData = New DataObject
myData.GetFromClipboard
tempString = myData.GetText(1)
If Err.Number = 0 Then
IsCopyingCompleted = True
Else
IsCopyingCompleted = False
End If
On Error GoTo 0
End Function
I had this same problem - I developed code and it worked on my computer - SendKeys "^v"...but it did not work on another user's computer! I was trying EVERYTHING...delays, appreciate, accessig access from Excel, having the user check various settings, selection.paste...no good. Finally I noticed different syntaxes...we were using the same office version, I'm not sure about Windows. But I entered 6 different syntaxes and had it skip over if it failed....SendKeys with a capital v, enclosed in squigley brackets, each component in squigleys separated by a plus sign, etc. IT WORKED! 2 of the commands put the ^V in the body of outlook...I remove those 2 and I'm golden.

Resources