Creating chart as GIF and load onto UserForm - excel

I'm using John Walkenbach code to export a chart as a GIF and then load it onto a user form. The problem I'm having is With this bit here Image1.Picture = LoadPicture(Fname). It is not loading the GIF onto the UserForm. It exports the Image fine I can see it in the same directory as my excel file.
Private Sub CommandButton1_Click()
Call GetChart
Image1.Picture = LoadPicture(Fname)
MsgBox "Yep"
End Sub
Public Sub GetChart()
Set CurrentChart = Sheets("StatsDB").ChartObjects(1).Chart
Fname = ThisWorkbook.Path & "/temp.gif"
CurrentChart.Export Filename:=Fname, FilterName:="GIF"
End Sub

You need to assign a value to Fname before you use it. There are several way of doing that:
1) Just assigning the value in CommandButton1_Click
Private Sub CommandButton1_Click()
GetChart
Fname = ThisWorkbook.Path & "/temp.gif"
Image1.Picture = LoadPicture(Fname)
MsgBox "Yep"
End Sub
Public Sub GetChart()
Set CurrentChart = Sheets("StatsDB").ChartObjects(1).Chart
Fname = ThisWorkbook.Path & "/temp.gif"
CurrentChart.Export Filename:=Fname, FilterName:="GIF"
End Sub
2) Passing the value you used in GetChart back as a "return" value:
Private Sub CommandButton1_Click()
Fname = GetChart()
Image1.Picture = LoadPicture(Fname)
MsgBox "Yep"
End Sub
Public Function GetChart() As String
Set CurrentChart = Sheets("StatsDB").ChartObjects(1).Chart
Fname = ThisWorkbook.Path & "/temp.gif"
CurrentChart.Export Filename:=Fname, FilterName:="GIF"
GetChart = Fname
End Sub
3) Making Fname module-level in scope:
Dim Fname As String
Private Sub CommandButton1_Click()
GetChart
Image1.Picture = LoadPicture(Fname)
MsgBox "Yep"
End Sub
Public Sub GetChart()
Set CurrentChart = Sheets("StatsDB").ChartObjects(1).Chart
Fname = ThisWorkbook.Path & "/temp.gif"
CurrentChart.Export Filename:=Fname, FilterName:="GIF"
End Sub
4) Combining the two subroutines into one:
Private Sub CommandButton1_Click()
Set CurrentChart = Sheets("StatsDB").ChartObjects(1).Chart
Fname = ThisWorkbook.Path & "/temp.gif"
CurrentChart.Export Filename:=Fname, FilterName:="GIF"
Image1.Picture = LoadPicture(Fname)
MsgBox "Yep"
End Sub
There would be lots more ways of doing this, but hopefully one of the above appeals to you.

Related

how exit sub in cancel input box

i want save chart to png format, (inputbox my png name)
my code is
Sub savechartaspng()
Dim fname As String
ActiveSheet.ChartObjects("Chart 1").Activate
If ActiveChart Is Nothing Then Exit Sub
fname = ThisWorkbook.Path & "\" & InputBox("filename") & ".png"
ActiveChart.Export FileName:=fname, filtername:="png"
MsgBox "saved"
End Sub
how inputbox = cancel or closed to exit sub and Do not save ?
tnx
Get the input from the InputBox first, check check to see if it's blank or not, and only continue if it's not blank.
Sub savechartaspng()
Dim fname As String
ActiveSheet.ChartObjects("Chart 1").Activate
If ActiveChart Is Nothing Then Exit Sub
fname = InputBox("filename")
If fname = "" Then Exit Sub
fname = ThisWorkbook.Path & "\" & fname & ".png"
ActiveChart.Export FileName:=fname, filtername:="png"
MsgBox "saved"
End Sub
Note: It might be a goof idea to also have code that checks to make sure the user did not enter any characters that cannot be used in filenames too.

Interactive Excel Calendar

I want to make an excel calendar interactive. When I click on a day, I want it to take the user to a new box/screen/cell where they can see a day schedule instead of the default month schedule.
I know I can hyperlink each cell to another cell on a different page but that would be very tedious. How can I automate this?
You can link the cell so when a user clicks into it, normally toggling the cell to be changed, it instead takes triggers an event. Here is some code that will trigger an event when a desired cell is selected posted by "iwrk4dedpr" on ozgrid.com. As for the actual code you would input to take the user to a new sheet with all the days events on it, well you specifically asked me to not tell you the answer. Have Fun looking/Learning!
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Do something if Cell(1,1) or Range("A1") has been selected
if target.row = 1 and target.column = 1 then
' your code in here
end if
End Sub
You can try:
Option Explicit
Dim ThisDay As Date
Dim ThisYear, ThisMth As Date
Dim CreateCal As Boolean
Dim i As Integer
Private Sub HelpLabel_Click()
End Sub
Private Sub UserForm_Initialize()
Application.EnableEvents = False
'starts the form on todays date
ThisDay = Date
ThisMth = Format(ThisDay, "mm")
ThisYear = Format(ThisDay, "yyyy")
For i = 1 To 12
CB_Mth.AddItem Format(DateSerial(Year(Date), Month(Date) + i, 0), "mmmm")
Next
CB_Mth.ListIndex = Format(Date, "mm") - Format(Date, "mm")
For i = -20 To 50
If i = 1 Then CB_Yr.AddItem Format((ThisDay), "yyyy") Else CB_Yr.AddItem _
Format((DateAdd("yyyy", (i - 1), ThisDay)), "yyyy")
Next
CB_Yr.ListIndex = 21
'Builds the calendar with todays date
CalendarFrm.Width = CalendarFrm.Width
CreateCal = True
Call Build_Calendar
Application.EnableEvents = True
End Sub
Private Sub CB_Mth_Change()
'rebuilds the calendar when the month is changed by the user
Build_Calendar
End Sub
Private Sub CB_Yr_Change()
'rebuilds the calendar when the year is changed by the user
Build_Calendar
End Sub
Private Sub Build_Calendar()
'the routine that actually builds the calendar each time
If CreateCal = True Then
CalendarFrm.Caption = " " & CB_Mth.Value & " " & CB_Yr.Value
'sets the focus for the todays date button
CommandButton1.SetFocus
For i = 1 To 42
If i < Weekday((CB_Mth.Value) & "/1/" & (CB_Yr.Value)) Then
Controls("D" & (i)).Caption = Format(DateAdd("d", (i - Weekday((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), _
((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), "d")
Controls("D" & (i)).ControlTipText = Format(DateAdd("d", (i - Weekday((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), _
((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), "m/d/yy")
ElseIf i >= Weekday((CB_Mth.Value) & "/1/" & (CB_Yr.Value)) Then
Controls("D" & (i)).Caption = Format(DateAdd("d", (i - Weekday((CB_Mth.Value) _
& "/1/" & (CB_Yr.Value))), ((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), "d")
Controls("D" & (i)).ControlTipText = Format(DateAdd("d", (i - Weekday((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), _
((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), "m/d/yy")
End If
If Format(DateAdd("d", (i - Weekday((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), _
((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), "mmmm") = ((CB_Mth.Value)) Then
If Controls("D" & (i)).BackColor <> &H80000016 Then Controls("D" & (i)).BackColor = &H80000018 '&H80000010
Controls("D" & (i)).Font.Bold = True
If Format(DateAdd("d", (i - Weekday((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), _
((CB_Mth.Value) & "/1/" & (CB_Yr.Value))), "m/d/yy") = Format(ThisDay, "m/d/yy") Then Controls("D" & (i)).SetFocus
Else
If Controls("D" & (i)).BackColor <> &H80000016 Then Controls("D" & (i)).BackColor = &H8000000F
Controls("D" & (i)).Font.Bold = False
End If
Next
End If
End Sub
Private Sub D1_Click()
'this sub and the ones following represent the buttons for days on the form
'retrieves the current value of the individual controltiptext and
'places it in the active cell
ActiveCell.Value = D1.ControlTipText
Unload Me
'after unload you can call a different userform to continue data entry
'uncomment this line and add a userform named UserForm2
'Userform2.Show
End Sub
Private Sub D2_Click()
ActiveCell.Value = D2.ControlTipText
Unload Me
End Sub
Private Sub D3_Click()
ActiveCell.Value = D3.ControlTipText
Unload Me
End Sub
Private Sub D4_Click()
ActiveCell.Value = D4.ControlTipText
Unload Me
End Sub
Private Sub D5_Click()
ActiveCell.Value = D5.ControlTipText
Unload Me
End Sub
Private Sub D6_Click()
ActiveCell.Value = D6.ControlTipText
Unload Me
End Sub
Private Sub D7_Click()
ActiveCell.Value = D7.ControlTipText
Unload Me
End Sub
Private Sub D8_Click()
ActiveCell.Value = D8.ControlTipText
Unload Me
End Sub
Private Sub D9_Click()
ActiveCell.Value = D9.ControlTipText
Unload Me
End Sub
Private Sub D10_Click()
ActiveCell.Value = D10.ControlTipText
Unload Me
End Sub
Private Sub D11_Click()
ActiveCell.Value = D11.ControlTipText
Unload Me
End Sub
Private Sub D12_Click()
ActiveCell.Value = D12.ControlTipText
Unload Me
End Sub
Private Sub D13_Click()
ActiveCell.Value = D13.ControlTipText
Unload Me
End Sub
Private Sub D14_Click()
ActiveCell.Value = D14.ControlTipText
Unload Me
End Sub
Private Sub D15_Click()
ActiveCell.Value = D15.ControlTipText
Unload Me
End Sub
Private Sub D16_Click()
ActiveCell.Value = D16.ControlTipText
Unload Me
End Sub
Private Sub D17_Click()
ActiveCell.Value = D17.ControlTipText
Unload Me
End Sub
Private Sub D18_Click()
ActiveCell.Value = D18.ControlTipText
Unload Me
End Sub
Private Sub D19_Click()
ActiveCell.Value = D19.ControlTipText
Unload Me
End Sub
Private Sub D20_Click()
ActiveCell.Value = D20.ControlTipText
Unload Me
End Sub
Private Sub D21_Click()
ActiveCell.Value = D21.ControlTipText
Unload Me
End Sub
Private Sub D22_Click()
ActiveCell.Value = D22.ControlTipText
Unload Me
End Sub
Private Sub D23_Click()
ActiveCell.Value = D23.ControlTipText
Unload Me
End Sub
Private Sub D24_Click()
ActiveCell.Value = D24.ControlTipText
Unload Me
End Sub
Private Sub D25_Click()
ActiveCell.Value = D25.ControlTipText
Unload Me
End Sub
Private Sub D26_Click()
ActiveCell.Value = D26.ControlTipText
Unload Me
End Sub
Private Sub D27_Click()
ActiveCell.Value = D27.ControlTipText
Unload Me
End Sub
Private Sub D28_Click()
ActiveCell.Value = D28.ControlTipText
Unload Me
End Sub
Private Sub D29_Click()
ActiveCell.Value = D29.ControlTipText
Unload Me
End Sub
Private Sub D30_Click()
ActiveCell.Value = D30.ControlTipText
Unload Me
End Sub
Private Sub D31_Click()
ActiveCell.Value = D31.ControlTipText
Unload Me
End Sub
Private Sub D32_Click()
ActiveCell.Value = D32.ControlTipText
Unload Me
End Sub
Private Sub D33_Click()
ActiveCell.Value = D33.ControlTipText
Unload Me
End Sub
Private Sub D34_Click()
ActiveCell.Value = D34.ControlTipText
Unload Me
End Sub
Private Sub D35_Click()
ActiveCell.Value = D35.ControlTipText
Unload Me
End Sub
Private Sub D36_Click()
ActiveCell.Value = D36.ControlTipText
Unload Me
End Sub
Private Sub D37_Click()
ActiveCell.Value = D37.ControlTipText
Unload Me
End Sub
Private Sub D38_Click()
ActiveCell.Value = D38.ControlTipText
Unload Me
End Sub
Private Sub D39_Click()
ActiveCell.Value = D39.ControlTipText
Unload Me
End Sub
Private Sub D40_Click()
ActiveCell.Value = D40.ControlTipText
Unload Me
End Sub
Private Sub D41_Click()
ActiveCell.Value = D41.ControlTipText
Unload Me
End Sub
Private Sub D42_Click()
ActiveCell.Value = D42.ControlTipText
Unload Me
End Sub

VBA Excel: Save to different directory

I currently use the following to save the file "Book1" with today's date. However, the file automatically saves in the same folder as "Book1". I am trying to save the files into a different directory, I am wondering how I can do that.
Sub filesave()
ActiveWorkbook.SaveAs ("Z:\Henry\test\Book1 " & Format(Now(), "YYYYMMDD") & ".xlsx")
End Sub
You use a folderPicker to allow the user to select their folder of choice. Be aware that the activeworkbook must be an xlsx.
Option Explicit
Public Sub SelectFolder()
Dim sFolder As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = -1 Then
sFolder = .SelectedItems(1)
End If
End With
If sFolder <> vbNullString Then
ActiveWorkbook.SaveAs sFolder & Format$(Now(), "YYYYMMDD") & ".xlsx"
End If
End Sub
Code adapted from here.
A similar version that drops the comparison with vbNullString, courtesy of #JohnyL:
Option Explicit
Public Sub SelectFolder()
Dim sFolder As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then
sFolder = .SelectedItems(1)
Else
Exit Sub
End If
End With
ActiveWorkbook.SaveAs sFolder & Format$(Now(), "YYYYMMDD") & ".xlsx"
End Sub
You could do something like this.
Sub filesave()
ActiveWorkbook.SaveAs Filename:="Z:\Henry\test\Book1" _
& Format(Now(), "YYYYMMDD") & ".xlsx"
End Sub

Function Input is False

So here is how I call the function:
FilePathLoD(FirewallAssy)
And the function is:
Function FilePathLoD(FileName as String)
Application.ScreenUpdating = False
Activewindow.WindowState = xlMinimized
FilePathLoD = "E:\List of Drawings"
Workbooks.Open (FilePathLoD & "\" & FileName & "LoD.xlsm")
Activewindow.WindowState = xlMaximized
ThisWorkbook.Activate
Application.ScreenUpdating = True
End Function
When I call the function the strings are parsed together as such.
E:\List of Drawings\FalseLoD.xlsm
Why is this happening and how do I fix this?
FileName is the first parameter of the Workbooks.Open method. Change it to fname to avoid confusion.
Change this to a Sub procedure. You are not returning a value; simply implementing a method. A Sub is more appropriate than a Function for this operation.
Sub FilePathLoD(fName as String)
Dim fPath as String
Application.ScreenUpdating = False
Activewindow.WindowState = xlMinimized
fPath = "E:\List of Drawings"
Workbooks.Open (fPath & "\" & fName & "LoD.xlsm")
Activewindow.WindowState = xlMaximized
ThisWorkbook.Activate
Application.ScreenUpdating = True
End Function
Call it as a procedure.
FilePathLoD FirewallAssy
'... or,
Call FilePathLoD(FirewallAssy)

Cut and past excel file in vba

With the below code, I am able to create a copy of excel but I want to move the particular file from one location to another location. Please advise as to what all changes are require in below code.
myFileNameDir = Sheet1.Range("V4").Value & TextBox38.Text & ".xlsx"
Workbooks.Open Filename:=myFileNameDir, UpdateLinks:=0
Set ws1 = Worksheets("sheet1")
ws1.Activate
ws1.SaveAs Sheet1.Range("V3").Value & TextBox3.Text & ".xlsx"
Try:
ws1.SaveAs "C:\yourpath\" & Sheet1.Range("V3").Value & TextBox3.Text & ".xlsm"
This code will move your file without having to open it:
Sub test()
Dim mySourceFileName As String
Dim myTargetFileName As String
mySourceFileName = Sheet1.Range("V4").Value & TextBox38.Text & ".xlsx"
myTargetFileName = Sheet1.Range("V3").Value & TextBox3.Text & ".xlsx"
MsgBox "File Copied:" & CopyFile(mySourceFileName, myTargetFileName, True), vbOKOnly
End Sub
Function CopyFile(FromFile As String, ToFile As String, Overwrite As Boolean) As Boolean
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
oFSO.CopyFile FromFile, ToFile, Overwrite
CopyFile = (Err.Number = 0)
Err.Clear
End Function
To move the file use:
Sub test()
Dim mySourceFileName As String
Dim myTargetFileName As String
mySourceFileName = Sheet1.Range("V4").Value & TextBox38.Text & ".xlsx"
myTargetFileName = Sheet1.Range("V3").Value & TextBox3.Text & ".xlsx"
MsgBox "File Moved:" & MoveFile(mySourceFileName, myTargetFileName), vbOKOnly
End Sub
Public Function MoveFile(FromFile As String, ToFile As String) As Boolean
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
oFSO.MoveFile FromFile, ToFile
MoveFile = (Err.Number = 0)
Err.Clear
End Function

Resources