Giúp update modules VBA (1 người xem)

Liên hệ QC

Người dùng đang xem chủ đề này

tueyennhi

Thành viên tích cực
Tham gia
18/10/10
Bài viết
1,192
Được thích
105
Chào anh chị!

Em đang có một vấn đề đau đầu như sau:

Khi xây dựng Modules tính công, theo thời gian em thấy được nhiều điểm cần phải chỉnh sửa. Tuy nhiên mỗi lần chỉnh sửa như thế em lại phải hỗ trợ bộ phận update các Modules mới. Như vậy thật phức tạp và không khoa học cho lắm. Giả sử em có 10 cái modules thì em phải gỡ ra toàn bộ sau đó insert 10 modules chỉnh sửa vào.

Vậy có cách nào mà biến tất cả các modules này thành một file để ở ổ nào đó mà sau này mỗi lần thay đổi bộ phận chỉ cần tải file mới này vào và lưu vào địa chỉ file cũ không ạ?
 
Thử cách sau xem:

- Export module đã chỉnh sửa thành file.bas (1)
- Ở file excel cần cập nhật code thì: xóa module cũ rồi import module ở (1) vào, lưu file excel.

Cách thức làm có thể thao tác tay hoặc code.
 
Upvote 0
Thử cách sau xem:

- Export module đã chỉnh sửa thành file.bas (1)
- Ở file excel cần cập nhật code thì: xóa module cũ rồi import module ở (1) vào, lưu file excel.

Cách thức làm có thể thao tác tay hoặc code.

Ừm hiện tại mình đang làm như thế mà thấy bất tiện quá. Hình như có cách chuyển sang dạng file dll của anh Nguyenduytuan mà khó quá mình không làm được.

Cách thức làm có thể thao tác tay hoặc code : Code là sao bạn? Mình thử ghi macro thì không thấy gì nếu thực hiện thac tác import trên.
 
Lần chỉnh sửa cuối:
Upvote 0
Ừm hiện tại mình đang làm như thế mà thấy bất tiện quá. Hình như có cách chuyển sang dạng file dll của anh Nguyenduytuan mà khó quá mình không làm được.

Cách thức làm có thể thao tác tay hoặc code : Code là sao bạn? Mình thử ghi macro thì không thấy gì nếu thực hiện thac tác import trên.

Dùng VB6 thì giải quyết được vấn nạn này nhé. Viết code và tạo file .dll và đưa và hế thống. Làm được nhưng cũng phải vất vả nghiên cứu mới được
 
Upvote 0
Ừm hiện tại mình đang làm như thế mà thấy bất tiện quá. Hình như có cách chuyển sang dạng file dll của anh Nguyenduytuan mà khó quá mình không làm được.

Cách thức làm có thể thao tác tay hoặc code : Code là sao bạn? Mình thử ghi macro thì không thấy gì nếu thực hiện thac tác import trên.

(1) Tham khảo bài viết theo link sau:
http://www.cpearson.com/excel/vbe.aspx

(2) Phần export thì chưa thử.

(3) Phần import
- Xóa các module đã có:
https://www.giaiphapexcel.com/forum/showthread.php?72632-Có-thể-dùng-vba-để-xóa-vba-đc-không
- Import file *.bas (kết quả của (2)):
Thử nghiệm:
Module1_msg:
Mã:
Sub msg()
MsgBox "hi"
End Sub
Module_ImportBas:
Mã:
Sub ImportBas()
Dim wb As Workbook, pth As String
Set wb = ThisWorkbook
pth = "C:\Users\abc\Desktop\Module1_msg.bas" 'là đường dẫn file .bas sau khi export module "Module1_msg"
wb.VBProject.VBComponents.Import (pth)
End Sub
 
Upvote 0
(1) Tham khảo bài viết theo link sau:
http://www.cpearson.com/excel/vbe.aspx

(2) Phần export thì chưa thử.

(3) Phần import
- Xóa các module đã có:
https://www.giaiphapexcel.com/forum/showthread.php?72632-Có-thể-dùng-vba-để-xóa-vba-đc-không
- Import file *.bas (kết quả của (2)):
Thử nghiệm:
Module1_msg:
Mã:
Sub msg()
MsgBox "hi"
End Sub
Module_ImportBas:
Mã:
Sub ImportBas()
Dim wb As Workbook, pth As String
Set wb = ThisWorkbook
pth = "C:\Users\abc\Desktop\Module1_msg.bas" 'là đường dẫn file .bas sau khi export module "Module1_msg"
wb.VBProject.VBComponents.Import (pth)
End Sub

Cách dùng file dll có vẻ chuyên nghiệp hơn nhưng cần ngâm cứu lâu dài. Có lẽ ăn xổi nhất bây giờ thì dùng cách này. Cảm ơn befaint , Cảm ơn anhquanghai1969 ạ!
Có cách nào update toàn bộ modules trong cùng một thư mục mà không phải điền tên từng cái không nhỉ?
 
Lần chỉnh sửa cuối:
Upvote 0

Có cách nào update toàn bộ modules trong cùng một thư mục mà không phải điền tên từng cái không nhỉ?
Tìm đoạn code chọn folder, duyệt các file trong folder đó và lấy các file *.bas => có đường dẫn của các file
Chạy một vòng lặp import các file *.bas theo đường dẫn vừa lấy được.
 
Upvote 0
Néu 1 lần chỉnh sửa mà phải chỉnh sửa nhiều modules thì có các lý do sau:
- Giải thuật chưa tốt, cần phải thiế kế cho thật đúng rồi mới bắt đầu code
- Code chưa tốt, nếu code tốt thì những phần hay chỉnh sửa phải nằm riêng biệt. Và 1 lần chỉnh sửa chỉ phải chỉnh chỗ đó mà thôi.

Có khả năng phải thiết kế và code lại từ đầu.

Có thể do 1 lý do khác: thường thì những code được viết theo kiểu tối ưu về tốc độ thì càng khó chỉnh sửa.
 
Upvote 0
Néu 1 lần chỉnh sửa mà phải chỉnh sửa nhiều modules thì có các lý do sau:
- Giải thuật chưa tốt, cần phải thiế kế cho thật đúng rồi mới bắt đầu code
- Code chưa tốt, nếu code tốt thì những phần hay chỉnh sửa phải nằm riêng biệt. Và 1 lần chỉnh sửa chỉ phải chỉnh chỗ đó mà thôi.

Có khả năng phải thiết kế và code lại từ đầu.

Có thể do 1 lý do khác: thường thì những code được viết theo kiểu tối ưu về tốc độ thì càng khó chỉnh sửa.

À, vì có cái code này của thầy ndu không hiểu sao mình cho vào chung modules thì nó lỗi, để riêng biệt nó thì không lỗi.

PHP:
Option Explicit
Public Chk As Boolean, Dic As Object, aResult()
Sub Auto_Open()
  Dim wks As Worksheet, SrcRng As Range, sArray
  Dim lR As Long, i As Long, n As Long, tmp
  On Error Resume Next
  Set wks = Sheets("Check")
  Set SrcRng = wks.Range("AI3:AK30")
  sArray = SrcRng.Value
  ReDim aResult(1 To UBound(sArray, 1), 1 To UBound(sArray, 2))
  Set Dic = CreateObject("Scripting.Dictionary")
  For i = 1 To UBound(sArray, 1)
    If CStr(sArray(i, 1)) <> "" Then
      tmp = sArray(i, 1)
      If Not Dic.Exists(tmp) Then
        lR = lR + 1
        Dic.Add tmp, lR
        aResult(lR, 1) = tmp
        aResult(lR, 2) = sArray(i, 2)
        aResult(lR, 3) = sArray(i, 3)
      End If
    End If
  Next
End Sub
 
Upvote 0
Chào anh chị!

Em đang có một vấn đề đau đầu như sau:

Khi xây dựng Modules tính công, theo thời gian em thấy được nhiều điểm cần phải chỉnh sửa. Tuy nhiên mỗi lần chỉnh sửa như thế em lại phải hỗ trợ bộ phận update các Modules mới. Như vậy thật phức tạp và không khoa học cho lắm. Giả sử em có 10 cái modules thì em phải gỡ ra toàn bộ sau đó insert 10 modules chỉnh sửa vào.

Vậy có cách nào mà biến tất cả các modules này thành một file để ở ổ nào đó mà sau này mỗi lần thay đổi bộ phận chỉ cần tải file mới này vào và lưu vào địa chỉ file cũ không ạ?
như bạn diễn tả thì hình như là mỗi file bạn đều code chèn một loại module vào, vậy tại sao không dùng Addins.
còn nếu bạn vẫn muốn như cũ . tất cả file đều có module thì bạn tham khảo code sau nhé
nội dung code sẽ xuất các module ra hoặc nạp lại, bạn xem và chỉnh sửa lại như ý nha.
Mã:
Public Sub ExportModules() 'xuat module
    Dim bExport As Boolean
    Dim wkbSource As Excel.Workbook
    Dim szSourceWorkbook As String
    Dim szExportPath As String
    Dim szFileName As String
    Dim cmpComponent As VBIDE.VBComponent
''' The code modules will be exported in a folder named.
''' VBAProjectFiles in the Documents folder.
''' The code below create this folder if it not exist
''' or delete all files in the folder if it exist.
If FolderWithVBAProjectFiles = "Error" Then
    MsgBox "Export Folder not exist"
    Exit Sub
End If
On Error Resume Next
Kill FolderWithVBAProjectFiles & "\*.*"
On Error GoTo 0
''' NOTE: This workbook must be open in Excel.
szSourceWorkbook = ThisWorkbook.Name 'ActiveWorkbook.Name
Set wkbSource = Application.Workbooks(szSourceWorkbook)
If wkbSource.VBProject.Protection = 1 Then
    MsgBox "The VBA in this workbook is protected," & _
            "not possible to export the code"
    Exit Sub
End If
szExportPath = FolderWithVBAProjectFiles & "\"
For Each cmpComponent In wkbSource.VBProject.VBComponents
    bExport = True
    szFileName = cmpComponent.Name
    ''' Concatenate the correct filename for export.
    Select Case cmpComponent.Type
        Case vbext_ct_ClassModule
            szFileName = szFileName & ".cls"
        Case vbext_ct_MSForm
            szFileName = szFileName & ".frm"
        Case vbext_ct_StdModule
            szFileName = szFileName & ".bas"
        Case vbext_ct_Document
            ''' This is a worksheet or workbook object.
            ''' Don't try to export.
            bExport = False
    End Select
    If bExport Then
        ''' Export the component to a text file.
        cmpComponent.Export szExportPath & szFileName
        ''' remove it from the project if you want
        '''wkbSource.VBProject.VBComponents.Remove cmpComponent
    End If
Next cmpComponent
MsgBox "Export is ready"
End Sub
Public Sub ImportModules() 'nhap module
    Dim wkbTarget As Excel.Workbook
    Dim objFSO As Scripting.FileSystemObject
    Dim objFile As Scripting.File
    Dim szTargetWorkbook As String
    Dim szImportPath As String
    Dim szFileName As String
    Dim cmpComponents As VBIDE.VBComponents
    'If ActiveWorkbook.Name = ThisWorkbook.Name Then
    '   MsgBox "Select another destination workbook" & _
    '           "Not possible to import in this workbook "
    '   Exit Sub
    'End If
    'Get the path to the folder with modules
    If FolderWithVBAProjectFiles = "Error" Then
        MsgBox "Import Folder not exist"
        Exit Sub
    End If
    ''' NOTE: This workbook must be open in Excel.
    szTargetWorkbook = ActiveWorkbook.Name
    Set wkbTarget = Application.Workbooks(szTargetWorkbook)
    If wkbTarget.VBProject.Protection = 1 Then
        MsgBox "The VBA in this workbook is protected," & _
                "not possible to Import the code"
        Exit Sub
    End If
    ''' NOTE: Path where the code modules are located.
    szImportPath = FolderWithVBAProjectFiles & "\"
    Set objFSO = New Scripting.FileSystemObject
    If objFSO.GetFolder(szImportPath).Files.Count = 0 Then
        MsgBox "There are no files to import"
        Exit Sub
    End If
    'Delete all modules/Userforms from the ActiveWorkbook
    'Call DeleteVBAModulesAndUserForms
    Set cmpComponents = wkbTarget.VBProject.VBComponents
    ''' Import all the code modules in the specified path
    ''' to the ActiveWorkbook.
    For Each objFile In objFSO.GetFolder(szImportPath).Files
        If (objFSO.GetExtensionName(objFile.Name) = "cls") Or _
                (objFSO.GetExtensionName(objFile.Name) = "frm") Or _
                (objFSO.GetExtensionName(objFile.Name) = "bas") Then
            cmpComponents.Import objFile.Path
        End If
    Next objFile
    MsgBox "Import is ready"
End Sub
Function FolderWithVBAProjectFiles() As String
    Dim WshShell As Object
    Dim fso As Object
    Dim SpecialPath As String
    Set WshShell = CreateObject("WScript.Shell")
    Set fso = CreateObject("scripting.filesystemobject")
    SpecialPath = WshShell.SpecialFolders("MyDocuments")
    If Right(SpecialPath, 1) <> "\" Then
        SpecialPath = SpecialPath & "\"
    End If
    If fso.FolderExists(SpecialPath & "VBAProjectFiles") = False Then
        On Error Resume Next
        MkDir SpecialPath & "VBAProjectFiles"
        On Error GoTo 0
    End If
    If fso.FolderExists(SpecialPath & "VBAProjectFiles") = True Then
        FolderWithVBAProjectFiles = SpecialPath & "VBAProjectFiles"
    Else
        FolderWithVBAProjectFiles = "Error"
    End If
End Function
Function DeleteVBAModulesAndUserForms()
    Dim VBProj As VBIDE.VBProject
    Dim VBComp As VBIDE.VBComponent
    Set VBProj = ActiveWorkbook.VBProject
    For Each VBComp In VBProj.VBComponents
        If VBComp.Type = vbext_ct_Document Then
            'Thisworkbook or worksheet module
            'We do nothing
        Else
            VBProj.VBComponents.Remove VBComp
        End If
    Next VBComp
End Function
nguồn: http://www.rondebruin.nl/win/s9/win002.htm
 
Upvote 0
Upvote 0
PHP:
Sub ImportFiles()
    Dim FileS As FileSearch
    Set FileS = Application.FileSearch
    With FileS
        .NewSearch
        .Filename = "*.bas"
        .LookIn = "C:\"
        .SearchSubFolders = False
        .Execute
    End With
    
    Dim F As Variant
    For Each F In Application.FileSearch.FoundFiles
        ActiveWorkbook.VBProject.VBComponents.Import F
    Next F
End Sub

Mình sưu tầm được code này trên diễn đàn của minhtu chạy báo lỗi ở dòng Set FileS = Application.FileSearch. :(
 
Upvote 0
có cách gọi macro từ 1 file khác đó bác , mình làm chung hàm macro cho 1 file rồi các file client gọi cũng đc đó !

Application.Run "'d:\thu_vien.xls'!ham_macro", "tham_so"
ví dụ như file b gọi macro từ file a .
giải nén vào d:\ là chạy
http://www.mediafire.com/file/bcy5pbcs1ryhlsi/example.7z
 
Lần chỉnh sửa cuối:
Upvote 0
Con mới sinh nên nhìn đau đầu hoa mắt quá befaint ạ, mọi người ạ. Chắc phải mấy ngày sau mới có thể tìm hiểu :(
 
Upvote 0
hình như hàm FileSearch chỉ xài cho office 2003 hay sao ý

Thử vầy xem:
Files = Application.FindFile
 
Upvote 0
PHP:
Sub ImportFiles()
    Dim FileS As FileSearch
    Set FileS = Application.FileSearch
    With FileS
        .NewSearch
        .Filename = "*.bas"
        .LookIn = "C:\"
        .SearchSubFolders = False
        .Execute
    End With
    
    Dim F As Variant
    For Each F In Application.FileSearch.FoundFiles
        ActiveWorkbook.VBProject.VBComponents.Import F
    Next F
End Sub

Mình sưu tầm được code này trên diễn đàn của minhtu chạy báo lỗi ở dòng Set FileS = Application.FileSearch. :(
Bởi vì FileSearch chỉ làm việc được với Excel 2003 trở về trước. Giờ ta dung FileSystemObject cho khỏe
 
Upvote 0
Bởi vì FileSearch chỉ làm việc được với Excel 2003 trở về trước. Giờ ta dung FileSystemObject cho khỏe

Thầy ndu96081631 có thể chỉ hẳn cho em được không? Em không biết mới cái mới lạ này mà chỉ muốn làm cho xong hẳn. Mệt mỏi thầy ạ. Ví dụ code này em sưu tầm được của thầy Siwtown

PHP:
Sub Main()
Dim fso As Object, res() As String
    Set fso = CreateObject("Scripting.FileSystemObject")
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then
            ReDim res(1 To 1)
            GetAllFiles .SelectedItems(1), fso, res
            If UBound(res) > 1 Then [A5].Resize(UBound(res) - 1) = Application.Transpose(res)
        End If
    End With
End Sub
Function GetAllFiles(ByVal StrFolder As String, fso As Object, res() As String)
Dim objFolder As Object, objSubFolder As Object, File
    Set objFolder = fso.GetFolder(StrFolder)
    For Each File In objFolder.Files
        res(UBound(res)) = fso.GetBaseName(File)
        ReDim Preserve res(1 To UBound(res) + 1)
    Next
    For Each objSubFolder In objFolder.SubFolders
        GetAllFiles objSubFolder.Path, fso, res
    Next objSubFolder
End Function

Nhờ file này em đã lấy được danh sách toàn bộ tên file trong thư mục chỉ định. Nhưng để áp vào cái này

Sub ImportBas()
Dim wb As Workbook, pth As String
Set wb = ThisWorkbook
pth = "D:\Minhanh" 'là du?ng d?n file .bas sau khi export module "Module1_msg"
wb.VBProject.VBComponents.Import (pth)
End Sub

Thì em chưa làm được.


 
Lần chỉnh sửa cuối:
Upvote 0
Web KT

Bài viết mới nhất

Back
Top Bottom