如何获取一个值来调用excel中的函数

时间:2018-04-25 18:28:52

标签: excel-vba excel-2007 vba excel

我正在尝试获取值返回以在Excel中运行调用函数

Sub ID_Num是我想在Sub SubmitButton的第一个ws.range中调用的,但是我无法弄清楚如何调用函数,所以我尝试从函数中提取代码,如下所示。这工作一次并添加一个数字,但在此之后它停止 - 与ActiveCell.Row部分

有关
ws.Range("A" & LastRow).Value = "=MAX(A2:A" + CStr(ActiveCell.Row - 1) + ")+1"    'Adds the ID into Col A & Last Blank Row"

我目前的代码是:

Sub ID_Num()

 Dim LastRow As Long, ws As Worksheet

Set ws = Sheets("Data")

ActiveCell.Formula = "=MAX(A1:A" + CStr(ActiveCell.Row - 1) + ")+1"

ActiveCell.Formula = ActiveCell.Value 'Convert formula to a value

End Sub



Sub SubmitButton()

Dim LastRow As Long, ws As Worksheet

Set ws = Sheets("Data")

LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row + 1 'Finds the last blank row

ws.Range("A" & LastRow).Value = "=MAX(A2:A" + CStr(ActiveCell.Row - 1) + ")+1"    'Adds the ID into Col A & Last Blank Row"
ws.Range("B" & LastRow).Value = Environ("userdomain") & "\" & Environ("username") 'Adds the UserName into Col B & Last Blank Row
ws.Range("C" & LastRow).Value = Format(Now(), "dd/mm/yyyy hh:mm") 'Adds the Date_Time into Col C & Last Blank Row
ws.Range("D" & LastRow).Value = Sheets("Form").Range("C5") 'Adds the Name into Col D & Last Blank Row
ws.Range("E" & LastRow).Value = Sheets("Form").Range("C7") 'Adds the Service into Col E & Last Blank Row
ws.Range("F" & LastRow).Value = Sheets("Form").Range("F7") 'Adds the Team into Col F & Last Blank Row
ws.Range("G" & LastRow).Value = Sheets("Form").Range("C9") 'Adds the Cycle into Col G & Last Blank Row
ws.Range("H" & LastRow).Value = Sheets("Form").Range("F5") 'Adds the Date into Col H & Last Blank Row
ws.Range("I" & LastRow).Value = Sheets("Form").Range("C11") 'Adds the Section into Col I & Last Blank Row
ws.Range("J" & LastRow).Value = Sheets("Form").Range("C13") 'Adds the Functionality into Col J & Last Blank Row
ws.Range("K" & LastRow).Value = Sheets("Form").Range("C15") 'Adds the Form into Col K & Last Blank Row
ws.Range("L" & LastRow).Value = Sheets("Form").Range("C17") 'Adds the Report into Col L & Last Blank Row
ws.Range("M" & LastRow).Value = Sheets("Form").Range("C19") 'Adds the System1_ID into Col M & Last Blank Row
ws.Range("N" & LastRow).Value = Sheets("Form").Range("F19") 'Adds the System2_ID into Col N & Last Blank Row
ws.Range("O" & LastRow).Value = Sheets("Form").Range("B22") 'Adds the Comment into Col O & Last Blank Row


End Sub

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

CStr(ActiveCell.Row - 1)替换为CStr(LastRow - 1)