Tạo nút bấm copy và export 1 vùng số liệu

Liên hệ QC

nilt02

Thành viên hoạt động
Tham gia
19/3/08
Bài viết
119
Được thích
17
Giới tính
Nam
Chào các bạn,
Các bạn có thể chỉ cho mình cách dùng câu lệnh gì để chèn thêm 1 chữ cuối "END" của cột A446 không (vì số liệu thường xuyên thay đổi có thể rất dài nên mình cần chốt thêm 1 từ ở cuối hàng.
Sau khi chèn được chữ "END" ở hàng cuối cùng cột A mình muốn tạo 1 nút bấm có chức năng copy và export sang dạng *.txt (Tab delimited) từ cell A2:C446 (trong ví dụ chỉ là 1 đoạn nhỏ, có thể rất dài). Lưu file có thể ở cùng với file excel này, tên file theo ô B1.
Cảm ơn các bạn nhiều
 

File đính kèm

  • copyExport.xlsx
    21.6 KB · Đọc: 16
Lần chỉnh sửa cuối:
hi...bạn nào có thể cho mình xin hướng dẫn đc không ?
cảm ơn các bạn
 
Upvote 0
đây là đoạn code để tìm vị trí ô cell cuối hàng, cuối cột, ...các bạn tham khảo
Mã:
Sub LastCell()  'Name of a Code
'Declaring the Variables
Dim LastCell As Integer 'Declaring the Variables

'Performing All VBA Action in the Active Sheet
With ActiveSheet

'Finding Last Row in a Sheet
LastCell = .Cells.SpecialCells(xlCellTypeLastCell).Row

'Displaying Message
MsgBox LastCell, , "LastRowinaSheet"

'Finding Last Column in a sheet
LastCell = .Cells.SpecialCells(xlCellTypeLastCell).Column

'Displaying Message
MsgBox LastCell, , "LastColumninaSheet"

'Finding Last Row in a Sigle Colum
LastCell = .Cells(.Cells.Rows.Count, "F").End(xlUp).Row

'Displaying Message
MsgBox LastCell, , "LastRowinaSingleColumn"

'Finding Last Column in a Single Row.
LastCell = .Cells(7, .Cells.Columns.Count).End(xlToLeft).Column

'Displaying Message
MsgBox LastCell, , "LastColumninaSingleRow"

'Finding Last Row using UsedRange
LastCell = .UsedRange.Rows(.UsedRange.Rows.Count).Row

'Displaying Message
MsgBox LastCell, , "LastRowUsingUsedRange"

'Finding Last Column using UsedRange
LastCell = .UsedRange.Columns(.UsedRange.Columns.Count).Column

'Displaying Message
MsgBox LastCell, , "LastColumnUsingUsedRange"

End With

End Sub
Bài đã được tự động gộp:

còn đây là code để xuất excel ra định dạng txt
Mã:
Sub ExceltoText()

'Declaring the variables
Dim FileName, sLine, Deliminator As String
Dim LastCol, LastRow, FileNumber As Integer

'Excel Location and File Name
FileName = ThisWorkbook.Path & "\ExceltoText.txt"

'Field Separator
Deliminator = " "

'Identifying the Last Cell
LastCol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
LastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row
FileNumber = FreeFile

'Creating or Overwrighting a text file
Open FileName For Output As FileNumber

'Reading the data from Excel using For Loop
For I = 1 To LastRow
For J = 1 To LastCol

'Removing Deliminator if it is wrighting the last column
If J = LastCol Then
sLine = sLine & Cells(I, J).Value
Else
sLine = sLine & Cells(I, J).Value & Deliminator
End If
Next J

'Wrighting data into text file
Print #FileNumber, sLine
sLine = ""
Next I

'Closing the Text File
Close #FileNumber

'Generating message to display
MsgBox "Text file has been generated"

End Sub
 
Lần chỉnh sửa cuối:
Upvote 0
Nhưng bạn nào có code thêm "END" cuối cùng của row A share mình với ?
Cảm ơn các bạn
 
Upvote 0
Chọn một trong hai giải pháp sau
1/ Chèn thêm chữ END vào hàng cuối cột A khi đã có dữ liệu
Mã:
Sub AddEnd()
     Dim n As Long
     n = Range("A650000").End(xlUp).Row
     Range("A" & n) = Range("A" & n) & "END"
End Sub

2/ Chèn thêm chữ END vào hàng cuối cột A
Mã:
Sub AddEnd()
     Dim n As Long
     n = Range("A650000").End(xlUp).Row
     Range("A" & n + 1) =  "END"
End Sub
 
Upvote 0
Chọn một trong hai giải pháp sau
1/ Chèn thêm chữ END vào hàng cuối cột A khi đã có dữ liệu
Mã:
Sub AddEnd()
     Dim n As Long
     n = Range("A650000").End(xlUp).Row
     Range("A" & n) = Range("A" & n) & "END"
End Sub

2/ Chèn thêm chữ END vào hàng cuối cột A
Mã:
Sub AddEnd()
     Dim n As Long
     n = Range("A650000").End(xlUp).Row
     Range("A" & n + 1) =  "END"
End Sub
Mình thử và thấy ok rồi. cảm ơn bạn nhiều!
 
Upvote 0
Web KT
Back
Top Bottom