2017年3月5日 星期日

[VisualBox] 無法共用剪貼簿

VisualBox 是 Oracle 佛心來給眾生免費使用的虛擬主機程式,就像是 MS 的 Hyper-V 功能一樣。一直使用這個軟體二、三年了,作測試或是作開發環境都很正常,但 VisualBox 無法共用剪貼簿的問題困擾我好久,算是這個問題不大,心裡就是湊合著用就用,等到每次要用到剪下、貼上時嘴巴就哼個二聲,說這個軟體真爛。

而昨天我就真的認真了,直接上 Stack overflow 找答案,原來只要安裝 Guest Additions CD 就可以啟用「共用剪貼簿」了,Guest Additions CD 的映像檔就在以下的第二張圖哦,請安心服用。

image

 

image

2017年2月22日 星期三

[VBA] 自動化產生圖表 (Chart)

實際運作擷圖

VGA-Chart

第一次在部落格中記錄 VBA 程式,因為最近有個需求,來源是客戶每週有個重要任務:整理公司的生產統計資料並產生多種不同圖表於 Weekly review會議中報告,而這些圖表包含有 Pie、Line圖等。
客戶久而久之發現整理這些資料很花時間,重覆性很高,聽別人說有 Excel VBA ,可以將原本手動的工作改為程式處理,以後只要執行程式就可以自動產生出圖表。沒錯,這個客戶的觀念非常正確,Excel VBA 就是這樣好用,如果你是Excel的重度使用者,常常有一些計算或是統計的任務或許可以考慮將這些工作交由程式處理,以自動化的方式處理。那你省下來的時間呢?當然是去作更重要的事情啦!
image 

以下是這次 VBA 的部份程式,我擷取部份精華下來作為記錄也分享給大家。
image
image
第1部份:原始資料(Raw data) 產生樞紐分析表
'=====================================================
'2.Create Pivot table
'=====================================================
'Where do you want Pivot Table to start?
StartPvt = shtName & "!" & Range("A2").Address(ReferenceStyle:=xlR1C1)
'Create Pivot Cache from Source Data
Set pvtCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=SrcData)
'Create Pivot table from Pivot Cache
Set PT = pvtCache.CreatePivotTable(TableDestination:=StartPvt, TableName:="PivotTable" & shtName)
  
'在新的PivotTable上設置欄位
With PT
'欄
    .PivotFields("Region").Orientation = xlRowField
'資料
    .PivotFields("NO").Orientation = xlDataField
'可以設定各欄位的顯示與否
    With .PivotFields("Page Two.RMA Region")
        .PivotItems("(blank)").Visible = False
    End With
End With
'建立樞紐分析表會顯示出其命令列, 關掉它
Application.CommandBars("PivotTable").Visible = False
第2部份:樞紐分析表轉為 Pie 圖表
'=====================================================
'3.Create Chart
'=====================================================
Set objChart = Charts.Add
With objChart
    .ChartType = xlPie
    .SetSourceData Source:=Sheets(shtName).Range, PlotBy:=xlColumns
    .Location Where:=xlLocationAsObject, Name:=shtName
End With
'Chart 定位, 大小
With ActiveChart.Parent
    .Left = 200
    .Top = 50
    .Width = 450
    .Height = 350
End With
'DataLabel 及 Title
With ActiveChart
    .HasTitle = True
    .ChartTitle.Text = "By Region"
    .ApplyDataLabels xlDataLabelsShowPercent
    .SeriesCollection(1).DataLabels.NumberFormat = "##0.00%"
End With
第3部份:Pie 圖表匯出到 PowerPoint,以下這段程式碼是由 G 大神來的
Sub ChartsToPresentation()
' Set a VBE reference to Microsoft PowerPoint Object Library
Dim PPT As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim PresentationFileName As Variant
Dim SlideCount As Long
Dim iCht As Integer
' Reference existing instance of PowerPoint
Set PPApp = GetObject(, "Powerpoint.Application")
' Reference active presentation
Set PPPres = PPApp.ActivePresentation
PPApp.ActiveWindow.ViewType = ppViewSlide
For iCht = 1 To ActiveSheet.ChartObjects.Count
' copy chart as a picture
ActiveSheet.ChartObjects(iCht).Chart.CopyPicture _
Appearance:=xlScreen, Size:=xlScreen, Format:=xlPicture
' Add a new slide and paste in the chart
SlideCount = PPPres.Slides.Count
Set PPSlide = PPPres.Slides.Add(SlideCount + 1, ppLayoutBlank)
PPApp.ActiveWindow.View.GotoSlide PPSlide.SlideIndex
With PPSlide
' paste and select the chart picture
.Shapes.Paste.Select
' align the chart
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignCenters, True
PPApp.ActiveWindow.Selection.ShapeRange.Align msoAlignMiddles, True
End With
Next
' Clean up
Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing
End Sub

完整程式及測試檔案
https://drive.google.com/open?id=0B0pY4dpq6q9WZVZtZ21fbWNfMEU

2017年2月20日 星期一

[FB] 粉絲專頁管理員

剛在 FB 上花了十分鐘找不到粉絲專頁管理員的設定,趕快記下來免得下回又找不到了。

image

2016年7月1日 星期五

Access 2007 開啟時只顯示表單

客戶需求:
一般使用者進入 Access 2007 只顯示表單(Form),將資料表隱藏起來,並且無法直接存取資料表(Table)。避免一般使用者直接更改資料表內容。

經過 Google 查詢國內外的資料,確認只要加上以下的 code 於VBA程式碼,再重新啟動就看不到表單之外的資料。那開發人員或是管理者呢?處理方式為開啟Access檔案時,按下 Shift 鍵就會跳過以下程序。

Sub SetStartupProperties()
Const DB_Text As Long = 10
Const DB_Boolean As Long = 1
    ChangeProperty "StartupForm", DB_Text, "首頁"  ‘修改為啟動表單頁
    ChangeProperty "StartupShowDBWindow", DB_Boolean, False
    ChangeProperty "StartupShowStatusBar", DB_Boolean, False
    ChangeProperty "StartupMenuBar", DB_Boolean, False
    ChangeProperty "AllowShortcutMenus", DB_Boolean, False
    ChangeProperty "AllowBuiltInToolbars", DB_Boolean, False
    ChangeProperty "AllowFullMenus", DB_Boolean, False
    ChangeProperty "AllowBreakIntoCode", DB_Boolean, False
    ChangeProperty "AllowSpecialKeys", DB_Boolean, True
    ChangeProperty "AllowBypassKey", DB_Boolean, True
   
    DoCmd.ShowToolbar "Ribbon", acToolbarNo
    DoCmd.ShowToolbar "Status Bar", acToolbarNo
    DoCmd.NavigateTo "acNavigationCategoryObjectType"
    DoCmd.RunCommand acCmdWindowHide
   
End Sub

Function ChangeProperty(strPropName As String, varPropType As Variant, varPropValue As Variant) As Integer
    Dim dbs As Object, prp As Variant
    Const conPropNotFoundError = 3270

    Set dbs = CurrentDb
    On Error GoTo Change_Err
    dbs.Properties(strPropName) = varPropValue
    ChangeProperty = True

Change_Bye:
    Exit Function

Change_Err:
    If Err = conPropNotFoundError Then ' 找不到屬性。
     Set prp = dbs.CreateProperty(strPropName, _
     varPropType, varPropValue)
     dbs.Properties.Append prp
     Resume Next
    Else
     ' 未知的錯誤。
     ChangeProperty = False
     Resume Change_Bye
    End If
End Function

2016年6月20日 星期一

VS2013 failed to register URL 0x80070020

今天開啟一個久違的專案,無奈VS2013出現一個錯誤訊息:

vs2013 failed to register URL  0x80070020。

疑…上個月開專案不是好好的嗎?我的乖乖呢?網路上查了一些解決方式,研判是port被佔住造成無法執行VS2013內建的 IIS Express。但 VS2013的web port可不是說改就改的哦,修改方式我也幫你準備好了,請看以下步驟二。

步驟一、找出是否真的有程式佔用了port,123456是port編號。

netstat -a -n -o | find "123456"

 

步驟二、修改以下二個檔案內有關 port 的連結,換另一個port
  • C:\Users\[user name]\Documents\IISExpress\config\applicationhost.config
  • 專案.sln

2016年5月27日 星期五

取出字串的最後一個數字

取出字串中的第一數字比較容易,那字串有好多組數字,想要取回字串的最後一組字串怎麼處理呢?
請出Google大神,竟然沒有什麼參考資料,只好自己來。
以下為範例,重點在那個 REVERSE 函數哦!

DECLARE @productname varchar(100);
set  @productname = 'T20-5-2.12'

select
@productname,
cast(
reverse(
case PATINDEX('%[-,a-z,A-Z,~,@,#,$,%,&,*,(,),<,>]%',STUFF(REVERSE(@productname), 1,PATINDEX('%[0-9]%', REVERSE(@productname))-1,''))
when 0 then STUFF(REVERSE(@productname), 1,PATINDEX('%[0-9]%', REVERSE(@productname))-1,'')
else SUBSTRING(STUFF(REVERSE(@productname), 1,PATINDEX('%[0-9]%', REVERSE(@productname))-1,''),1,
     PATINDEX('%[-,a-z,A-Z,~,@,#,$,%,&,*,(,),<,>]%',STUFF(REVERSE(@productname), 1,PATINDEX('%[0-9]%', REVERSE(@productname))-1,''))-1)
end) as decimal(18,2))