【问题标题】:Have the userform textbox link to the same row as the combobox value but a different column让用户表单文本框链接到与组合框值相同的行但不同的列
【发布时间】:2019-10-10 07:45:55
【问题描述】:

我有一个带有组合框和文本框的用户表单。我希望文本框从 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 显示在与组合框值相同的行中,但在列 AE 中。在与文本框值和组合框值相同的行中,我希望填写到 AM 的列。最后,我希望 AM 之前的列有边框。

【问题讨论】:

  • 您可以重复使用相同的代码.Cells(.Rows.Count, "B").End(xlUp).Offset(1, 0) = ComboBox1.Value,只需在offset 列中添加2,即:.Cells(.Rows.Count, "B").End(xlUp).Offset(1, 2) = TextBox.Value。至于将值保留为空白,您应该将每个值分配包装在 if 语句中,例如 if combobox1.value <> "" then...
  • 请澄清:您是否希望将“combobox1 的值超过单元格 2 列”中的值放在 TextBox 中,反之亦然
  • 单元格 2 列在组合框 @GMalc 的链接单元格上方
  • @DarXyde 这是我迄今为止尝试过的,但如果我输入文本框,我什么也不会显示。我也认为当它留空时不会发生任何事情。 With Worksheets("ML") If TextBox1.Value "" Then .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 2) = TextBox1.Value .Cells(Rows.Count , "A").End(xlUp).Offset(-1, 1).Resize(, 43).AutoFill .Cells(Rows.Count, "A").End(xlUp).Offset(-1, 1) .Resize(, 43).Resize(2) Else End If End If End With ActiveWorkbook.RefreshAll 卸载我
  • 请将响应视为答案... cmets 中的缩进很可怕(嗯,没有)。

标签: excel vba userform


【解决方案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

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

【讨论】:

  • 它将文本框链接到正确的单元格,但行 .Offset(-1, 1).Resize(0, 43).AutoFill 会导致运行时错误 '1--4': Appliation-定义或对象定义的错误。
  • If TextBox1.Value "" Then With Worksheets("Monthly FGL Report") .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 30) = TextBox1.Value .Cells(.Rows.Count, "A").End(xlUp).Offset(-1, 1).Resize(, 43).AutoFill .Cells(Rows.Count, "A").End (xlUp).Offset(-1, 1).Resize(, 43).Resize(2) With .Cells(.Rows.Count, "A").End(xlUp).Offset(-1, 1).Resize (, 43).Resize(2) .Borders.LineStyle = xlContinuous 这不会显示错误,但在粘贴 textbox1 值之后不会执行任何操作
  • 查看更新的答案...我之前没有测试过。您提到的错误是因为: 1. 调整大小至少需要 1 行,0 不是一个好值(我在这里填写 0 不好)。 2.自动填充至少需要1个范围参数sourcerange.autofill destinationrange
  • 我不太确定您需要的正确范围,但是如果您尝试描述(或用图片显示)您希望最终结果如何,我可以使上述内容变得更好.
  • .Resize(1, .Columns.Count + 5).Borders.LineStyle = xlContinuous我也会更新答案。
猜你喜欢
  • 2019-10-15
  • 2017-02-14
  • 1970-01-01
  • 2015-03-09
  • 1970-01-01
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多