返回列表 上一主題 發帖

[發問] 儲存格索引值

Sub 測試()
Dim P$, S$, T$, R$, F$, N&, xA As Range, xB As Workbook, xS As Worksheet
P = [B6]: If P = "" Then MsgBox "**子資料夾未輸入! ": Exit Sub
P = ThisWorkbook.Path & "\" & P & "\"
If Dir(P, vbDirectory) = "" Then MsgBox "**子資料夾不存在! ": Exit Sub
S = [B2]: If S = "" Then MsgBox "**工作表名稱未輸入! ": Exit Sub
T = [B4].Text: If S = "" Then MsgBox "**寫入的內容未輸入! ": Exit Sub
R = [B5]: If R = "" Then MsgBox "**寫入位址未輸入! ": Exit Sub
On Error Resume Next: Set xA = Range(R): On Error GoTo 0
If xA Is Nothing Then MsgBox "**寫入位址不正確! ": Exit Sub
If xA.Count > 1 Then MsgBox "**寫入位址不是單一儲存格! ": Exit Sub
'--------------------------------------------------------------------
[F1:H999].ClearContents:  Set xA = [F1]
Application.ScreenUpdating = False
Do
   If F = "" Then F = Dir(P & "*.xls") Else F = Dir()  'xls 自行改成 xlsx  
   If F = "" Then Exit Do
   N = N + 1: xA = N: xA(1, 2) = F: xA(1, 3) = "(X)工作表不存在"
   Set xB = Workbooks.Open(P & F):  Set xS = Nothing
   On Error Resume Next: Set xS = xB.Sheets(S): On Error GoTo 0
   If Not xS Is Nothing Then xS.Range(R) = T: xB.Save: xA(1, 3) = "(V)寫入完成"
   Set xA = xA(2):  xB.Close 0
Loop
End Sub

測試(1).rar (23.65 KB)


>>>>

TOP

回復 11# PJChen


Sub 測試()
Dim P$, S$, T$, R$, F$, N&, xB As Workbook, xS As Worksheet
P = [B6]: If P = "" Then MsgBox "**資料夾名稱,子資料夾未輸入! ": Exit Sub
P = ThisWorkbook.Path & "\" & P & "\"
If Dir(P, vbDirectory) = "" Then MsgBox "**子資料夾不存在! ": Exit Sub
S = [B2]: If S = "" Then MsgBox "**工作表名稱未輸入! ": Exit Sub
T = [B4].Text: If S = "" Then MsgBox "**寫入的內容未輸入! ": Exit Sub
R = [B5]: If R = "" Then MsgBox "**寫入位址未輸入! ": Exit Sub
On Error Resume Next: Set xA = Range(R): On Error GoTo 0
If xA Is Nothing Then MsgBox "**寫入位址不正確! ": Exit Sub
If xA.Count > 1 Then MsgBox "**寫入位址不是單一儲存格! ": Exit Sub
'--------------------------------------------------------------------
Dim xD, A
Set xD = CreateObject("Scripting.Dictionary")
Do
   If F = "" Then F = Dir(P & "*.xlsx") Else F = Dir()
   If F = "" Then Exit Do Else xD(F) = ""
Loop
If xD.Count = 0 Then Exit Sub
Application.ScreenUpdating = False
For Each A In xD.keys
    Set xB = Workbooks.Open(P & A):  Set xS = Nothing
    On Error Resume Next: Set xS = xB.Sheets(S): On Error GoTo 0
    If Not xS Is Nothing Then xS.Range(R) = T: xB.Save
    xB.Close 0
Next
End Sub

===================

TOP

回復 13# PJChen


Path = "W:\私\範例\"
P = Path & "\" & P & "\"

紅色部份重覆兩個"\",  刪掉其中一個~~~

TOP

        靜思自在 : 要比誰更受誰.不要比誰更怕誰。
返回列表 上一主題