Những câu hỏi về code, xin giải thích các code, đề nghị các bạn gửi vào đây

Liên hệ QC
Status
Không mở trả lời sau này.

ST-Lu!

Love Wingchun
Tham gia
19/8/08
Bài viết
730
Được thích
546
Nghề nghiệp
Xích lô một thời
Kể từ hôm nay, tất cả những câu hỏi nhờ giải thích dùm một đoạn code, hay là hỏi những vấn đề linh tinh gì liên quan đến cách viết code, đề nghị các bạn gửi chung vào đây.

Những đề tài mới với tiêu đề: "Nhờ giải thích dùm đoạn code", mà không nói rõ là code gì, code dùng để làm gì, sẽ bị xóa.

BQT

----------------------------------------------------------------------------------------------------------------


Em xin được hỏi 2 đoạn code sau có tương đương nhau ?

Cells(Cells.Rows.Count, 1).End(xlUp).Row có tương đương với [A65000].End(xlup).row

Cám ơn các anh chỉ giáo
 
Chỉnh sửa lần cuối bởi điều hành viên:
@ Ndu câu này bị lỗi bạn ạ. Bạn vui lòng kiểm tra lại giúp mình với.
Xin lổi... tôi chưa test trường hợp vùng dử liệu không liên tục
Vậy xin bạn cho biết vùng mà bạn muốn clear có chứa cái gì? Có thể clear hết được không?
Hoặc giã bạn gữi ví dụ lên thì càng tốt

@ Ndu mình đã gửi nguyên cả file tại bài trước
Bạn bỏ đoạn SpecialCells(2,23) thử xem
Tức chỉ còn:
PHP:
[F6:F1000].Offset(, 28).Resize(, 3).ClearContents
Sai hay thiếu chổ nào đó thì bạn cứ gia giãm Offset và Resize nhé
Chú ý:
Bạn viết .Resize(0,3) là sai à nha ---> bỏ luôn số 0
 
Chỉnh sửa lần cuối bởi điều hành viên:
Upvote 0
Bạn bỏ đoạn SpecialCells(2,23) thử xem
Tức chỉ còn:
PHP:
[F6:F1000].Offset(, 28).Resize(, 3).ClearContents
Sai hay thiếu chổ nào đó thì bạn cứ gia giãm Offset và Resize nhé
Chú ý:
Bạn viết .Resize(0,3) là sai à nha ---> bỏ luôn số 0

bỏ SpecialCells thì nó xoá sạch cả vùng đó sao?

----------------------------------------------
Xin lỗi có lẽ do mình nói chưa rõ: Mình muốn từ cột F dịch sang phải 28 cột và xóa dữ liệu ở 2 cột 28 và 29 thì viết như thế nào ?
mình đoán chỉ xoá khi cột F có số liệu

nếu đoán đúng thì bạn dùng đoạn code này xem thế nào
PHP:
Sub Macro2()
With [F6:F1000]
    .SpecialCells(4).EntireRow.Hidden = True
    .Offset(, 28).Resize(, 2).SpecialCells(12).ClearContents
    .EntireRow.Hidden = False
End With
End Sub
 
Upvote 0
@ Ndu: nếu bỏ SpecialCells(2,23) thì nó sẽ xóa cả cột đích trong khi đó Tôi chỉ muốn xóa những dòng mà cột điều kiện (F, G) có dữ liệu như kết quả bài đầu tôi gửi.
 
Lần chỉnh sửa cuối:
Upvote 0
@ Ndu: nếu bỏ SpecialCells(2,23) thì nó sẽ xóa cả cột đích trong khi đó Tôi chỉ muốn xóa những dòng mà cột điều kiện có dữ liệu.
Nhưng nếu bạn dùng SpecialCells(2,23) thì bạn lại không Resize được
Vậy chỉ còn mỗi cách chia ra từng phần mà làm
Cột F và G nhập chung thành 1
PHP:
[F6:G1000].SpecialCells(2,23).Offset(, Bao nhiêu đó).ClearContents

PHP:
[I6:I1000].SpecialCells(2,23).Offset(, Bao nhiêu đó).ClearContents
Bỏ luôn Resize
 
Upvote 0
Nhưng nếu bạn dùng SpecialCells(2,23) thì bạn lại không Resize được
Vậy chỉ còn mỗi cách chia ra từng phần mà làm
Cột F và G nhập chung thành 1
PHP:
[F6:G1000].SpecialCells(2,23).Offset(, Bao nhiêu đó).ClearContents

PHP:
[I6:I1000].SpecialCells(2,23).Offset(, Bao nhiêu đó).ClearContents
Bỏ luôn Resize

Phải chăng Offset(, ,.).Resize(, ,.) chỉ áp dụng được cho từng dòng và phải dùng vòng lặp để quét?
 
Upvote 0
Phải chăng Offset(, ,.).Resize(, ,.) chỉ áp dụng được cho từng dòng và phải dùng vòng lặp để quét?
Tức là mỗi 1 câu lệnh có những quy luật riêng của nó...
Như bạn thấy, Resize chỉ chấp nhận 1 vùng liên tục ---> Với 1 vùng dử liệu nằm cách biệt nhau thì nó không làm việc được ---> Thế thôi (Offset thì OK)
 
Upvote 0
Em mới bắt đầu tìm hiểu về VBA, em có đoạn code này không biết nó sử dụng làm gì, các đại ca chỉ dùm em với.
Mã:
Sub Macro1()
'
' Macro1 Macro
' Hoc Macro
'
' Keyboard Shortcut: Ctrl+y
'
    Application.Run "PERSONAL.XLS!Macro1"
    Application.Run "PERSONAL.XLS!Macro1"
    Range("J10").Select
    Application.Run "PERSONAL.XLS!Macro1"
    Range("K7").Select
    Application.WindowState = xlMinimized
    Range("F6:M12").Select
    ActiveCell.FormulaR1C1 = "gfg"
    Range("F6:M12").Select
    Range("F7").Activate
    ActiveCell.FormulaR1C1 = "hg"
    Range("F6:M12").Select
    Range("F8").Activate
    ActiveCell.FormulaR1C1 = "hg"
    Range("F6:M12").Select
    Range("F9").Activate
    ActiveCell.FormulaR1C1 = "hg"
    Range("F6:M12").Select
    Range("F10").Activate
    ActiveCell.FormulaR1C1 = "jh"
    Range("F6:M12").Select
    Range("F11").Activate
    ActiveCell.FormulaR1C1 = "k"
    Range("F6:M12").Select
    Range("F12").Activate
    ActiveCell.FormulaR1C1 = "jk"
    Range("F6:M12").Select
    Range("G6").Activate
    ActiveCell.FormulaR1C1 = "j"
    Range("F6:M12").Select
    Range("G7").Activate
    ActiveCell.FormulaR1C1 = "jk"
    Range("F6:M12").Select
    Range("G8").Activate
    ActiveCell.FormulaR1C1 = "jk"
    Range("F6:M12").Select
    Range("G9").Activate
    ActiveCell.FormulaR1C1 = "jk"
    Range("F6:M12").Select
    Range("G10").Activate
    ActiveCell.FormulaR1C1 = "j"
    Range("F6:M12").Select
    Range("G11").Activate
    ActiveCell.FormulaR1C1 = "kj"
    Range("F6:M12").Select
    Range("G12").Activate
    ActiveCell.FormulaR1C1 = "kj"
    Range("H6").Select
    ActiveCell.FormulaR1C1 = "kj"
    Range("H7").Select
    ActiveCell.FormulaR1C1 = "kj"
    Range("H8").Select
    ActiveCell.FormulaR1C1 = "jk"
    Range("H9").Select
    ActiveCell.FormulaR1C1 = "jk"
    Range("H10").Select
    ActiveCell.FormulaR1C1 = "jk"
    Range("H11").Select
    ActiveCell.FormulaR1C1 = "kj"
    Range("H12").Select
    ActiveCell.FormulaR1C1 = "kj"
    Range("H13").Select
    ActiveCell.FormulaR1C1 = "jk"
    Range("H14").Select
    ActiveCell.FormulaR1C1 = "jk"
    Range("H13").Select
    Selection.ClearContents
    Range("H14").Select
    Selection.ClearContents
    Range("H7").Select
End Sub
 
Chỉnh sửa lần cuối bởi điều hành viên:
Upvote 0
Bạn nhấn vào nút "Đổi sang khung lớn". Rồi bạn kéo thanh trượt xuống -> Nhấn vào nút "Tải file từ máy" -> Rồi nạp file từ máy vào. Xong.
Thân.
 
Upvote 0
Hỏi về lệnh Call

Chào các bạn , khi thực hiện 1 sub ta dùng lệnh Call hoặc không .Tốc độ thấy như nhau .
Vậy 2 trường hợp này có gì khác nhau . Cảm ơn
 
Upvote 0
Call

Chào các bạn , khi thực hiện 1 sub ta dùng lệnh Call hoặc không .Tốc độ thấy như nhau .
Vậy 2 trường hợp này có gì khác nhau . Cảm ơn

Call Statement
Transfers control to a Sub procedure, Function procedure, or dynamic-link library (DLL) procedure.
Syntax
[Call] name [argumentlist]
The Call statement syntax has these parts:
PartDescriptionCallOptional; keyword. If specified, you must enclose argumentlist in parentheses. For example:
Call MyProc(0)
nameRequired. Name of the procedure to call.argumentlistOptional. Comma-delimited list of variables, arrays, or expressions to pass to the procedure. Components of argumentlist may include the keywords ByVal or ByRef to describe how the arguments are treated by the called procedure. However, ByVal and ByRef can be used with Call only when calling a DLL procedure. On the Macintosh, ByVal and ByRef can be used with Call when making a call to a Macintosh code resource.
Remarks
You are not required to use the Call keyword when calling a procedure. However, if you use the Call keyword to call a procedure that requires arguments, argumentlist must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around argumentlist. If you use either Call syntax to call any intrinsic or user-defined function, the function's return value is discarded.
Không bắt buộc phải dùng từ khóa Call khi gọi một thủ tục, nếu dùng từ khóa Call mà có đối số thì các đối số này cần phải được bao đóng trong cặp ngoặc (). Nếu dùng Call với hàm thì giả trị trả lại của hàm bị hủy bỏ.

To pass a whole array to a procedure, use the array name followed by empty parentheses.
Example

This example illustrates how the Call statement is used to transfer control to a Sub procedure, an intrinsic function, and a dynamic-link library (DLL) procedure.DLLs are not used on the Macintosh.
' Call a Sub procedure.Call PrintToDebugWindow("Hello World") ' The above statement causes control to be passed to the following' Sub procedure.Sub PrintToDebugWindow(AnyString) Debug.Print AnyString ' Print to the Immediate window.End Sub' Call an intrinsic function. The return value of the function is' discarded.Call Shell(AppName, 1) ' AppName contains the path of the ' executable file.
 
Upvote 0
Nhờ giúp đỡ viết code VBA dùm!

Hi các huynh!

Tôi có 3 sheet (sheet 1, 2,3). Làm sao để thể hiện:

If sheet2.cells(10,3)=1 then
sheet3.cells(10,3)=1 & sheet3.cells(20,3)=sheet2.cells(20,3)*5
end if

P/S: 1 là lấy dữ liệu từ sheet1

Xin chân thành cảm ơn quý AE giúp dùm! Mong reply
 
Upvote 0
Bạn tham khảo cái ni xem sao!

PHP:
Option Explicit
Sub Sheet123()
 Dim Rng As Range
 With Sheet2
    If .[c10] = Sheet1.[c10] Then
        Sheet3.[c10] = .[c10]
        Sheet3.[c20] = .[c20] * 5
    End If
 End With
End Sub

Chúc vui!
 
Upvote 0
Chỉ đơn giản vậy thôi! Làm gì mà cao siêu chi cho mệt vậy bác.
PHP:
Sub chay()
If Sheet2.[C10] = Sheet1.[C10] Then
Sheet3.[C10] = Sheet1.[C10]
Sheet3.[C20] = Sheet2.[C20] * 5
End If
End Sub
Thân.
 
Upvote 0
Cảm ơn các huynh ChanhTQ@ và Po_Pikachu nhiều!

Và làm ơn cho tôi hỏi thêm: Dữ liệu và công thức ở sheet1 được bảo vệ; thì các cells ở sheet2 sheet3 muốn bảo vệ thì không thực hiện được, có cách nào giải quyết xin chỉ dùm!

Chân thành.
 
Upvote 0
Cảm ơn các huynh ChanhTQ@ và Po_Pikachu nhiều!
Và làm ơn cho tôi hỏi thêm: Dữ liệu và công thức ở sheet1 được bảo vệ; thì các cells ở sheet2 sheet3 muốn bảo vệ thì không thực hiện được, có cách nào giải quyết xin chỉ dùm!
Chân thành.
NguyenVanSon có đưa ra 1 cách, bạn thử tìm với từ khóa Passwords xem sao?
Mình nhớ quy trình là vầy: (Tất cả bằng VBA)
* Mở khóa các ô cần nhập ở Sheet2 & sheet3;
* Chép dữ liệu từ Sheet1 vô những ô cần thiết;
* Khóa các ô đã mở để trả về nguyên trạng;

(*) Fa lcaffê & chiêm ngưỡng thành quả của mình & mọi người! :-=
 
Upvote 0
Code VBA

Yêu cấu công việc của mình như sau dùng VBA
- Mở một bản vẽ CAD có sẵn--> Scale xuỗng tỉ lệ a/b trong đó: a là kích thước của đoạng thẳng tùy chọn ghi trong bản vẽ (có thể đo). b là kích thước nhập từ bàn phím.
- Lần lượt đo kích thước một số đoạn thẳng ---> tự động đưa giá trị sang một file Exel.
Nhờ các cao nhân chỉ giáo giúp cho !$@!!
KHông có ai giúp mình được vấn đề này à? hicc các cao nhân đâu hết rồi????!!!
 
Lần chỉnh sửa cuối:
Upvote 0
Tôi thường hay phải khai rất nhiều biến, ví dụ iMa01 as long, iMa02 as long,...,iMa99 as long.
Vậy có thể rút gọn khai biến = for i = 1 to 99 được không?
Xin cám ơn!

Điều này là không được bác ạ
Tên của 1 biến phải được định danh rõ ràng và xác định, không phụ thuộc vào 1 biến khác (Tức là không có chứa biến ở trong tên)

Thân!
 
Upvote 0
Nhờ giải thích đoạn code để tính Key đăng ký cho file excel (màu xanh)

Các Anh(Chị) có thể giải thích giúp tôi đoạn code màu xanh dưới đây được không?

Option Private Module
Public App As String
Private g_key As String

PHP:
Function pass()
        pass = "your password"
End Function
PHP:
Function appname()
       App = "sys32vb7"
End Function

PHP:
Function Protectit()
Dim x As Variant
If LCase(Left(Application.OperatingSystem, 3)) <> "mac" And LCase(Left(Application.Version, 3)) <> "8.0" Then
    For Each x In ActiveWorkbook.Sheets
        If x.Name = "Control" Or x.Name = "Invoice" Or x.Name = "Reports" Then
            x.ScrollArea = "scr" & x.Name
            x.Protect Password:=pass, DrawingObjects:=True, Contents:=True, Scenarios:=True
        End If
    Next
    ActiveWorkbook.Protect Password:=pass, Structure:=True
End If
End Function

PHP:
Function Unprotectit()
Dim x As Variant
ActiveWorkbook.Unprotect Password:=pass
For Each x In ActiveWorkbook.Sheets
    x.Unprotect Password:=pass
    x.ScrollArea = ""
Next
End Function

PHP:
Function UnProtectbook()
         ActiveWorkbook.Unprotect Password:=pass
End Function
PHP:
Function lockall()
Unprotectit
Dim x As Variant
Range("keyflag").Value = 1
For Each x In ActiveWorkbook.Sheets
    x.Protect Password:=pass, DrawingObjects:=True, Contents:=True, Scenarios:=True
Next
End Function

PHP:
Function unlockall()
Dim x As Variant
For Each x In ActiveWorkbook.Sheets
    x.EnableSelection = xlNoRestrictions
    x.Unprotect Password:=pass
Next
Range("keyflag").Value = 0
End Function
'-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - '
Function KeyGen()
g_key = "@#ft^+*(^%$#h7A=?(^d-_h.,\|{$U2+0(*&][Gs9/*5df@!%#:J`&..?\|t8eSP+"
End Function
-------------------------------------------
Function EnCrypt(strCryptThis)
Dim strChar, iKeyChar, iStringChar, i
KeyGen
For i = 1 To Len(strCryptThis)
iKeyChar = Asc(Mid(g_key, i, 1))
iStringChar = Asc(Mid(strCryptThis, i, 1))
iCryptChar = iKeyChar Xor iStringChar
strEncrypted = strEncrypted & Chr(iCryptChar)
Next
EnCrypt = strEncrypted
End Function
-------------------------------------------
Function DeCrypt(strEncrypted)
Dim strChar, iKeyChar, iStringChar, i
KeyGen
For i = 1 To Len(strEncrypted)
iKeyChar = (Asc(Mid(g_key, i, 1)))
iStringChar = Asc(Mid(strEncrypted, i, 1))
iDeCryptChar = iKeyChar Xor iStringChar
strDecrypted = strDecrypted & Chr(iDeCryptChar)
Next
DeCrypt = strDecrypted
End Function
-------------------------------------------
Function CreateCode(emailaddress)
Dim iStringChar, i
For i = 2 To Len(emailaddress)
iStringChar = Asc(Mid(LCase(emailaddress), i, 1))
iStringChar = iStringChar ^ 5.23975
If Left(iStringChar, 1) < 4 Then
strCode = strCode & Chr(48 + Right(iStringChar, 1)) & Chr(57 - Left(iStringChar, 1))
ElseIf Left(iStringChar, 1) > 7 Then
strCode = strCode & Chr(97 + Right(iStringChar, 1) + Left(iStringChar, 1) + 5)
Else
strCode = strCode & Chr(97 + Right(iStringChar, 1) + Left(iStringChar, 1) + 10)
End If
strCode = strCode & Chr(48 + Mid(iStringChar, 3, 1)) & Chr(97 + Mid(iStringChar, 5, 1) + Mid(iStringChar, 6, 1))
Next
CreateCode = strCode
End Function
 
Chỉnh sửa lần cuối bởi điều hành viên:
Upvote 0
Sao không chạy dòng lệnh màu đỏ

Mình có đoạn code tự record như sau. Bình thường vẫn chạy tốt nhưng không hiểu sao được một vài hôm thì lại bị hỏng. Nguyên nhân có lẽ là do đoạn code màu đỏ đã không được thực hiện. Các bác xem hộ tại sao nhé

Sub Mergging1()
'
' Mergging Macro
' Macro recorded 2/5/2009 by thuy.dinh
'
' Keyboard Shortcut: Ctrl+Shift+K
'
Range("B1").Select
ActiveCell.FormulaR1C1 = "=LEFT(R1C[-1],FIND(""X, 0, NO"",R1C[-1],1)+7)"
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=LEFT(RIGHT(R1C[-1],LEN(R1C[-1])-SUM(OFFSET(R1C[1],,,ROW(R[-1]C[1]),1))),FIND(""X, 0, NO"",RIGHT(R1C[-1],LEN(R1C[-1])-SUM(OFFSET(R1C[1],,,ROW(R[-1]C[1]),1))),1)+7)"
Range("C1").Select
ActiveCell.FormulaR1C1 = "=LEN(RC[-1])"
Range("C2").Select
ActiveCell.FormulaR1C1 = "=LEN(RC[-1])"
Range("B2:C2").Select
Selection.AutoFill Destination:=Range("B2:C209"), Type:=xlFillDefault
Range("B2:C209").Select
ActiveWindow.SmallScroll Down:=-15
Range("B6").Select
Columns("B:B").EntireColumn.AutoFit
Cells.Select
Range("E10").Activate
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Columns("B:B").Select
Selection.Replace What:=",", Replacement:=" ", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
ActiveWindow.SmallScroll Down:=-9
Columns("B:B").Select
Selection.TextToColumns Destination:=Range("B1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(28, 1), Array(53, 1), Array(80, 1), Array(112, 1), _
Array(138, 1), Array(172, 1), Array(197, 1), Array(225, 1), Array(252, 1), Array(278, 1), _
Array(325, 1), Array(350, 1), Array(376, 1)), TrailingMinusNumbers:=True
Range("B12").Select
Columns("B:B").EntireColumn.AutoFit
Columns("B:B").ColumnWidth = 17.57
Columns("E:E").ColumnWidth = 12
Columns("D:E").Select
Selection.Delete Shift:=xlToLeft
Columns("E:H").Select
Selection.Delete Shift:=xlToLeft
Selection.ColumnWidth = 9.86
Columns("F:I").Select
Selection.Delete Shift:=xlToLeft
Range("H4").Select
Columns("E:E").ColumnWidth = 12.43
Columns("A:A").ColumnWidth = 10
Range("B1:E50").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection.Font
.Name = "Tahoma"
.Size = 10
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
Range("B1").Select
Columns("B:B").Select
Selection.Replace What:="#VALUE!", Replacement:="", _
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:= _
False, ReplaceFormat:=False
Range("B1").Select
End Sub
 
Upvote 0
Status
Không mở trả lời sau này.
Web KT
Back
Top Bottom