简体   繁体   English

Activecell.end.offset问题VBA

[英]Activecell.end.offset issue vba

I am new to vba, I am creating a userform and working on the code associated with the "Okay" button. 我是vba的新手,正在创建一个用户表单,并处理与“确定”按钮关联的代码。 I want to check the empty fields in the userform then go to another worksheet, select the top cell (B3), and then fill the data in the blank cell below (B4), moving along the row until column O. I want it to do the same each time someone fills in the userform and clicks "Okay". 我想检查用户窗体中的空字段,然后转到另一个工作表,选择顶部的单元格(B3),然后将数据填充到下面的空白单元格(B4)中,沿着行一直移动到O列。我希望它每次有人填写用户表单并单击“确定”时,都执行相同的操作。

However, there is an error message highlighting the line 但是,有一条错误消息突出显示了该行

Worksheets("Data_raw").Select Range("b3").Select 工作表(“ Data_raw”)。选择范围(“ b3”)。选择

. I tried to use another file and it is working I do not know why it is displaying as an error on this file :(. 我试图使用另一个文件,但它正在工作,我不知道为什么它在此文件上显示为错误:(。

Can anyone help me figure out what is wrong with my code please?. 谁能帮我弄清楚我的代码有什么问题吗?

Here is my code: 这是我的代码:

    Private Sub CmdOKAY_Click()


    'check all the fields to see if they are empty


    If BoxRM = "" Then
                MsgBox "Select a Relationship Manager"

            End If

    If TextCompany_name = "" Then
                MsgBox " Enter a company name"

            End If


    If BoxCountry = "" Then
                MsgBox "Select a country"

            End If

    If BoxTransactions = "" Then
                MsgBox "Enter a volume for the transactions"

            End If

    If Btn_Yearly_Transactions.Value = False And Btn_Monthly_Transactions.Value = False Then
                MsgBox "Select a frequency for the volume of transactions inputted"

            End If

    If BoxPosition_Vol = "" Then
                MsgBox "Enter a volume for the positions"

            End If

    If Btn_Yearly_Positions.Value = False And Btn_Monthly_Positions.Value = False Then
                MsgBox "Select a frequency for the volume of positions inputted"

            End If

    If CheckBox_RP.Value = False And CheckBox_TP.Value = False And CheckBox_TE.Value = False Then
                MsgBox " Select at least one type of account"

            End If

    Worksheets("Data_raw").Range("b3").Select

    ActiveCell.End(xlDown).Offset(1, 0).Select
    ActiveCell.Value = RM


    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = NameCo

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = Pays

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = RepTrd

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = FreqYrTrd

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = OP

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = FreqYrOP

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = AvrgDays

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = AcRP

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = AcTP

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = AcTE

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = RP_Per

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = TP_Per

    ActiveCell.Offset(0, 1).Select
    ActiveCell.Value = TE_Per


    'copy the formats from the row above
    Range("b4:o4").Copy
    ActiveCell.Offset(0, -12).PasteSpecial xlPasteFormats
        Application.CutCopyMode = False

    Unload Me
End sub

Just add 1 line before that line: 只需在该行之前添加1行:

Worksheets("Data_raw").Activate
Worksheets("Data_raw").Range("b3").Select

You can work directly with the object and make your code shorter and easier to read and debug by refactoring your code like what is below. 您可以直接使用该对象,并通过重构代码(如下所示)来使代码更短,更易于阅读和调试。

The other thing I thought worth pointing out is that if you intend for the code to not paste any data if one or any of your checks fail, you'll need to add an Exit Sub inside each If block. 我想指出的另一件事是,如果您打算让代码在一项或多项检查失败时不粘贴任何数据,则需要在每个If块内添加一个Exit Sub Otherwise, it will keep going and paste what is available each time the button is clicked. 否则,它将继续进行并粘贴每次单击按钮时可用的内容。

Dim wsRawData as Worksheet
Set wsRawData = Worksheets("Data_raw")

With wsRawData

    Dim lNextRow as Long
    lNextRow = .Range("B" & .Rows.Count).End(xlUp).Offset(1).Row

    .Cells(lNextRow, 2).Value = RM
    .Cells(lNextRow, 3).Value = NameCo
    .Cells(lNextRow, 4).Value = Pays

    '... and so on

End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM