[英]Repeating a Macro in Excel untill it reaches a blank cell
我對excel中的VBA還是很陌生,我已經記錄並修改了一個獲取值的宏,然后基於“ IF”語句,它使用答案來計算特定單元格中的值。 我需要重復此操作,直到A列中有一個空白單元格。
我的數據是
在前面的4列中,零件編號報告的實際浪費是我的代碼,我需要重復執行直到A列為空白。
Sub MissingMix()
'
' MissingMix Macro
' Calculates Missing Mix based on scrap
'
' Keyboard Shortcut: Ctrl+q
Application.Goto Reference:="R1C1"
Application.Goto Reference:="R3C8"
ActiveCell.FormulaR1C1 = "=IF(RC[-5]-RC[-6]>0,SUM(RC[-5]-RC[-6])*RC[+1],"""")"
Application.Goto Reference:="R3C9"
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-8],'QAD Weights'!RC[-8]:R[37]C[-5],4)"
End Sub
用這個:
' get active sheet
Dim CurrentSheet As Worksheet
Set CurrentSheet = ActiveWorkbook.ActiveSheet
' defining starting row
Dim Values As Range
Set Values = Rows(5)
For i = 2 To Values.Cells.Count - 1
If Values.Cells(i).Text <> "" Then
' Do Something ...
End If
Next
您也可以使用If Not IsEmpty(Cells(i).Value) Then ...
編輯
您可以使用另一種方法來處理此問題。
Dim CurrentSheet As Worksheet
Set CurrentSheet = ActiveWorkbook.ActiveSheet
' getting the lastrow by jumping to the last filled row of cell(1,1)
' cell(1,1) means the cell at row=1 and column=1 (column "A")
Dim LastRow As Long
LastRow = CurrentSheet.Cells(1, 1).End(xlDown).Row
MsgBox LastRow
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.