- 帖子
- 1447
- 主題
- 40
- 精華
- 0
- 積分
- 1471
- 點名
- 0
- 作業系統
- Windows 7
- 軟體版本
- Excel 2010 & 2016
- 閱讀權限
- 50
- 性別
- 男
- 來自
- 台灣
- 註冊時間
- 2020-7-15
- 最後登錄
- 2025-3-24
|
3#
發表於 2023-3-9 13:10
| 只看該作者
本帖最後由 Andy2483 於 2023-3-9 13:16 編輯
回復 1# 我肥人
謝謝前輩發表此主題與範例
後學練習陣列以輔助表的方式 做資料 修改 作業的解決方案,請前輩參考
執行方式:
1.選取資料表其中一列或一格後按修改鈕:
1.1.執行結果:
2.修改資料後請切換回資料表 "Worksheet":
3.完成修改:
form_20230309_1.zip (40.77 KB)
Option Explicit
Sub 編輯修改模式()
Dim Arr, Brr, R&
Application.DisplayAlerts = False
If Selection.Rows.Count > 1 Then MsgBox "每次只能修改一列資料": Exit Sub
If Selection.Row <= 6 Or Cells(Selection.Row, 1) = "" Then
MsgBox "先選取修改列": Exit Sub
End If
R = Selection.Row: Arr = [A6:Q6]: Brr = Range(Cells(R, "A"), Cells(R, "Q"))
Rows(R).Font.ColorIndex = 1
On Error Resume Next
Sheets("Modify").Delete
On Error GoTo 0
Worksheets.Add(After:=Sheets(Sheets.Count)).Name = "Modify"
[A2].Resize(UBound(Arr, 2), 1) = Application.Transpose(Arr)
[A2].Resize(UBound(Arr, 2), 1).Interior.ColorIndex = 35
[B2].Resize(UBound(Brr, 2), 1) = Application.Transpose(Brr)
[A1] = "項目": [B1] = "原值": [C1] = "新值"
Cells.Font.Size = 14
[A:B].EntireColumn.AutoFit
[C:C].ColumnWidth = [B:B].ColumnWidth * 2
ActiveSheet.UsedRange.Borders.LineStyle = xlContinuous
[D1] = R
ActiveSheet.Protection.AllowEditRanges.Add Title:="範圍1", Range:=[C2].Resize(UBound(Brr, 2), 1)
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="0000"
Sheets("Worksheet").Protect DrawingObjects:=True, Contents:=True, Scenarios:=True, Password:="0000"
End Sub
=============================================
Sub 修改資料帶入資料表()
Dim Arr, R&, i&
If Sheets("Modify").Cells(Rows.Count, "C").End(3).Row = 1 Then MsgBox "沒有修改": Exit Sub
Arr = Sheets("Modify").UsedRange
R = Sheets("Modify").[D1]
For i = 2 To UBound(Arr)
If Trim(Arr(i, 3)) <> "" Then
Sheets("Worksheet").Cells(R, i - 1) = Trim(Arr(i, 3))
Sheets("Worksheet").Cells(R, i - 1).Font.ColorIndex = 5
End If
Next
End Sub
======================================
Private Sub Worksheet_Activate()
ActiveSheet.Unprotect "0000"
Call 修改資料帶入資料表
Application.DisplayAlerts = False
Sheets("Modify").Delete
End Sub |
|