Function Num2Str(Str As String) As String
For i = 1 To Len(Str)
a = Mid(Str, i, 1)
Select Case True
Case a = 0
aa = aa & “ 零”
Case a = 1
aa = aa & “ 壹”
Case a = 2
aa = aa & “ 貳”
Case a = 3
aa = aa & “ 參”
Case a = 4
aa = aa & “ 肆”
Case a = 5
aa = aa & “ 伍”
Case a = 6
aa = aa & “ 陸”
Case a = 7
aa = aa & “ 柒”
Case a = 8
aa = aa & “ 捌”
Case a = 9
aa = aa & “ 玖”
Case Else
aa = aa & a
End Select
Next i
aa = Right(“零 零 零 零 零 零 零 零 零” & aa, 18)
bb = Left(aa, 6) & ” ” & Mid(aa, 7, 2) & ” ” & Mid(aa, 9, 2) & ” ” & Mid(aa, 11, 2) & Right(aa, 6)
Num2Str = bb
End Function
Excel Button Click with ERROR handler
Sub 按鈕1_Click()
‘On Error Resume Next
On Error GoTo MyErrorHandler:
s_line = Cells(1, 8)
e_line = Cells(1, 9)
If Not IsNumeric(s_line) Then
MsgBox (“請輸入數字”)
End
End If
If Not IsNumeric(e_line) Then
MsgBox (“請輸入數字”)
End
End If
If s_line > e_line Then
MsgBox (“請輸入正確起訖”)
End
End If
If e_line – s_line > 4 Then
MsgBox (“最多輸入5筆資料”)
End
End If
Range(“C5:F9”).Value = “”
For i = 1 To e_line - s_line + 1
'Cells(4 + i, 3).Value = Worksheets("shipment").Cells(s_line + i - 1, 14).Value
Cells(4 + i, 3).Value = Application.WorksheetFunction.VLookup(Worksheets("shipment").Cells(s_line + i - 1, 6).Value, Worksheets("item_ref").Range("A:E"), 2, False)
Cells(4 + i, 5).Value = Worksheets("shipment").Cells(s_line + i - 1, 7).Value
Cells(4 + i, 6).Value = Worksheets("shipment").Cells(s_line + i - 1, 9).Value
Next i
Exit Sub
MyErrorHandler:
If Err.Number = 1004 Then
MsgBox “第” & CStr(s_line + i – 1) & “行無參照品名”
Resume Next
ElseIf Err.Number = 13 Then
MsgBox “You have entered an invalid value.”
End If
End Sub
自訂 IsInt函數 檢查是否是整數
Function IsInt(aValue as Variant) As Boolean
On Error Resume Next
IsInt = (CInt(aValue) = aValue)
On Error Goto 0
End Function
Delete Duplicates From a Table in SQL Server
To delete the duplicate rows from the table in SQL Server, you follow these steps:
- Find duplicate rows using
GROUP BY
clause orROW_NUMBER()
function. - Use
DELETE
statement to remove the duplicate rows.
Let’s set up a sample table for the demonstration.
Setting up a sample table
First, create a new table named sales.contacts
as follows:
DROP TABLE IF EXISTS sales.contacts;
CREATE TABLE sales.contacts(
contact_id INT IDENTITY(1,1) PRIMARY KEY,
first_name NVARCHAR(100) NOT NULL,
last_name NVARCHAR(100) NOT NULL,
email NVARCHAR(255) NOT NULL,
);
Code language: SQL (Structured Query Language) (sql)
Second, insert some rows into the sales.contacts
table:
INSERT INTO sales.contacts
(first_name,last_name,email)
VALUES
('Syed','Abbas','syed.abbas@example.com'),
('Catherine','Abel','catherine.abel@example.com'),
('Kim','Abercrombie','kim.abercrombie@example.com'),
('Kim','Abercrombie','kim.abercrombie@example.com'),
('Kim','Abercrombie','kim.abercrombie@example.com'),
('Hazem','Abolrous','hazem.abolrous@example.com'),
('Hazem','Abolrous','hazem.abolrous@example.com'),
('Humberto','Acevedo','humberto.acevedo@example.com'),
('Humberto','Acevedo','humberto.acevedo@example.com'),
('Pilar','Ackerman','pilar.ackerman@example.com');
Code language: SQL (Structured Query Language) (sql)
Third, query data from the sales.contacts
table:
SELECT
contact_id,
first_name,
last_name,
email
FROM
sales.contacts;
Code language: SQL (Structured Query Language) (sql)
The following picture shows the output of the query:

There are many duplicate rows (3,4,5), (6,7), and (8,9) for the contacts that have the same first name, last name, and email.
Delete duplicate rows from a table example
The following statement uses a common table expression (CTE) to delete duplicate rows:
WITH cte AS (
SELECT
contact_id,
first_name,
last_name,
email,
ROW_NUMBER() OVER (
PARTITION BY
first_name,
last_name,
email
ORDER BY
first_name,
last_name,
email
) row_num
FROM
sales.contacts
)
DELETE FROM cte
WHERE row_num > 1;
Code language: SQL (Structured Query Language) (sql)
In this statement:
- First, the CTE uses the
ROW_NUMBER()
function to find the duplicate rows specified by values in thefirst_name
,last_name
, andemail
columns. - Then, the
DELETE
statement deletes all the duplicate rows but keeps only one occurrence of each duplicate group.
SQL Server issued the following message indicating that the duplicate rows have been removed.
(4 rows affected)
If you query data from the sales.contacts
table again, you will find that all duplicate rows are deleted.
SELECT contact_id,
first_name,
last_name,
email
FROM sales.contacts
ORDER BY first_name,
last_name,
email;
Code language: SQL (Structured Query Language) (sql)

https://www.sqlservertutorial.net/sql-server-basics/delete-duplicates-sql-server/
Setup A Excel file as A linked server in ms-sql
USE [master]
GO
sp_configure ‘show advanced options’, 1
GO
RECONFIGURE WITH OverRide
GO
sp_configure ‘Ad Hoc Distributed Queries’, 1
GO
RECONFIGURE WITH OverRide
GO
EXEC master . dbo. sp_MSset_oledb_prop N’Microsoft.ACE.OLEDB.12.0′ , N’AllowInProcess’ , 1
GO
EXEC master . dbo. sp_MSset_oledb_prop N’Microsoft.ACE.OLEDB.12.0′ , N’DynamicParameters’ , 1
EXEC sp_addLinkedServer
@server= ‘XLSX_MATRIX’,
@srvproduct = ‘ACE 12.0’,
@provider = ‘Microsoft.ACE.OLEDB.12.0’,
@datasrc =’D:\SQL\vn_item_no.xlsx’,
@provstr = ‘Excel 12.0; HDR=Yes’
SELECT * FROM OPENQUERY (XLSX_MATRIX, ‘Select * from [Application$]’)
SELECT * FROM OPENQUERY (XLSX_MATRIX, ‘Select * from [Device$]’)
SELECT * FROM OPENQUERY (XLSX_MATRIX, ‘Select * from [User$]’)
SELECT * FROM XLSX_MATRIX…[Application$]
SELECT * FROM XLSX_MATRIX…[Device$]
SELECT * FROM XLSX_MATRIX…[User$]
select b.*,a.item_no,a.ref_no from [XLSX_MATRIX]…[工作表1$] a right outter join [XLSX_MATRIX]…[‘原料(B倉)$’] b on a.f_2=b.f_2 where (b.ref_no like ‘%’+a.ref_no+’%’ or (a.ref_no) is null)
order by 1
select b.*,a.item_no,a.ref_no from [XLSX_MATRIX]…[‘原料(B倉)$’] b left outer join [XLSX_MATRIX]…[工作表1$] a on a.f_2=b.f_2
where (b.ref_no like ‘%’+a.ref_no+’%’ or (a.ref_no) is null) and sort_id is not null
order by sort_id
final**
select distinct b.*,a.item_no,a.ref_no from [XLSX_MATRIX]…[‘原料(B倉)$’] b left outer join [XLSX_MATRIX]…[工作表1$] a on a.f_2=b.f_2
and (b.ref_no like ‘%’+a.ref_no+’%’ or (a.ref_no) is null) where sort_id is not null
order by sort_id
select distinct b.*,a.item_no,a.ref_no,isnull(a.v_no,a.t_no) from [XLSX_MATRIX]…[‘原料(B倉)$’] b left outer join [XLSX_MATRIX]…[工作表1$] a on isnull(a.v_no,a.t_no)=b.f_2
and (b.ref_no like ‘%’+a.ref_no+’%’ or (a.ref_no) is null) where sort_id is not null
order by sort_id
AccessDatabaseEngine_X64 maybe needed for Microsoft.ACE.OLEDB.12.0 driver Microsoft Access Database Engine 2010 可轉散發套件
p.s.使用openrowset 可直接讀excel檔案,不用設置linked server
select a.* from
(SELECT * FROM OpenRowSet(‘Microsoft.ACE.OLEDB.12.0’,
‘Excel 12.0;HDR=Yes;IMEX=2;Database=c:\temp\Employee.xls’,[工作表1$])) a,
(SELECT * FROM OpenRowSet(‘Microsoft.ACE.OLEDB.12.0’,
‘Excel 12.0;HDR=Yes;IMEX=2;Database=c:\temp\Employee.xls’,[工作表1$])) b
where a.col1 = b.col1
select distinct b.*,a.item_no,a.ref_no,isnull(a.v_no,a.t_no) from
(SELECT * FROM OpenRowSet(‘Microsoft.ACE.OLEDB.12.0’,
‘Excel 12.0;HDR=Yes;IMEX=1;Database=D:\SQL\vn_item_no.xlsx’,[‘原料(B倉)$’])) b left outer join
(SELECT * FROM OpenRowSet(‘Microsoft.ACE.OLEDB.12.0’,
‘Excel 12.0;HDR=Yes;IMEX=1;Database=D:\SQL\vn_item_no.xlsx’,[工作表1$])) a on isnull(a.v_no,a.t_no)=b.f_2
and (b.ref_no like ‘%’+a.ref_no+’%’ or (a.ref_no) is null) where sort_id is not null
order by sort_id
(註1)SQL Server預設不允許使用 OPENROWSET 和 OPENDATASOURCE 進行特定分散式查詢。使用 OpenRowSet前,需使用sp_configure 來啟用 ‘Ad Hoc Distributed Queries’
EXEC sp_configure 'show advanced options','1' RECONFIGURE EXEC sp_configure 'Ad Hoc Distributed Queries',1 RECONFIGURE

參照
Import data from Excel to SQL Server or Azure SQL Database
如何跳過 Win11 強制連網?
1.在連到網路的頁面,不要連上網路,也把實體網路線拔除,這時候按下鍵盤上的「Shift + F10」就可以開啟 CMD 視窗,如果按下去沒有反應,請試試看「fn + Shift + F10」組合,應該就可以順利呼叫開啟 CMD。
2.開啟之後輸入「oobe\bypassnro」,並按下 Enter 鍵。之後系統會自動重新啟動,重開機後會再進入 OOBE 畫面一次,設定都會重新來過,請依照自己需求設定。

4.到連接網路畫面時,就可以看到「我沒有網際網路」的選項,點選後就可以跳過連網!

SQL語法針對 BOM減少一階 後做用料量彙總
–bom2要被減少的階層 此處用量皆為重量,算出總重量,屆時可依比例更新用量回主BOM,即bom1
update Cf_inv_bom_2
set Cf_inv_bom_2.sum_qty = t1.c_sum
from (select assy_no,sum(qty) c_sum from Cf_inv_bom_2 group by assy_no) t1
where Cf_inv_bom_2.assy_no = t1.assy_no
–bom1無下階先抓出來
select * from cf_inv_bom_1
where item_no not in (select assy_no from cf_inv_bom_2 )
–再結合有bom1有下階的資料(用量=>被替換子件原用量*(下階子件用量/下階用量之和))
select * from cf_inv_bom_1
where item_no not in (select assy_no from cf_inv_bom_2 )
union all
select T1.assy_no,T2.item_no,CAST(t1.qty*t2.qty/t2.sum_qty AS DECIMAL(10,4) ) d_qty,T1.sum_qty
from cf_inv_bom_2 T2,cf_inv_bom_1 T1
where T2.assy_no=T1.item_no order by 1,2
–由於合併後發現兩階間有共用子件因此多一層group來加總資件用量
select Tu.assy_no,tu.item_no,sum(tu.qty),max(Tu.sum_qty) from
(select [assy_no]
,[item_no]
,[qty]
,[sum_qty] from cf_inv_bom_1
where item_no not in (select assy_no from cf_inv_bom_2 )
union all
select T1.assy_no,T2.item_no,CAST(t1.qty*t2.qty/t2.sum_qty AS DECIMAL(10,4) ) d_qty,T1.sum_qty
from cf_inv_bom_2 T2,cf_inv_bom_1 T1
where T2.assy_no=T1.item_no) as Tu
group by Tu.assy_no,tu.item_no
order by 1,2
Excel VBA 迴圈抓取每三欄轉置成多筆資料 用array寫出
Sub LoopThroughRowsByRefBom()
LastRow = Range("A" & Rows.Count).End(xlUp).Row
FirstRow = 1
i = FirstRow
FirstColumn = 1
Do Until i > LastRow
LastColumn = Cells(i, Columns.Count).End(xlToLeft).Column
j = FirstColumn
Do Until j > LastColumn
If i Mod 15 = 1 And j Mod 3 = 1 Then
For k = 0 To 10
Cells(i + k, j + 2).Value = Cells(i, j)
Next k
End If
j = j + 3
Loop
i = i + 15
Loop
Dim dynArray(1 To 5000, 1 To 4) As String
i = 1
k = 1
Do Until i > LastRow
LastColumn = Cells(i, Columns.Count).End(xlToLeft).Column
j = 1
Do Until j > LastColumn
If j Mod 3 = 1 And i Mod 15 <= 10 And Not IsEmpty(Cells(i, j + 1).Value) And Cells(i, j + 1).Value > 0 Then
dynArray(k, 1) = Cells(i, j + 2)
dynArray(k, 2) = Cells((i \ 15) * 15 + 12, j + 1)
dynArray(k, 3) = Cells(i, j)
dynArray(k, 4) = Cells(i, j + 1)
k = k + 1
End If
j = j + 3
Loop
i = i + 1
Loop
Dim De1 As Range
Set De1 = Range("AK1")
De1.Resize(UBound(dynArray, 1), UBound(dynArray, 2)).Value = dynArray
End Sub
印表機分享異常處理
連接win 10印表機出現0x0000011b錯誤解決方法 fix-0x0000011b
[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Print]
“RpcAuthnLevelPrivacyEnabled”=dword:00000000
解決Win10-1709版更新後無法存取網路芳鄰分享的共用資料夾\HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\LanmanWorkstation\Parameters
add dword
AllowInsecureGuestAuth=1
Win10 關閉 UAC
HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Policies\ System
DWORD 值
EnableLUA=0
[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Print]
“RpcAuthnLevelPrivacyEnabled”=dword:00000001
列印伺服器
只要將下列機碼中的內容匯出就 ok 了,日後新電腦只要重新匯入 3 秒鐘就可搞定。
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Forms
新增資料夾_==>命名為 “Printers.{2227A280-3AEA-1069-A2DE-08002B30309D}”可顯示所有印表機
https://www.dhb-scripting.com/Forums/posts/t46-Windows-10-Printers-Not-Showing-Up
其他可用系統資料夾
Default Programs. {17cd9488-1228-4b2f-88ce-4298e93e0966}
MyComputer. {20D04FE0-3AEA-1069-A2D8-08002B30309D}
Network. {208D2C60-3AEA-1069-A2D7-08002B30309D}
All Networks For Current Connection. {1FA9085F-25A2-489B-85D4-86326EEDCD87}
Programs and Features. {15eae92e-f17a-4431-9f28-805e482dafd4}
Power Settings. {025A5937-A6BE-4686-A844-36FE4BEC8B6D}
Printers. {2227A280-3AEA-1069-A2DE-08002B30309D}
Icons And Notifications. {05d7b0f4-2121-4eff-bf6b-ed3f69b894d9}
Firewall and Security. {4026492F-2F69-46B8-B9BF-5654FC07E423}
All NET Frameworks and COM Libraries. {1D2680C9-0E2A-469d-B787-065558BC7D43}
Application Connections. {241D7C96-F8BF-4F85-B01F-E2B043341A4B}
Administrative Tools. {D20EA4E1-3957-11d2-A40B-0C5020524153}
Credentials and Logins. {1206F5F1-0569-412C-8FEC-3204630DFB70}
Speech Recognition. {58E3C745-D971-4081-9034-86E34B30836A}
Oracle免費主機架設LAMP 跑WordPress
Oracle cloud infrastructure + Freenom + Cloudflare =>Free VPS + DNS + CDN
L: ubuntu 20.04 A:apache 2 M: mySQL P: PHP LAMP Stack setup – Linux Apache MySQL PHP https://www.cloudsigma.com/lamp-stack-setup-linux-apache-mysql-php/
How to Install WordPress with LAMP on Ubuntu 20.04 https://www.cloudsigma.com/how-to-install-wordpress-with-lamp-on-ubuntu-20-04/
How To Secure Apache with Let’s Encrypt on Ubuntu 22.04 https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-22-04