Cần sửa giúp đoạn code VBA (1 người xem)

  • Thread starter Thread starter bagiacom
  • Ngày gửi Ngày gửi
Liên hệ QC

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

bagiacom

Thành viên mới
Tham gia
1/11/10
Bài viết
27
Được thích
1
Mình có đoạn code, lấy dữ liệu từ nhiều file excel về một file excel. Nhưng đoạn code lại lấy công thức sang, mình muốn đoạn code lấy giá trị sang thôi. Mong các bạn giúp đỡ
Sub test()
Dim FSO As Object, FileItem As Object, i As Integer, wbmain As Workbook, wb As Workbook
Set wbmain = ThisWorkbook
Set FSO = CreateObject("Scripting.FileSystemObject")
With wbmain
For Each FileItem In FSO.GetFolder(wbmain.Path).Files
If FileItem.Name <> "Tong hop.xlsx" And Left(FileItem.Name, 1) <> "~" Then
i = i + 1
Set wb = Workbooks.Open(FileItem.Path)
wb.Sheets("Tram 1").Range("D2:E34").Copy .ActiveSheet.Cells(4, i * 2)
.ActiveSheet.Cells(2, i * 2) = Left(FileItem.Name, Len(FileItem.Name) - 5)
wb.Close False
End If
Next
End With
End SubSub test()
Dim FSO As Object, FileItem As Object, i As Integer, wbmain As Workbook, wb As Workbook
Set wbmain = ThisWorkbook
Set FSO = CreateObject("Scripting.FileSystemObject")
With wbmain
For Each FileItem In FSO.GetFolder(wbmain.Path).Files
If FileItem.Name <> "Tong hop.xlsx" And Left(FileItem.Name, 1) <> "~" Then
i = i + 1
Set wb = Workbooks.Open(FileItem.Path)
wb.Sheets("Tram 1").Range("D2:E34").Copy .ActiveSheet.Cells(4, i * 2)
.ActiveSheet.Cells(2, i * 2) = Left(FileItem.Name, Len(FileItem.Name) - 5)
wb.Close False
End If
Next
End With
End Sub
 
Mình có đoạn code, lấy dữ liệu từ nhiều file excel về một file excel. Nhưng đoạn code lại lấy công thức sang, mình muốn đoạn code lấy giá trị sang thôi. Mong các bạn giúp đỡ
Sub test()
Dim FSO As Object, FileItem As Object, i As Integer, wbmain As Workbook, wb As Workbook
Set wbmain = ThisWorkbook
Set FSO = CreateObject("Scripting.FileSystemObject")
With wbmain
For Each FileItem In FSO.GetFolder(wbmain.Path).Files
If FileItem.Name <> "Tong hop.xlsx" And Left(FileItem.Name, 1) <> "~" Then
i = i + 1
Set wb = Workbooks.Open(FileItem.Path)
wb.Sheets("Tram 1").Range("D2:E34").Copy .ActiveSheet.Cells(4, i * 2)
.ActiveSheet.Cells(2, i * 2) = Left(FileItem.Name, Len(FileItem.Name) - 5)
wb.Close False
End If
Next
End With
End SubSub test()
Dim FSO As Object, FileItem As Object, i As Integer, wbmain As Workbook, wb As Workbook
Set wbmain = ThisWorkbook
Set FSO = CreateObject("Scripting.FileSystemObject")
With wbmain
For Each FileItem In FSO.GetFolder(wbmain.Path).Files
If FileItem.Name <> "Tong hop.xlsx" And Left(FileItem.Name, 1) <> "~" Then
i = i + 1
Set wb = Workbooks.Open(FileItem.Path)
wb.Sheets("Tram 1").Range("D2:E34").Copy .ActiveSheet.Cells(4, i * 2)
.ActiveSheet.Cells(2, i * 2) = Left(FileItem.Name, Len(FileItem.Name) - 5)
wb.Close False
End If
Next
End With
End Sub
Bạn thêm dòng dưới đây vào xem:
PHP:
.UsedRange.Value = .UsedRange.Value
 
Upvote 0
Mình có đoạn code, lấy dữ liệu từ nhiều file excel về một file excel. Nhưng đoạn code lại lấy công thức sang, mình muốn đoạn code lấy giá trị sang thôi. Mong các bạn giúp đỡ
Sub test()
Dim FSO As Object, FileItem As Object, i As Integer, wbmain As Workbook, wb As Workbook
Set wbmain = ThisWorkbook
Set FSO = CreateObject("Scripting.FileSystemObject")
With wbmain
For Each FileItem In FSO.GetFolder(wbmain.Path).Files
If FileItem.Name <> "Tong hop.xlsx" And Left(FileItem.Name, 1) <> "~" Then
i = i + 1
Set wb = Workbooks.Open(FileItem.Path)
wb.Sheets("Tram 1").Range("D2:E34").Copy .ActiveSheet.Cells(4, i * 2)
.ActiveSheet.Cells(2, i * 2) = Left(FileItem.Name, Len(FileItem.Name) - 5)
wb.Close False
End If
Next
End With
End SubSub test()
Dim FSO As Object, FileItem As Object, i As Integer, wbmain As Workbook, wb As Workbook
Set wbmain = ThisWorkbook
Set FSO = CreateObject("Scripting.FileSystemObject")
With wbmain
For Each FileItem In FSO.GetFolder(wbmain.Path).Files
If FileItem.Name <> "Tong hop.xlsx" And Left(FileItem.Name, 1) <> "~" Then
i = i + 1
Set wb = Workbooks.Open(FileItem.Path)
wb.Sheets("Tram 1").Range("D2:E34").Copy .ActiveSheet.Cells(4, i * 2)
.ActiveSheet.Cells(2, i * 2) = Left(FileItem.Name, Len(FileItem.Name) - 5)
wb.Close False
End If
Next
End With
End Sub
Sửa dòng này thành 2 dòng
wb.Sheets("Tram 1").Range("D2:E34").Copy .ActiveSheet.Cells(4, i * 2)

thành

wb.Sheets("Tram 1").Range("D2:E34").Copy
.ActiveSheet.Cells(4, i * 2)
.PasteSpecial xlPasteValues
 
Upvote 0
Sửa dòng này thành 2 dòng
wb.Sheets("Tram 1").Range("D2:E34").Copy .ActiveSheet.Cells(4, i * 2)

thành

wb.Sheets("Tram 1").Range("D2:E34").Copy
.ActiveSheet.Cells(4, i * 2)
.PasteSpecial xlPasteValues
Cảm ơn bạn. Mình đã thành công. Cho mình hỏi thếm một chút. Hiện nay code này ghi dữ liệu từ Cột B trở đi. Mình muốn ghi sang cột D hoặc E thì phải thay đổi code như thế nào
 
Upvote 0
Web KT

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

Back
Top Bottom