overnight_9
strive for mastery
- Tham gia
- 4/7/12
- Bài viết
- 160
- Được thích
- 81
- Nghề nghiệp
- Công nhân
Mình thì hiểu nhưng... lười quáBạn xem lại mô tả của mình xem có vấn đề gì khó hiểu không, mình đọc cũng vài lần mà không thể hiểu được nhu cầu của bạn
Mình thì hiểu nhưng... lười quá
Ý bạn ấy là chèn 1 đoạn code (ở dạng chuổi nằm trên sheet 1 của file hiện hành) vào tất cà các file con. Vậy thôi
Vấn đề chèn thêm code, xóa bớt code hoặc chỉnh sửa code đã có nhiều trên diễn đàn rồi!
cám ơn thầy NDU,
mấy hôm nay em search muốn xỉu luôn mà không thấy, vậy em nhờ thầy giúp em 1 tí với còn giấy bút của thầy em đã lập 1 bảng tính riêng rồi, em sẽ xuống BH gặp thầy 1 chuyến gần nhất, hihihihihi
Bạn theo hướng dẫn của bài này:
Function getFile(Tit As String, formatName As String, formatType As String)
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
With dlgOpen
.Title = Tit
.Filters.Clear
.Filters.Add formatName, formatType
.AllowMultiSelect = False
result = .Show
If (result <> 0) Then
getFile = Trim(dlgOpen.SelectedItems.Item(1))
End If
End With
End Function
Private Sub CommandButton1_Click()
Me![Add_File] = getFile("c:\", "Select the Excel File", "*.xls")
MsgBox "import thanh cong"
End Sub
Vầy mới đúng chứ:hi anh chị,
vậy thì anh chị hướng dẫn tôi từng bước vậy.
bước 1: Chèn file vào listbox
1./ Listbox (getfile) tạo 1 module 2 với code là:
PHP:Function getFile(Tit As String, formatName As String, formatType As String) Dim dlgOpen As FileDialog Set dlgOpen = Application.FileDialog(msoFileDialogOpen) With dlgOpen .Title = Tit .Filters.Clear .Filters.Add formatName, formatType .AllowMultiSelect = False result = .Show If (result <> 0) Then getFile = Trim(dlgOpen.SelectedItems.Item(1)) End If End With End Function
2./ cmd Add_File với code là:
PHP:Private Sub CommandButton1_Click() Me![Add_File] = getFile("c:\", "Select the Excel File", "*.xls") MsgBox "import thanh cong" End Sub
bước đầu tiên bị lỗi nhiều quá, anh chị hướng dẫn giúp. cám ơn nhiều
Function GetFile(Tit As String, formatName As String, formatType As String)
Dim Arr()
Dim n As Long, lCount As Long
On Error Resume Next
With Application.FileDialog(msoFileDialogOpen)
.Title = Tit
.Filters.Clear
.Filters.Add formatName, formatType
.AllowMultiSelect = True
If .Show Then
lCount = .SelectedItems.Count
ReDim Arr(1 To lCount)
For n = 1 To lCount
Arr(n) = .SelectedItems(n)
Next
GetFile = Arr
End If
End With
End Function
Private Sub CommandButton1_Click()
Dim Arr
On Error Resume Next
Arr = GetFile("Select the Excel File", "Excel file", "*.xls")
If IsArray(Arr) Then
Me.ListBox1.List() = Arr
MsgBox "Import thanh cong"
End If
End Sub
Vầy mới đúng chứ:
PHP:[php] Private Sub CommandButton1_Click() Dim Arr On Error Resume Next Arr = GetFile("Select the Excel File", "Excel file", "*.xls") If IsArray(Arr) Then Me.ListBox1.List() = Arr MsgBox "Import thanh cong" End If End Sub
Option Explicit
Private Sub CommandButton1_Click()
Dim Arr
On Error Resume Next
Arr = getFile("Select the Excel File", "Excel file", "*.xls")
If IsArray(Arr) Then
Me.Listbox1.List() = Arr
End
Application.Workbooks.Open (Arr)
Arr.Visible = True
With ActiveWorkbook
.RunAutoMacros xlAutoOpen
.Saved = True
.Close
.......................................
Kích hoạt file hiện hành & copy 1 chuỗi code vào workbook đó luôn thì biến sao anh NDU? cho em xin hướng để mò. em cảm ơn
PHP:Option Explicit Private Sub CommandButton1_Click() Dim Arr On Error Resume Next Arr = getFile("Select the Excel File", "Excel file", "*.xls") If IsArray(Arr) Then Me.Listbox1.List() = Arr End Application.Workbooks.Open (Arr) Arr.Visible = True With ActiveWorkbook .RunAutoMacros xlAutoOpen .Saved = True .Close .......................................
Arr là 1 Array trong đó có chứa nhiều file
Khi bạn mở hộp Dialog Open file, nếu bạn chỉ chọn duy nhất 1 file thì Arr là mảng 1 phần tử. Nhưng khi bạn chọn cùng lúc nhiều file thì Arr sẽ là mảng nhiều phần tử
Và dù thế nào đi nữa thì cũng không thể dùng Workbooks.Open (Arr) kiểu đó được. Ít ra cũng phải vầy: Workbooks.Open (Arr(1)) ---> Tức mở file có đường dẫn là phần tử đầu tiên của mảng Arr
Option Explicit
Private Sub CommandButton1_Click()
Dim Arr
On Error Resume Next
Arr = getFile("Select the Excel File", "Excel file", "*.xls")
If IsArray(Arr) Then
Me.Listbox1.List() = Arr
End With
Application.Workbooks.Open (Arr(1))
Arr.Visible = True
With ActiveWorkbook
.RunAutoMacros xlAutoOpen
.Saved = True
.Close
Dim Tmp, CurSh As Workbook
With ThisWorkbook.VBProject.VBComponents(ActiveWorkbook.CodeName).CodeModule
Tmp = .Lines(1, .CountOfLines)
End With
Tmp = Replace(Tmp, "Option Explicit", "")
Set CurSh = .ActiveWorkbook
With .VBProject.VBComponents(CurSh.CodeName).CodeModule
If InStr(.Lines(1, .CountOfLines), Tmp) = 0 Then .AddFromString Tmp
End With
.Close True
End With
End If
End Sub
Ối trời ơi... Sai cả rổ luôn!hi anh NDU,
anh xem giúp em cái ma trận này,
em cám ơn anh nhiều.
Tôi nói luôn: Tôi và tất cả thành viên GPE này đều không tính đến chuyện lợi nhuận khi giúp 1 ai đâu (bạn có thể yên tâm)... Có điều ai cũng có công việc, rảnh rỗi và thảnh thơi đầu óc sẽ giúp bạnDear anh NDU,
Anh giúp em code này giúp
http://www.giaiphapexcel.com/forum/showthread.php?68820-Code-copy-code-vao-codesheet
hôm nào em xuống Đồng Nai gặp anh, em muốn nhờ anh phân tích giúp em 1 số cơ sỡ dữ liệu cho công ty em, nếu có khả thi em sẽ có kế hoạch làm việc với sếp em tất nhiên anh sẽ không thiệt thòi về chất xám đâu.
Ối trời ơi... Sai cả rổ luôn!
Nói chung tôi khẳng định là yêu cầu của bạn hoàn toàn làm được nhưng đừng có nóng! Từ từ tôi nghiên cứu cho
(mà sao hổng thấy cao thủ nào vào làm bài này nhỉ?)
----------------------
Nói ngoài lề 1 chút: .................
(bài này code cũng hơi lằng nhằng đây!)
Bài toán khó quá nhưng cũng làm thử xem trúng đựơc bao nhiêu %. Các Anh Chị xem và góp ý dựa trên code của em dùm nghen anh, đừng đưa cái khác cao siêu quá là em bị knocked out liềncám ơn anh NDU, em cũng đang mò vọc tứ tung càng vọc càng như vào vùng rừng (Nam Cát Tiên), cứ râu ông này cấm càm bà kia nên không có lối thoát.
& đang nhờ sự giúp đỡ của các anh, còn chuyện khác hôm nào em gặp các anh em tâm sự chứ post lên đây mod xóa liền.. HIC.
còn rất rất nhiều vấn đề em đang nhờ vả & học để mà giải quyết từ từ.... những cái module này chỉ giải quyết trước mắt thôi anh NDU.
Sub add_list()
Application.ScreenUpdating = False
Dim FilesToOpen
Dim x As Integer
On Error GoTo ErrHandler
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Microsoft Excel Files (*.xls), *.xls", MultiSelect:=True)
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
While x <= UBound(FilesToOpen)
Workbooks.Open Filename:=FilesToOpen(x)
UserForm1.ListBox1.AddItem (ActiveWorkbook.Name)
add_code
ActiveWorkbook.Close True
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
Application.ScreenUpdating = True
End Sub
Sub add_code()
Application.ScreenUpdating = False
Dim StartLine As Long
Dim cLines As Long
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
cLines = .CountOfLines + 1
.InsertLines cLines, _
"Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)" & Chr(13) & _
" Dim vunggiao1 As Range, vunggiao2 As Range" & Chr(13) & _
" Set vunggiao1 = Range([K132], [K10000].End(xlUp))" & Chr(13) & _
" Set vunggiao2 = Range([AI132], [AI10000].End(xlUp))" & Chr(13) & _
" msgbox ""Da tao code"" " & Chr(13) & _
"End Sub"
End With
Application.ScreenUpdating = True
End Sub
Sub xoa_1_file()
Application.ScreenUpdating = False
Dim tenfile
On Error GoTo thoat
tenfile = UserForm1.ListBox1.Column(0)
With UserForm1
.ListBox1.RemoveItem (.ListBox1.ListIndex)
MsgBox "Da xoa " & tenfile
End With
Workbooks.Open ThisWorkbook.Path & "\filedulieu\" & tenfile
Xoatatcacode
ActiveWorkbook.Close True
Application.ScreenUpdating = True
Exit Sub
thoat: MsgBox " Khong con file"
Application.ScreenUpdating = True
End Sub
Sub Xoatatcacode()
Application.ScreenUpdating = False
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Set VBProj = ActiveWorkbook.VBProject
For Each VBComp In VBProj.VBComponents
If VBComp.Type = vbext_ct_Document Then
Set CodeMod = VBComp.CodeModule
With CodeMod
.DeleteLines 1, .CountOfLines
End With
Else
VBProj.VBComponents.Remove VBComp
End If
Next VBComp
Application.ScreenUpdating = True
End Sub
Sub xoa_het_file()
Application.ScreenUpdating = False
Dim tenfile
On Error GoTo thoat
For i = 0 To UserForm1.ListBox1.ListCount
tenfile = UserForm1.ListBox1.Column(0)
With UserForm1
.ListBox1.RemoveItem (.ListBox1.ListIndex)
End With
Workbooks.Open ThisWorkbook.Path & "\filedulieu\" & tenfile
Xoatatcacode
ActiveWorkbook.Close True
Next
MsgBox "Da xoa het file trong List"
Application.ScreenUpdating = True
Exit Sub
thoat: MsgBox " Khong con file"
Application.ScreenUpdating = True
End Sub
2./ Khi thoát form để làm việc thì toàn bộ file được add trong listbox mất luôn, không lưu lại (ý nghĩa lưu lại là khi ta làm việc xong file nào rồi ta mở lại form đề xoá code & close khỏi Excel để add tiếp file khác vào)Programmatic access to visual basic project is not trusted.
Debug
With ActiveWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
1. Chọn Tools > Macro>Security>Trusted Publishers và đánh dấu chọn ô Trust acess....Cám ơn anh Quanghai1969 rất nhiều. nói thật em mà viết được như anh thì phải ngồi học hơn 12 tháng nữa, em đang phấn đấu điều đó.
cho Code anh giúp em test thì thấy có 2 vấn đề anh xem chỉnh lại giúp em tí.
1./ Lỗi khi chạy sub của add_code là:
2./ Khi thoát form để làm việc thì toàn bộ file được add trong listbox mất luôn, không lưu lại (ý nghĩa lưu lại là khi ta làm việc xong file nào rồi ta mở lại form đề xoá code & close khỏi Excel để add tiếp file khác vào)
cám ơn anh Quanghai nhiều.
1. Chọn Tools > Macro>Security>Trusted Publishers và đánh dấu chọn ô Trust acess....
2. Trong cửa sổ VBE chọn Tools> References > và tìm đánh dấu vào mục Microsoft Visual Basic for Application Extensibility 5.3
Sau khi code hoạt động rồi thì khi mở Form lên sẽ không có em nào trong list đâu
Ps: không cần 12 tháng đâu, biết căn bản như mình chỉ cần chịu cày khoảng 3 tháng là được.
1. Không thể add code vào file nhaplieu, minh đâu có bị như vậy đâuTks anh Quanghai nhiều,
anh giúp em tí nữa anh Quanghai,
1./ Khi ta add_File vào listbox đừng cho sub add_code vào workbook file gốc của nó là file ("nhaplieu"), mà chỉ add_code vào file vừa được add vào listbox.
2./ Hình như vấn đề em diễn đạt sau đây thì em có đọc bài của anh NDU thì phải. Là nó phải xét code trùng có nghĩa là nếu file add vào list mà đã có sẳn code ở workbook giống nó rồi thì thôi, chỉ add 1 lần duy nhất.
Code hiện tại là nó copy quá nhiều sub vào workbook, nếu mình add 10 files vào list, thì ở workbook của mổi file được add đến 10 sub luôn. (trùng sub nó bị lỗi không cho minh làm việc)
3./Em muốn sao khi được add vào listbook thì file đó hiện hành luôn, và vẫn lưu các file được add vào listbox khi làm việc xong thì mở form lại mình mới xoá file đó khỏi list kèm theo xoá luôn code file đó & close khỏi Excel.
Hy vọng anh hiểu ý em diễn đạt mà giúp em lần nữa, cám ơn anh.