Latest Posts
Ms Marvel

Power Girl

Super Girl

Dưới đây là code Vba về một số hình thức copy/paste
Sử dụng phương thức paste special
Sub copy_paste_special()
'Sử dụng phương thức Range.PasteSpecial để dán dữ liệu copy

    'Copy và PasteSpecial trongrange
    Range("A1").Copy
    Range("A3").PasteSpecial Paste:=xlPasteFormats
    
    'Copy và PasteSpecial giữa các worksheets
    Worksheets("Sheet1").Range("A2").Copy 
    Worksheets("Sheet2").Range("A2").PasteSpecial Paste:=xlPasteFormulas
    
    'Copy và PasteSpecial giữa các workbooks
    Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1").Copy
    Workbooks("Book2.xlsx").Worksheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteFormats
    
    'Xóa đường viền copy xung quanh khu vực chọn copy
    Application.CutCopyMode = False

End Sub

Phương thức copy/paste đơn giản

Sub copy_paste_simple()
'Sử dụng phương thức Range.Copy cho copy/paste

    'Phương thức Range.Copy để Copy & Paste trên một dòng
    Range("A1").Copy Range("C1")
    Range("A1:A3").Copy Range("D1:D3")
    Range("A1:A3").Copy Range("D1")
    
    'Range.Copy trên các worksheets khác nhau
    Worksheets("Sheet1").Range("A1").Copy Worksheets("Sheet2").Range("A1")
    
    'Range.Copy trên các workbooks khác nhau
    Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1").Copy _
        Workbooks("Book2.xlsx").Worksheets("Sheet1").Range("A1")

End Sub

Code khác


Sub Paste_Values_Examples()
'Set the cells' values equal to another to paste values

    'Set a cell's value equal to another cell's value
    Range("C1").Value = Range("A1").Value
    Range("D1:D3").Value = Range("A1:A3").Value
     
    'Set values between worksheets
    Worksheets("Sheet2").Range("A1").Value = Worksheets("Sheet1").Range("A1").Value
     
    'Set values between workbooks
    Workbooks("Book2.xlsx").Worksheets("Sheet1").Range("A1").Value = _
        Workbooks("Book1.xlsx").Worksheets("Sheet1").Range("A1").Value
        
End Sub


Lưu ý: The Copy method of the Worksheet object doesn't have a Destination argument.
Làm thế nào để đưa hàm index và hàm match chuyên tìm kiếm thông tin như dưới đây vào trong vba của Excel =INDEX(Data!B:F,MATCH(B4,Data!B:B,0),5) Dưới đây là code cần thiết để làm điều đó.
Sub Dienthoai()
    
    Dim i As Integer
    Dim LastRow1 As Long
    LastRow1 = Worksheets("Free 50k").Cells(4, "B").End(xlDown).Row
      
     Application.Interactive = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    For i = 4 To LastRow1
            
           
           'Cells(i, 11).Value = "=INDEX(Data!B:F,MATCH(B4,Data!B:B,0),5)"
               
      
        Cells(i, 11).Value = Application.Index(Worksheets("data").Range("B:F"), Application.Match(Range("B" & i), Worksheets("data").Range("B:B"), 0), 5)
  
           
     Next i
           
   
    Application.Interactive = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
   
End Sub
To add the GetOpenFilename method to Excel for Windows Add a standard module to the Microsoft Visual Basic for Applications (VBA) project in your workbook. Type Alt+F11 to open the Visual Basic Editor, click Insert, and then click Module. Paste or type the following macro into the module.
Sub Select_File_Or_Files_Windows()
    Dim SaveDriveDir As String
    Dim MyPath As String
    Dim Fname As Variant
    Dim N As Long
    Dim FnameInLoop As String
    Dim mybook As Workbook

    ' Save the current directory.
    SaveDriveDir = CurDir

    ' Set the path to the folder that you want to open.
    MyPath = Application.DefaultFilePath

    ' You can also use a fixed path.
    'MyPath = "C:\Users\Ron de Bruin\Test"

    ' Change drive/directory to MyPath.
    ChDrive MyPath
    ChDir MyPath

    ' Open GetOpenFilename with the file filters.
    Fname = Application.GetOpenFilename( _
            FileFilter:="Excel Files (*.xls), *.xlsx", _
            Title:="Select a file or files", _
            MultiSelect:=True)

    ' Perform some action with the files you selected.
    If IsArray(Fname) Then
        With Application
            .ScreenUpdating = False
            .EnableEvents = False
        End With

        For N = LBound(Fname) To UBound(Fname)

            ' Get only the file name and test to see if it is open.
            FnameInLoop = Right(Fname(N), Len(Fname(N)) - InStrRev(Fname(N), Application.PathSeparator, , 1))
            If bIsBookOpen(FnameInLoop) = False Then

                Set mybook = Nothing
                On Error Resume Next
                Set mybook = Workbooks.Open(Fname(N))
                On Error GoTo 0

                If Not mybook Is Nothing Then
                    MsgBox "You opened this file : " & Fname(N) & vbNewLine & _
                           "And after you press OK, it will be closed" & vbNewLine & _
                           "without saving. You can replace this line with your own code."
                    mybook.Close SaveChanges:=False
                End If
            Else
                MsgBox "We skipped this file : " & Fname(N) & " because it is already open."
            End If
        Next N
        With Application
            .ScreenUpdating = True
            .EnableEvents = True
        End With
    End If

    ' Change drive/directory back to SaveDriveDir.
    ChDrive SaveDriveDir
    ChDir SaveDriveDir
End Sub


Function bIsBookOpen(ByRef szBookName As String) As Boolean
' Contributed by Rob Bovey
    On Error Resume Next
    bIsBookOpen = Not (Application.Workbooks(szBookName) Is Nothing)
End Function

When you want to add more filters, you can use the following code in the macro to filter on .xls and .csv files.
    Fname = Application.GetOpenFilename( _
            FileFilter:="XLS Files (*.xls),*.xls,CSV Files (*.csv),*.csv", _
            Title:="Select a file or files", _
            MultiSelect:=True)

Type Alt +Q to close the Visual Basic Editor. Type Alt + F8 to open the macro dialog box and run the macro.
.ghost-button {
  display: inline-block;
  width: 200px;
  padding: 8px;
  color: #fff;
  border: 2px solid #fff;
  text-align: center;
  outline: none;
  text-decoration: none;
  transition: background-color 0.2s ease-out,
              color 0.2s ease-out;
}
.ghost-button:hover,
.ghost-button:active {
  background-color: #fff;
  color: #000;
  transition: background-color 0.3s ease-in,
              color 0.3s ease-in;
}
Cách sử dụng: class=”ghost-button”. Ví dụ: Nếu bạn muốn thêm một download link, thì làm như dưới đây
<a class="ghost-button" href="link cần">Download Now</a>
Sử dụng vòng lặp for để tạo mảng trong vba của excel.
Dưới đây là code xuất giá trị vào một ô có vị trí là dòng thứ (i) và cột thứ (j) trong một sheet của excel
Sử dụng công thức countifs để đếm giá trị thỏa mãn yêu cầu và sau đó xuất giá trị (value) vào vị trí mình muốn.


Sub Macro1()
    
    Dim i As Integer, j As Integer, a As Integer, b As Integer, kq As Integer
    Dim LastRow1 As Long
    Dim LastRow2 As Long
    Dim Macro1 As Integer
    Dim Total As Integer
    LastRow1 = Worksheets("Data").Cells(2, "H").End(xlDown).Row
    LastRow2 = Worksheets("Data").Cells(2, "I").End(xlDown).Row

 ' Tìm vị trí của hàng có chữ Macro1
    Macro1 = Application.Match("Macro1", Worksheets("sheet2").Range("A:A"), 0)
 ' Tìm vị trí của hàng có chữ Total
    Total = Application.Match("TOTAL", Worksheets("sheet2").Range("A:A"), 0)
    
     Application.Interactive = False
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    For i = Macro1 + 1  To Total - 1    ' Chạy vòng lặp từ vị trí đứng dưới chữ Macro đến vị trí đứng trên chữ total
        a = 0
        b = 0
        kq = 0
          For j = 4 To 29
           
            a = Application.CountIfs(Worksheets("Data").Range("H2:H" & LastRow1), Worksheets("sheet2").Range("B" & i), Worksheets("Data").Range("I2:I" & LastRow2), Worksheets("sheet2").Cells(1, j))
            b = Application.CountIfs(Worksheets("Data").Range("H2:H" & LastRow1), Worksheets("sheet2").Range("C" & i), Worksheets("Data").Range("I2:I" & LastRow2), Worksheets("sheet2").Cells(1, j))
            kq = a + b
            Cells(i, j).Value = kq
        Next j
       
    Next i
    
    Application.Interactive = True
    Application.EnableEvents = True
    Application.ScreenUpdating = True
   
End Sub
  • 6: Tìm dòng cuối cùng của cột H bắt đầu từ vị trí thứ 2 trong sheet Data
  • 18: Sử dụng hàm countifs để đếm giá trị của B thỏa mãn điều kiện trong cột H của sheet Data và ô của hàng đầu tiên của sheet2 với cột I của sheet Data
  • 19: Sử dụng hàm countifs để đếm giá trị của C thỏa mãn điều kiện trong cột H của sheet Data và ô của hàng đầu tiên của sheet2 với cột I của sheet Data
  • 21: Xuất giá trị của kq vào ô có vị trí thứ i và thứ j
Những code có ghi chú là để trong trường hợp nếu thêm hàng vào thì code vẫn chạy đúng như mình mong muốn.


Vọc sỹ: Phoenix