Đường dẫn thư mục chứa tài liệu nguồn

Liên hệ QC

hic1802

Thành viên tiêu biểu
Tham gia
16/2/13
Bài viết
545
Được thích
34
Giới tính
Nam
Xin chào mọi người trên GPE,
Nhờ mọi người hướng dẫn mình về đường dẫn thư mục chứa tài liệu nguồn
Bình thường mình thấy nếu các file làm việc và file nguồn chung 1 folder thì nó là dạng ThisWorkbook.Path
Nếu file nguồn không nằm cùng thư mục thì như thế nào nhỉ?
Mình có search google : bằng cách đặt biến : spart = "link" --> chạy lệnh open file báo lỗi, báo lỗi ở dòng : Set Wb = Workbooks.Open(spart & "\" & WbName)
Nhờ mọi người giúp đỡ

Mã:
Sub CapNhat()
Dim Wb As Workbook, WbName As String, WsName As String, Darr(), LastR As Long, i As Long
Dim spart As String
spart = "\\192.168.14.1\HongLam\6. KHO"

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
For j = 1 To 10
  i = Cells(Rows.Count, j).End(xlUp).Row
  If i > LastR Then LastR = i
Next j
Darr = Range("A2:J" & LastR).Value

WsName = ThisWorkbook.Name
WsName = Left(WsName, Len(WsName) - 5)
WbName = "Data_N.xlsx"
If Test(WbName) Then
  Set Wb = Workbooks(WbName)
Else
  'Set Wb = Workbooks.Open(ThisWorkbook.Path & "\" & WbName)
  Set Wb = Workbooks.Open(spart & "\" & WbName)
End If
With Wb.Sheets(WsName)
  .Range("A2:A9999").ClearContents
  .Range("A2").Resize(UBound(Darr), 10) = Darr

End With
Wb.Close True
ActiveWorkbook.Save
MsgBox "Xong!"
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
 
Xin chào mọi người trên GPE,
Nhờ mọi người hướng dẫn mình về đường dẫn thư mục chứa tài liệu nguồn
Bình thường mình thấy nếu các file làm việc và file nguồn chung 1 folder thì nó là dạng ThisWorkbook.Path
Nếu file nguồn không nằm cùng thư mục thì như thế nào nhỉ?
Mình có search google : bằng cách đặt biến : spart = "link" --> chạy lệnh open file báo lỗi, báo lỗi ở dòng : Set Wb = Workbooks.Open(spart & "\" & WbName)
Nhờ mọi người giúp đỡ

Mã:
Sub CapNhat()
Dim Wb As Workbook, WbName As String, WsName As String, Darr(), LastR As Long, i As Long
Dim spart As String
spart = "\\192.168.14.1\HongLam\6. KHO"

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
For j = 1 To 10
  i = Cells(Rows.Count, j).End(xlUp).Row
  If i > LastR Then LastR = i
Next j
Darr = Range("A2:J" & LastR).Value

WsName = ThisWorkbook.Name
WsName = Left(WsName, Len(WsName) - 5)
WbName = "Data_N.xlsx"
If Test(WbName) Then
  Set Wb = Workbooks(WbName)
Else
  'Set Wb = Workbooks.Open(ThisWorkbook.Path & "\" & WbName)
  Set Wb = Workbooks.Open(spart & "\" & WbName)
End If
With Wb.Sheets(WsName)
  .Range("A2:A9999").ClearContents
  .Range("A2").Resize(UBound(Darr), 10) = Darr

End With
Wb.Close True
ActiveWorkbook.Save
MsgBox "Xong!"
Application.AskToUpdateLinks = True
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
Đường dẫn kiểu FTP đó sao code chạy được bạn. Nếu file kia khác với file chứa code thì phải chỉ rõ ra là D:\FolderXXX\SubFolderYYY\ thì mới chạy được chứ
 
Cách dễ nhất là "Map Network Drive" ra một drive gì đó. G, H chẳng hạn.
 
Web KT
Back
Top Bottom