让用户窗体文本框 link 与组合框值位于同一行,但位于不同的列
Have the userform textbox link to the same row as the combobox value but a different column
我有一个带有组合框和文本框的用户表单。我希望文本框从 combobox1 的值 link 到单元格 2 列。我该怎么做?
此外,如果 combobox/textbox 为空白,我希望 linked 单元格值保持原样。
下面用于填充用户窗体组合框的代码。
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 之前的列有边框。
这里假设您想要的是 filldown 而不是自动填充(从最后一行获取公式)。看看这是否适合你。
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
编辑: 根据上次讨论进行了更改...
我有一个带有组合框和文本框的用户表单。我希望文本框从 combobox1 的值 link 到单元格 2 列。我该怎么做?
此外,如果 combobox/textbox 为空白,我希望 linked 单元格值保持原样。
下面用于填充用户窗体组合框的代码。
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 之前的列有边框。
这里假设您想要的是 filldown 而不是自动填充(从最后一行获取公式)。看看这是否适合你。
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
编辑: 根据上次讨论进行了更改...