Nhập dữ liệu vào ô trống trong vùng nhập liệu không liên tục

Liên hệ QC

Bong0503

Thành viên hoạt động
Tham gia
17/8/16
Bài viết
155
Được thích
91
Nhờ các anh chị và các bạn giúp dùm vấn đề như trong file đính kèm. Chân thành cảm ơn!
 

File đính kèm

  • Book1.xlsb
    25.2 KB · Đọc: 16
Bạn cho cái ví dụ nhập cụ thể xem nào.
Có lẽ tại mình diễn đạt không rõ ý, nếu mình chọn mã bên combobox1 là D và chọn bên combobox2 là V2 thì sẽ gán giá trị của textbox vào ô trống bất kỳ trong vùng D7:F7.
tương tự nếu mình chọn mã là G và combobox 2 là v1 thì sẽ gán giá trị của textbox vào ô trống bất kỳ trong vùngB10:C10.
Nếu 1 cột cụ thể thì mình đã làm được. Còn chưa biết ô trống là ô nào thì mình thua. Nhờ bạn trợ giúp nhé. Cám ơn bạn!
 
Upvote 0
Macro bạn cần có thể là vầy:
PHP:
Private Sub CommandButton1_Click()
Dim Rng As Range, sRng As Range
Dim Rws As Long, Cot As Integer, Col As Byte, Den As Long, W As Integer
With Sheet1
    Rws = .[A3].CurrentRegion.Rows.Count
    Set Rng = .[A3].Resize(Rws)
    Set sRng = Rng.Find(Me!ComboBox1.Text, , xlFormulas, xlWhole)
    If Not sRng Is Nothing Then
        Col = CByte(Right(Me!ComboBox2.Text, 1))
        Cot = Choose(Col, 2, 4, 7, 12)
        Den = Choose(Col, 1, 2, 4)
        For W = Cot To Cot + Den
            If .Cells(sRng.Row, W).Value = 0 Then
                  .Cells(sRng.Row, W).Value = Me!TextBox1.Text
                  Exit For
             End If
        Next W
    End If
End With
End Sub
 
Upvote 0
Macro bạn cần có thể là vầy:
PHP:
Private Sub CommandButton1_Click()
Dim Rng As Range, sRng As Range
Dim Rws As Long, Cot As Integer, Col As Byte, Den As Long, W As Integer
With Sheet1
    Rws = .[A3].CurrentRegion.Rows.Count
    Set Rng = .[A3].Resize(Rws)
    Set sRng = Rng.Find(Me!ComboBox1.Text, , xlFormulas, xlWhole)
    If Not sRng Is Nothing Then
        Col = CByte(Right(Me!ComboBox2.Text, 1))
        Cot = Choose(Col, 2, 4, 7, 12)
        Den = Choose(Col, 1, 2, 4)
        For W = Cot To Cot + Den
            If .Cells(sRng.Row, W).Value = 0 Then
                  .Cells(sRng.Row, W).Value = Me!TextBox1.Text
                  Exit For
             End If
        Next W
    End If
End With
End Sub
Thật tuyệt. Chân thành cám ơn bác.
 
Upvote 0
Macro bạn cần có thể là vầy:
PHP:
Private Sub CommandButton1_Click()
Dim Rng As Range, sRng As Range
Dim Rws As Long, Cot As Integer, Col As Byte, Den As Long, W As Integer
With Sheet1
    Rws = .[A3].CurrentRegion.Rows.Count
    Set Rng = .[A3].Resize(Rws)
    Set sRng = Rng.Find(Me!ComboBox1.Text, , xlFormulas, xlWhole)
    If Not sRng Is Nothing Then
        Col = CByte(Right(Me!ComboBox2.Text, 1))
        Cot = Choose(Col, 2, 4, 7, 12)
        Den = Choose(Col, 1, 2, 4)
        For W = Cot To Cot + Den
            If .Cells(sRng.Row, W).Value = 0 Then
                  .Cells(sRng.Row, W).Value = Me!TextBox1.Text
                  Exit For
             End If
        Next W
    End If
End With
End Sub
Bác có thể giải thích dùm E đoạn này được không. E không hiểu
Mã:
Cot = Choose(Col, 2, 4, 7, 12)
        Den = Choose(Col, 1, 2, 4)
 
Upvote 0
@Bong0503
Cot = Choose(Col, 2, 4, 7, 12)
Col là vị trí để lấy dữ liệu trong Array( Null, 2, 4, 7, 12, ....)
0 là Null
1 là 2
2 là 4
3 là 7
4 là 12
Tìm hiểu thêm ParamArray
 
Lần chỉnh sửa cuối:
Upvote 0
Bác có thể giải thích dùm E đoạn này được không.
CODE]Cot = Choose(Col, 2, 4, 7, 12)
Den = Choose(Col, 1, 2, 4)[/CODE]
Hàm Choose() trong VBA cũng tương tự hàm CHOOSE() trong Excel

Đây là những gì trong VBA:
Choose Function
Selects and returns a value from a list of arguments.
Syntax
Choose(
index, choice-1[, choice-2, ... [, choice-n]])
The Choose function syntax has these parts:
PartDescriptionindex Required. Numeric expression or field that results in a value between 1 and the number of available choices. choice Required. Variant expression containing one of the possible choices.
Remarks
Choose
returns a value from the list of choices based on the value of index. If index is 1, Choose returns the first choice in the list; if index is 2, it returns the second choice, and so on.
You can use Choose to look up a value in a list of possibilities. For example, if index evaluates to 3 and choice-1 = "one", choice-2 = "two", and choice-3 = "three", Choose returns "three". This capability is particularly useful if index represents the value in an option group.
Choose evaluates every choice in the list, even though it returns only one. For this reason, you should watch for undesirable side effects. For example, if you use the MsgBox function as part of an expression in all the choices, a message box will be displayed for each choice as it is evaluated, even though Choose returns the value of only one of them.
The Choose function returns a Null if index is less than 1 or greater than the number of choices listed.
If index is not a whole number, it is rounded to the nearest whole number before being evaluated.
Example
This example uses the Choose function to display a name in response to an index passed into the procedure in the
Ind parameter.
Mã:
Function GetChoice(Ind As Integer)
    GetChoice = Choose(Ind, "Speedy", "United", "Federal")
End Function

Còn trong Excel thì:
Description
Uses index_num to return a value from the list of value arguments. Use CHOOSE to select one of up to 254 values based on the index number. For example, if value1 through value7 are the days of the week, CHOOSE returns one of the days when a number between 1 and 7 is used as index_num.
Syntax
CHOOSE(index_num, value1, [value2], ...)
The CHOOSE function syntax has the following arguments (argument: A value that provides information to an action, an event, a method, a property, a function, or a procedure.):
. . . . . . .
Example 1
The example may be easier to understand if you copy it to a blank worksheet.
How do I copy an example?
  1. Select the example in this article.
    Important Do not select the row or column headers.
    ExcelExampleSelection_ZA10243433.gif

    Selecting an example from Help
  2. Press CTRL+C.
  3. In Excel, create a blank workbook or worksheet.
  4. In the worksheet, select cell A1, and press CTRL+V.
    Important For the example to work properly, you must paste it into cell A1 of the worksheet.
  5. To switch between viewing the results and viewing the formulas that return the results, press CTRL+` (grave accent), or on the Formulas tab, in the Formula Auditing group, click the Show Formulas button.
  6. After you copy the example to a blank worksheet, you can adapt it to suit your needs.
  7. 12345678


A. . . . . . . . . . . . . . . . . . . . . . .B


Data

Data

1st


Nails


2nd


Screws


3rd


Nuts


Finished


Bolts


Formula


Description (Result)


=CHOOSE(2,A2,A3,A4,A5)


Value from the second argument A3 (2nd)


=CHOOSE(4,B2,B3,B4,B5)

Value from the fourth argument B5 (Bolts)

Example 2
The example may be easier to understand if you copy it to a blank worksheet.
How do I copy an example?
  1. Select the example in this article.
    Important Do not select the row or column headers.
    ExcelExampleSelection_ZA10243433.gif

    Selecting an example from HelpPress CTRL+C.
  2. In Excel, create a blank workbook or worksheet.
  3. In the worksheet, select cell A1, and press CTRL+V.
    Important For the example to work properly, you must paste it into cell A1 of the worksheet.
  4. To switch between viewing the results and viewing the formulas that return the results, press CTRL+` (grave accent), or on the Formulas tab, in the Formula Auditing group, click the Show Formulas button.
After you copy the example to a blank worksheet, you can adapt it to suit your needs.
1234567


AB

Data




23




45




12




10





Formula



Description (Result)


=SUM(A2:CHOOSE(2,A3,A4,A5))


Sums the range A2:A4 (80)

choose Choose function choose worksheet function index pick from a list what function will choose a number from a list of value arguments? xl

Index_num Required. Specifies which value argument is selected. Index_num must be a number between 1 and 254, or a formula or reference to a cell containing a number between 1 and 254.
  • If index_num is 1, CHOOSE returns value1; if it is 2, CHOOSE returns value2; and so on.
  • If index_num is less than 1 or greater than the number of the last value in the list, CHOOSE returns the #VALUE! error value.
  • If index_num is a fraction, it is truncated to the lowest integer before being used.
  • Value1, value2, ... Value 1 is required, subsequent values are optional. 1 to 254 value arguments from which CHOOSE selects a value or an action to perform based on index_num. The arguments can be numbers, cell references, defined names, formulas, functions, or text.
 
Lần chỉnh sửa cuối:
Upvote 0
Mình chép fần 'Help' trong chúng í mà!

Mình rành tiếng 'Em' hơn, còn tiếng Anh mình chưa có bằng cấp nào sất!

E gặp vấn đền như thế này.
Nếu vùng "B4:B10" nhập trực tiếp dữ liệu, thì code chạy đúng.
Nếu vùng "B4:B10" là kết quả lấy từ 1 vùng khác bằng công thức thì code không chạy.
Nhờ Bác giải quyết dùm E.
Cụ thể như file đính kèm.
Chân thành cảm ơn Bác!
 

File đính kèm

  • Book1.xlsb
    28.6 KB · Đọc: 6
Upvote 0
PHP:
Private Sub CommandButton1_Click()
Dim Rng As Range, sRng As Range
Dim Rws As Long, Cot As Integer, Col As Byte, Den As Long, W As Integer
With Sheet1
    Rws = .[b3].CurrentRegion.Rows.Count
    Set Rng = .[b3].Resize(Rws)
    Set sRng = Rng.Find(Me!ComboBox1.Text, , xlValues, xlWhole)     '** '
    If Not sRng Is Nothing Then
        Col = CByte(Right(Me!ComboBox2.Text, 1))
        Cot = Choose(Col, 3, 5, 8, 9, 13, 35)
        Den = Choose(Col, 1, 2, 0, 3, 0, 6)         '*          '
        For W = Cot To Cot + Den
            If .Cells(sRng.Row, W).Value = 0 Then
                  .Cells(sRng.Row, W).Value = Me!TextBox1.Text
                  Exit For
             End If
        Next W
    End If
End With
End Sub

Trong fương thức FIND(), tham số xlFormulas giúp ta tìm nhanh hơn trong 1 vùng không chứa công thức;
Bằng ngược lại, trong vùng chứa công thức thì nhất thiết xài tham số xlValues
& chúc vui!
 
Lần chỉnh sửa cuối:
Upvote 0
PHP:
Private Sub CommandButton1_Click()
Dim Rng As Range, sRng As Range
Dim Rws As Long, Cot As Integer, Col As Byte, Den As Long, W As Integer
With Sheet1
    Rws = .[b3].CurrentRegion.Rows.Count
    Set Rng = .[b3].Resize(Rws)
    Set sRng = Rng.Find(Me!ComboBox1.Text, , xlValues, xlWhole)     '** '
    If Not sRng Is Nothing Then
        Col = CByte(Right(Me!ComboBox2.Text, 1))
        Cot = Choose(Col, 3, 5, 8, 9, 13, 35)
        Den = Choose(Col, 1, 2, 0, 3, 0, 6)         '*          '
        For W = Cot To Cot + Den
            If .Cells(sRng.Row, W).Value = 0 Then
                  .Cells(sRng.Row, W).Value = Me!TextBox1.Text
                  Exit For
             End If
        Next W
    End If
End With
End Sub

& chúc vui!
Chúc Bác ăn trưa ngon miệng!
 
Upvote 0
Web KT
Back
Top Bottom