זמן קריאה משוער: 3 דקות
הליך המשנה הבא קורא ערכים מהתאים בעמודה A של גליון העבודה הפעיל, עד שהוא נתקל בתא ריק. הערכים מאוחסנים במערך. דוגמא מאקרו פשוטה זו של Excel ממחישה את השימוש ב:
Do Until
;Ubound
ingebouwd (המחזיר את המדד הגבוה ביותר של מערך).' Sub procedure store values in Column A of the active Worksheet
' into an array
Sub GetCellValues()
Dim iRow As Integer ' stores the current row number
Dim dCellValues() As Double ' array to store the cell values
iRow = 1
ReDim dCellValues(1 To 10)
' Do Until loop to extract the value of each cell in column A
' of the active Worksheet, as long as the cell is not blank
Do Until IsEmpty(Cells(iRow, 1))
' Check that the dCellValues array is big enough
' If not, use ReDim to increase the size of the array by 10
If UBound(dCellValues) < iRow Then
ReDim Preserve dCellValues(1 To iRow + 9)
End If
' Store the current cell in the CellValues array
dCellValues(iRow) = Cells(iRow, 1).Value
iRow = iRow + 1
Loop
End Sub
ההליך מאחסן את הערכים בעמודה A של גליון העבודה הפעיל במערך, שימו לב:
Do Until
מחלץ את הערכים של כל תא בעמודה A של גליון העבודה הפעיל, תוך התעלמות מתאים ריקיםIf UBound(dCellValues) < iRow
” בודק שמערך dCellValues גדול מספיק כדי להכיל את המידע, אם לא, השתמש ב-ReDim כדי להגדיל את גודל המערך ב-10dCellValues(iRow) = Cells(iRow, 1).Value
"מאחסן את התא הנוכחי במערך CellValuesהליך המשנה הבא קורא את הערכים מעמודה A של גליון העבודה בשם "Sheet2" ומבצע פעולות אריתמטיות על הערכים. הערכים המתקבלים מודפסים בעמודה A של גליון העבודה הפעיל הנוכחי.
מאקרו זה ממחיש:
Do Until
;' Sub procedure to loop through the values in Column A of the Worksheet
' "Sheet2", perform arithmetic operations on each value, and write the
' result into Column A of the current Active Worksheet ("Sheet1")
Sub Transfer_ColA()
Dim i As Integer
Dim Col As Range
Dim dVal As Double
' Set the variable 'Col' to be Column A of Sheet 2
Set Col = Sheets("Sheet2").Columns("A")
i = 1
' Loop through each cell of the column 'Col' until
' a blank cell is encountered
Do Until IsEmpty(Col.Cells(i))
' Apply arithmetic operations to the value of the current cell
dVal = Col.Cells(i).Value * 2 + 1
' The command below copies the result into Column A
' of the current Active Worksheet - no need to specify
' the Worksheet name as it is the active Worksheet.
Cells(i, 1) = dVal
i = i + 1
Loop
End Sub
בוא נכתוב מאקרו VBA פשוט שמופעל כאשר תא בטווח מסוים של הגיליון שלנו מתעדכן. נניח שאתה רוצה לעקוב אחר שינויים בעמודה B (B4 עד B11) ורשום את התאריך והשעה של השינוי בעמודה A.
בואו נמשיך כך:
Developer
לחץ על האפשרות "Visual Basic
" כדי לפתוח את עורך VBA. Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B1:B10")) Is Nothing Then
Target.Range("A1:A1").Value = Now
End If
End Sub
שמור את חוברת העבודה עם פקודות מאקרו מופעלות (לדוגמה, כקובץ .xlsm).
כעת, בכל פעם שאנו מעדכנים תא בעמודה B (משורה 1 עד שורה 10), התא בעמודה A יציג אוטומטית את התאריך והשעה הנוכחיים.
Ercole Palmeri
Coveware מבית Veeam תמשיך לספק שירותי תגובה לאירועי סחיטה בסייבר. Coveware תציע יכולות זיהוי פלילי ותיקון...
תחזוקה חזויה מחוללת מהפכה בתחום הנפט והגז, עם גישה חדשנית ויזומה לניהול מפעלים.…
ה-CMA הבריטי פרסם אזהרה על התנהגותה של ביג טק בשוק הבינה המלאכותית. שם…
צו "הבתים הירוקים", שנוסח על ידי האיחוד האירופי כדי לשפר את היעילות האנרגטית של מבנים, סיכם את תהליך החקיקה שלו עם...