将用户窗体中的值粘贴到Excel工作表
我想要从Excel用户窗体中粘贴值到工作表。 这些值可以粘贴到不同的工作表中,具体取决于您放入用户窗体的内容。
我来了这么远:
Private Sub Lagginarenda_Click() Sheets("KategoriComboBox").Range("B2").Value = TextBoxFragestallare.Value End Sub
KategoriComboBox是一个Userform下拉列表,您可以在其中select一个名称。包含在该列表中的相同值具有类似的Excel工作表。
TextBoxFragestallare是一个可以在其中写入值的文本框。 这个值我想粘贴在单元格B2中,也可以在用户窗体中select。
该代码不会工作,因为它说:“索引超出限制,运行时错误'9'”
我设法走了这么远:
Private Sub Lagginarende_Click() Dim emptyRow As Long 'Aktiverar sheet Sheets("Byggkonstruktion").Activate 'Determine emptyRow emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1 'Överför information Cells(emptyRow, 1).Value = TextBoxLopnummer.Value Cells(emptyRow, 2).Value = TextBoxFragestallare.Value Cells(emptyRow, 3).Value = TextBoxMottagare.Value Cells(emptyRow, 4).Value = TextBoxDatum.Value Cells(emptyRow, 5).Value = TextBoxFraga.Value Cells(emptyRow, 8).Value = TextBoxSvar.Value If KanBesvaraFraganJa.Value = True Then Cells(emptyRow, 6).Value = KanBesvaraFraganJa.Caption Else Cells(emptyRow, 6).Value = KanBesvaraFraganNej.Caption Unload Me End Sub
我现在唯一的问题是我怎样才能不使用表格(“Byggkonstruktion”)。激活使用用户窗体中的下拉列表中的值?
下面的Sub
可能适合你。 您应该适当地限定Range
s,并且避免使用Select
或Activate
除非他们是严格需要的。
请注意,您应该:1)在使用之前填充您的ComboBox
,2)确保ComboBox
中的选定值是您想要的,所以您可以使用KategoriComboBox.Value
,3)确保存在工作表试图在ActiveWorkbook
使用,或者适当地select它。
Private Sub Lagginarende_Click() Dim emptyRow As Long 'Aktiverar sheet Dim ws As Worksheet Set ws = ActiveWorkbook.Sheets(KategoriComboBox.Value) 'Sheets("Byggkonstruktion").Activate 'Determine emptyRow emptyRow = WorksheetFunction.CountA(ws.Range("A:A")) + 1 'emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1 'Överför information ws.Cells(emptyRow, 1).Value = TextBoxLopnummer.Value ws.Cells(emptyRow, 2).Value = TextBoxFragestallare.Value ws.Cells(emptyRow, 3).Value = TextBoxMottagare.Value ws.Cells(emptyRow, 4).Value = TextBoxDatum.Value ws.Cells(emptyRow, 5).Value = TextBoxFraga.Value ws.Cells(emptyRow, 8).Value = TextBoxSvar.Value If KanBesvaraFraganJa.Value = True Then ws.Cells(emptyRow, 6).Value = KanBesvaraFraganJa.Caption Else ws.Cells(emptyRow, 6).Value = KanBesvaraFraganNej.Caption Unload Me End Sub
如果我正确理解你的问题,你想让用户通过下拉控件select一个特定的表名,然后将他们在这个表中input的文本粘贴到单元格'B2'中。
所以你的设置可能看起来像这样:
下面是你可以做到这一点:(假设你有一个名为cbxSheet的ComboBox,一个名为txbText的TextBox和一个名为btnCopyTextToSelectedSheet的用户窗体中的CommandButton)
Option Explicit Private Sub UserForm_Initialize() Dim wksCurrentSheet As Worksheet 'Add all available sheet names to dropdown box For Each wksCurrentSheet In Worksheets cbxSheet.AddItem wksCurrentSheet.Name Next wksCurrentSheet End Sub Private Sub btnCopyTextToSelectedSheet_Click() Dim strText As String Dim strSheetName As String Dim wksDestination As Worksheet 'Read sheet name from dropdown box strSheetName = cbxSheet.Value 'Try to get sheet with the defined name Set wksDestination = Worksheets(strSheetName) 'If there is no sheet with this name you will receive 'an 'Index out of bound' (9) runtime error 'Get text from textbox strText = txbText.Text 'Write to cell in destination worksheet wksDestination.Activate 'Not needed, just to let the user see 'that the copying really happens :) wksDestination.Range("B2").Value = strText 'Unload form (makes sure the UserForm_Initialize sub is called on ' each use of the form) Unload Me End Sub
我在这里上传了示例: https : //dl.dropboxusercontent.com/u/40951326/SheetSelectionExample.xlsm
希望这给你一个关于如何实现你想要的想法!
看来你只是想要:
Private Sub Lagginarenda_Click() Sheets(KategoriComboBox.Value).Range("B2").Value = TextBoxFragestallare.Value End Sub
我的问题的答案如下:
Private Sub KategoriComboBox_Change() Sheets(KategoriComboBox.Text).Activate End Sub
它的作用是激活与Combobox中select的名称相同的表单。