Lấy path tương đối ?

Liên hệ QC

vumian

Mỗi bậc thang là mỗi Cell
Tham gia
12/3/07
Bài viết
267
Được thích
186
Nghề nghiệp
employee only, not a boss
hi all,

Trong VB6, application.path thì lấy được đường dẫn của file hiện tại, nhưng trong VBA, code này cứ lấy đường dẫn của microsoft

Public sub Mo_File()
p = Me.Application.Path & "\abc.xls"
Workbooks.Open (p)
end sub

cho nên file abc.xls sẽ hông tồn tại, nhưng thực ra nó đang nằm cùng lớp folder với file đang dùng để mở nó

Cám on nhieu
 
Bác phải dùng câu lệnh
thisworkbook.path

thân.
 
hhihi, sao mà đầu hôg nghĩ ra ,thiệt là :p,

Soibien oi, làm sao không cần mở abc.xls mà mình vẫn copy toàn nội dung sheet1 duoc

thanks SOiBien nha
 
Lần chỉnh sửa cuối:
mÌNH NHỚ LÀ CÓ BÀI HƯỚNG DẪN CHI TIẾT TRÊN 4R RỒI. Ở ĐÂU THÌ CHƯA NHỚ, BÁC TÌM THỬ XEM NHÉ.

tHÂN
 
Để lấy Data từ một sheet ko mở. Bác có thể dụng Công cụ Database Query.
Tham khảo tại đây
Hay dùng chức năng Import Data
Tham khảo tại đây
 
ontopoftheworld đã viết:
Để lấy Data từ một sheet ko mở. Bác có thể dụng Công cụ Database Query.
Tham khảo tại đây
Hay dùng chức năng Import Data
Tham khảo tại đây
Bạn đã thử chưa, liệu có nhanh hơn mở 1 file ra copy vào tmp và đóng lại.
Cám ơn nhiều.
 
ThuNghi đã viết:
Bạn đã thử chưa, liệu có nhanh hơn mở 1 file ra copy vào tmp và đóng lại.
Cám ơn nhiều.
Vâng, Tất nhiên là em đang sử dụng Ms Query đấy chứ. Tuy rằng nó có nhược điễm là fải thao tác nhiều lần, khi thay đồi điều kiện lấy data về thì phải vào MS Query để thay đổi điều kiện.

Ở đây, do evumian muốn lấy Data từ một sheet của file không mở thì em đưa ra một sự lựa chọn trong nhiều sự lựa chọn có thể. Tuy theo khả năng của vumian và hiệu quả của công cụ này mang lại cho vumian. Ai có gì sài nấy mà anh.
 
Lần chỉnh sửa cuối:
vumian đã viết:
hhihi, sao mà đầu hôg nghĩ ra ,thiệt là :p,

Soibien oi, làm sao không cần mở abc.xls mà mình vẫn copy toàn nội dung sheet1 duoc

thanks SOiBien nha
From j-walk
Mã:
 Option Explicit


Private Function GetValue(path, file, sheet, ref)
'   Retrieves a value from a closed workbook
'path:  The drive and path to the closed file (e.g., "d:\files")
'file:  The workbook name (e.g., "99budget.xls")
'sheet: The worksheet name (e.g., "Sheet1")
'ref:   The cell reference (e.g., "C4")

    Dim arg As String

'   Make sure the file exists
    If Right(path, 1) <> "\" Then path = path & "\"
    If Dir(path & file) = "" Then
        GetValue = "File Not Found"
        Exit Function
    End If

'   Create the argument
    arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
      Range(ref).Range("A1").Address(, , xlR1C1)

'   Execute an XLM macro
    GetValue = ExecuteExcel4Macro(arg)
End Function
Sub TestGetValue()
'D:\PTC  CloseIniFileToVBA.xls Sheet1 B3

Dim p As String, f As String, s As String, a As String
    p = "D:\PTC"
    f = "CloseIniFileToVBA.xls"
    s = "Sheet1"
    a = "B3"
    MsgBox GetValue(p, f, s, a)
End Sub
Sub TestGetValue2()
 'reads 1,200 values (100 rows and 12 columns) from a closed file, and places the values into the active worksheet.
    Dim p As String, f As String, s As String, a As String
    Dim r As Integer, c As Integer
    'hay lam , dung file de dua may cai duong dan, ten file nay vao 18Oct2007
    p = "D:\PTC"
    f = "CloseIniFileToVBA.xls"
    s = "Sheet1"
    Application.ScreenUpdating = False
    For r = 1 To 100
        For c = 1 To 12
            a = Cells(r, c).Address
            Cells(r, c) = GetValue(p, f, s, a)
        Next c
    Next r
    Application.ScreenUpdating = True
End Sub
Sub thu()
Dim ref
'Range("A1").Address(, , xlR1C1) 'bi loi
'ref = Range("b1").Range("A1").Address(, , xlR1C1) '  : ref : "R1C2" : Variant/String
ref = Range("A1").Address(, , xlR1C1) '  : ref : "R1C1" : Variant/String
End Sub
 
ThuNghi đã viết:
Bạn đã thử chưa, liệu có nhanh hơn mở 1 file ra copy vào tmp và đóng lại.
Cám ơn nhiều.
Theo kiểu của bác Thu Nghi đây
Mã:
 Option Explicit
    Dim destWB As Workbook, sourceWB As Workbook
    'Dim destWk As Worksheet, sourceWk As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim intRow As Integer
    Dim smallrng As Range
    'Public Const SWbName As String = "CloseIniFileToVBA.xls" 'to all procedures in all modules. Not allowed in procedures
    'de no cung kg dong duoc ini file ???
    Public Const DongFlag As String = "A12"
Sub copy_to_another_workbook() ' Area to Range lien tuc, chep tung cai, bang resize
Dim SWbName As String
SWbName = "HDGTGTToData_IniFile.xls"
Dim WbIniFile As Workbook
    'Dim Lr As Long
     Application.ScreenUpdating = False
     MsgBox "THAY doi o Public const khi chay so hoa don moi, kiem tra khi on dinh roi se dua vao vong lap"
     
     'Lay ten source va destination,sau do dong lai
    If bIsBookOpen(SWbName) Then
     Set WbIniFile = Workbooks(SWbName)
   Else
       Set WbIniFile = Workbooks.Open(ThisWorkbook.Path & "\" & SWbName)
   End If
      With WbIniFile.Worksheets("Sheet1")
        intRow = .Range(DongFlag).Row
         If bIsBookOpen(.Cells(intRow, 2)) Then
            Set sourceWB = Nothing
            Set sourceWB = Workbooks(.Cells(intRow, 2)) ' bi loi o day neu da mo va chua dung nothing,dung cho lan 2
        Else
            Set sourceWB = Workbooks.Open(Left(ThisWorkbook.Path, 3) & "PTC" & "\" & .Cells(intRow, 6) & "\" & .Cells(intRow, 2))
            'Set sourceWk = Workbooks(.Cells(intRow, 2)).Worksheets(.Cells(intRow, 3)) khg the dung wk duoc, tot nhat cu theo cai co san da
         End If
         
        If bIsBookOpen(.Cells(intRow, 7)) Then
            Set destWB = Workbooks(.Cells(intRow, 7))
        Else
            Set destWB = Workbooks.Open(Left(ThisWorkbook.Path, 3) & "PTC" & "\" & .Cells(intRow, 11) & "\" & .Cells(intRow, 7))
        End If
     End With
      WbIniFile.Close True
   Set WbIniFile = Nothing
   
   ' Chep sang tabe1 cua data
   'Windows("2007HDGTGT_Data.xls").Activate
   Dim LastPlc As Integer, i As Integer
   i = 1
   With destWB 'dung with qua hay va thuan tien,nhung hay bat dau bang Activate de code chay on ding da
        LastPlc = Application.CountA(.Worksheets("Data").Range("a:a")) + 1  'Find last cell/row plus one
   End With
    For Each smallrng In sourceWB.Worksheets("Sheet1").Range("e2,e3,i4,i12,i15").Areas
        'With smallrng
         Set destrange = Worksheets("Data").Cells(LastPlc, i)
        'End With
        destrange.Value = smallrng.Value
        i = i + 1
    Next smallrng

'' Chep sang tabe2 cua data
Dim SoDong As Integer, LastPlc2 As Integer
Dim DataCount As Range
Set DataCount = Worksheets("Data").Range("g:g")
LastPlc2 = Application.CountA(DataCount) + 1 'kieu nay hay hon xlDown phai co min 2 cell, nhung phai chon duoc vung dem co dinh
'sourceWB.Worksheets("Sheet1").Activate
'Mot cai do cua xlDown la vung data phai co it nhat 2 Cells thi moi dung, do do doi a19->17, nen doc them NotEmpty cho nay
With sourceWB.Worksheets("Sheet1")
    SoDong = Application.WorksheetFunction.CountA(.Range("A17:A" & .Range("a18").End(xlDown).Row)) - 2
End With

'destWB.Worksheets("Data").Activate
i = 0
 With Worksheets("Data")
    For i = 0 To SoDong - 1
    'Dim i2 As Integer
    'i2 = 7
    'For Each smallrng In sourceWB.Worksheets("Sheet1").Range("e3")
    '.Cells(LastPlc2, i2) = smallrng
   ' i2 = i2 + 1
    
    .Cells(LastPlc2, 7) = sourceWB.Worksheets("Sheet1").Range("e3")
    .Cells(LastPlc2, 8) = sourceWB.Worksheets("Sheet1").Range("a19").Offset(i, 0)
   .Cells(LastPlc2, 9) = sourceWB.Worksheets("Sheet1").Range("b19").Offset(i, 0)
   .Cells(LastPlc2, 9).WrapText = False
   .Cells(LastPlc2, 10) = sourceWB.Worksheets("Sheet1").Range("c19").Offset(i, 0)
   .Cells(LastPlc2, 11) = sourceWB.Worksheets("Sheet1").Range("d19").Offset(i, 0)
    .Cells(LastPlc2, 12) = sourceWB.Worksheets("Sheet1").Range("e19").Offset(i, 0)
    
   ' Next smallrng
    
     LastPlc2 = Application.CountA(DataCount) + 1
    Next
End With

sourceWB.Close True
'destWB.Close True
Set sourceWB = Nothing
'Set destWB = Nothing
CloseMe
End Sub
Sub CloseMe()
ThisWorkbook.Close True
End Sub
 
Dim m As String
m = InputBox("pls Input No ?", "Requirement")
'MsgBox (m)
P = ThisWorkbook.Path & "\file - " & m & ".xls"
'MsgBox (P)
On Error GoTo loi
Application.Windows(P).Activate
Workbooks.Open (P)
'Call FunctionHere
MsgBox "Sucessfully open"
Exit Sub
loi:
MsgBox "Sai ten hoac khong ton tai", vbCritical, "thongbaoloi"
End Sub

- Giả sử mình có 3 file như sau
abc file - No.xls
cde file - No.xls
gfh file - No.xls

Làm sao mở duoc 3 file trên
Cam on
 
Lần chỉnh sửa cuối:
Đảo thứ tự 2 cái này thử xem bạn.
Application.Windows(P).Activate
Workbooks.Open (P)
 
Hông duoc,hix, mà mình active nó rùi mới mở nó chứ, vậy mình thấy hợp lý hơn,

Cam on
 
Lần chỉnh sửa cuối:
bác bỏ bẫy lỗi đi (On error) rồi xem nó báo lỗi dòng nào.
còn bác phải open nó rồi mới active được chứ.
 
P = ThisWorkbook.Path & "\abc file- " & m & ".xls"

Cái dòng trên mình kết hợp vậy, mà nó báo là "Object vaỉable or with block variable not set"

Khắc phục chỗ này sao đây bác
 
Lần chỉnh sửa cuối:
Chắc bạn phải up 2 file đó lên quá, chứ mình thử đoạn code trên bỏ cái activate đi thì nó vẫn open như thường.
 
vumian đã viết:
Hông duoc,hix, mà mình active nó rùi mới mở nó chứ, vậy mình thấy hợp lý hơn,

Cam on

Có mở cửa thì mới vào nhà được, có mở File đó ra thì mới active được chứ ??

Thân!
 
hi, mình đã mờ được rồi,Bỏ active luôn là lấy tiền, nhưng giờ mình muốn mở 3 file cùng lúc, với điều kiện là tên đầu fie lần lựot là
aa file m.xls
bb file m.xls
cc file m.xls

m là số inputbox okê rùi, vậy còn aa,bb,cc làm sao cho nó vào đoạn code trên duợc,
Ỳ mình là hôg muốn kết hợp thêm aa, hay bb, hay cc vào trước filenam, nếu vậy sẽ có 3 dòng, hợi dở :p
Cam on
 
Lần chỉnh sửa cuối:
vậy thì bạn open 3 lần thôi.
 
Open 3 lần hông hay, mình muốn dùng

Dim mang(aa,bb,cc) as string

rùi ghép mang(0), manng(1), mang(2) với cái filename đó, và ghétp với m

Hay có cách nào đại loại như vậy

Thanks
 
Lần chỉnh sửa cuối:
Web KT
Back
Top Bottom