将Userform中的值粘贴到Excel工作表

时间:2014-12-30 08:26:34

标签: excel vba excel-vba

我想将Excel Userform中的值粘贴到工作表中。可以将值粘贴到不同的工作表中,具体取决于您在Userform中放置的内容。

我到目前为止:

Private Sub Lagginarenda_Click()
Sheets("KategoriComboBox").Range("B2").Value = TextBoxFragestallare.Value
End Sub

KategoriComboBox 是一个Userform下拉列表,您可以在其中选择名称。该列表中包含的相同值具有类似的Excel工作表。

TextBoxFragestallare 是一个TextBox,您可以在其中写入值。这个值我想在一个工作表中粘贴Cell B2,你也在userform中选择。

守则不会起作用,因为它说“索引超出限定 - 运行时错误'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”)。激活使用userform下拉列表中的值?

4 个答案:

答案 0 :(得分:1)

如果我正确理解您的问题,您希望让用户通过下拉控件选择某个工作表名称,然后将他们在此工作表中输入的文字粘贴到单元格' B2'。

因此,您的设置可能如下所示: Demo application for pasting on selected sheet

Enter text

Text is pasted on selected sheet

以下是如何实现这一目标: (假设您有一个名为cbxSheet的ComboBox,一个名为txbText的TextBox和一个在UserForm中名为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

希望这可以让您了解如何实现您想要的目标!

答案 1 :(得分:0)

看起来你只是想要:

Private Sub Lagginarenda_Click()
Sheets(KategoriComboBox.Value).Range("B2").Value = TextBoxFragestallare.Value
End Sub

答案 2 :(得分:0)

下面的Sub可能适合您。 您应该适当地限定Range,并且除非严格需要,否则请避免使用SelectActivate

请注意,您应该:1)在使用之前填充ComboBox,2)确保ComboBox中所选的值符合您的要求,这样您就可以使用KategoriComboBox.Value 3)确保存在您尝试在ActiveWorkbook中使用的工作表,或者选择适当的工作表。

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

答案 3 :(得分:0)

我的问题的答案如下:

Private Sub KategoriComboBox_Change()

   Sheets(KategoriComboBox.Text).Activate

End Sub

它的作用是激活与您在Combobox中选择的名称相同的表格。

相关问题