2014-01-06 26 views
1

我想通過單擊按鈕爲列中的序列號分配值。我已經做了一個代碼,但沒有按鈕。我也想添加按鈕。通過VBA中的單擊按鈕進行比較和賦值

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 
+0

你要編號柱A爲每個填充膠原G細胞? –

+0

是如果列G中已經有數據,那麼它在列A中按順序分配一個值,但是我想通過點擊按鈕來分配這個值。爲按鈕我有一個鏈接,但如何合併按鈕與此代碼請幫助我... http://office.microsoft.com/en-us/excel-help/add-a-button-and-assign-a-宏在工作表中-HP010236676.aspx – Nitin

+0

看到我下面的答案 –

回答

1
  1. 插入在Developer工具欄通過Insert一個Form按鈕。

    enter image description here

  2. 一個模塊在下面的代碼粘貼,然後用鼠標右鍵單擊該按鈕,然後單擊assign Macro

  3. Assign Macro對話框中選擇Button1_Click,就完成了。

代碼

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 
+0

謝謝很多...它的工作... – Nitin

+0

很高興成爲幫助:) –

+0

@Nitin如果它可以幫助你比接受通過點擊答案旁邊的答案來回答問題 –