简体   繁体   English

通过VBA中的单击按钮进行比较并分配值

[英]Comparison and assign value via click button in VBA

I want to assign a value in column like serial number via click button. 我想通过单击按钮在序列号之类的栏中分配一个值。 I already a make one code but its without button. 我已经制作了一个代码,但是没有按钮。 I want to add button also. 我也想添加按钮。

Option Base 1
Function Check()
    Dim i As Integer
    Dim startCell As Range
    Dim firstNonEmptyCell As Range

    Set startCell = Range("G2")

    If VBA.IsEmpty(startCell.Value) Then
        MsgBox "No data in this column"
    Else
        Range("A2") = 1
        Range("A2").Select
        Selection.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Date:=xlDay, _
        Step:=1, Stop:=400, Trend:=False
    End If
End Function
  1. Insert a Form button via Insert in the Developer Toolbar. 插入一个Form通过按钮InsertDeveloper工具栏。

    在此处输入图片说明

  2. Paste the below code in a module and then right click on the button and click on assign Macro . 将以下代码粘贴到模块中,然后右键单击按钮,然后单击“ assign Macro

  3. Select Button1_Click in the Assign Macro dialog box and you are done. 在“ Assign Macro对话框中选择Button1_Click ,即可完成操作。

Code

Option Explicit

Sub Button1_Click()
    Dim ws As Worksheet
    Dim LRow As Long, i As Long

    '~~> Change this to the relevant worsheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        '~~> Find Last Row in Col G which has data
        LRow = .Range("G" & .Rows.Count).End(xlUp).Row

        If LRow = 1 Then
            MsgBox "No data in column G"
        Else
            For i = 2 To LRow
                .Range("A" & i).Value = i - 1
            Next i
        End If
    End With
End Sub

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

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