使用户窗体文本框链接到组合框值所在的同一行,但不同的列

时间:2019-05-23 17:23:48

标签: excel vba userform

我有一个带有组合框和文本框的用户窗体。我希望文本框从combobox1的值链接到单元格2列。我将如何去做?

如果组合框/文本框为空白,我希望链接的单元格值保持原样。

下面的代码用于填充用户窗体组合框。

 With Worksheets("ML")
.Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0) = ComboBox1.Value
.Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0) = ComboBox2.Value
.Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).AutoFill 
.Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).Resize(2)

With .Cells(Rows.Count, "B").End(xlUp).Offset(-1, 1).Resize(, 3).Resize(2)
    .Borders.LineStyle = xlContinuous
End With
With Worksheets("CT")
  .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0) = ComboBox2.Value
  .Cells(Rows.Count, "A").End(xlUp).Offset(-1, 1).Resize(, 21).AutoFill 
  .Cells(Rows.Count, "A").End(xlUp).Offset(-1, 1).Resize(, 21).Resize(2)
With .Cells(Rows.Count, "A").End(xlUp).Offset(-1, 1).Resize(, 3).Resize(2)
    .Borders.LineStyle = xlContinuous
End With


ActiveWorkbook.RefreshAll
Unload Me
End Sub

我希望将Combobox1的值显示在A列中的下一个可用单元格中,然后我希望将textbox1显示在与combobox值相同的行中,但要显示在AE列中。在与textbox值和combobox值相同的行中,我希望填充AM之前的列。最后,我希望直到AM的列都有边框。 Worksheet Monthly FGL Report

1 个答案:

答案 0 :(得分:1)

假设此处需要填充而不是自动填充(从最后一行获取公式)。看看这是否适合您。

Dim shtML As Worksheet: Set shtML = ActiveWorkbook.Worksheets("ML") 'Set this to the correct workbook
Dim rngDest As Range
Dim lRow As Long

If ComboBox1.Value <> "" And TextBox1.Value <> "" Then  'Use <[ Or ]> instead of <[ And ]> as you see fit
    With shtML
        lRow = .Cells(.Rows.Count, 1).End(xlUp).row + 1 'Get the first free row
        Set rngDest = .Range(.Cells(lRow, 1), .Cells(lRow, 39))

        With rngDest
            .FillDown 'In the same row as both the textbox value and the combobox value I would like the columns up to AM to be filled down
            .Cells(1, 1) = ComboBox1.Value   'the value of the Combobox1 to display at the next available cell in column A
            .Cells(1, 31) = TextBox1.Value   'the textbox1 to show up in the same row as the combobox value but in column AE
            .Resize(1, .Columns.Count + 5).Borders.LineStyle = xlContinuous  'Finally I would like the columns up to AM have borders (+5 past the fill down range).
        End With
    End With
End If

编辑:根据上次讨论进行了更改...