Tefito

He tauira o te Tohutono Excel i tuhia ki te VBA

Ko nga tauira tonotono Excel ngawari e whai ake nei i tuhia ma te whakamahi i te VBA 

Te taima panui: 3 minuti

He tauira VBA ma te whakamahi Array

Ko te tikanga iti e whai ake nei ka panui i nga uara mai i nga pūtau kei te pou A o te raumahi hohe, tae noa ki te tutaki ki tetahi pūtau tuwhera. Ka penapena nga uara ki roto i te huinga. Ko tenei tauira tonotono Excel ngawari e whakaatu ana i te whakamahinga o:

  • Whakapuakanga taurangi;
  • Nga rarangi hihiko;
  • He huringa Do Until;
  • Tirohia nga pūtau kei te raumahi Excel onāianei;
  • Ko te mahi VBA Ubound builtin (e whakahoki mai ana i te taupū teitei o te huānga).
' 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

Ko te tikanga ka pupuri i nga uara ki te pou A o te raumahi hohe i roto i te huranga, me mahara:

  • Te huringa Do Until ka tango i nga uara o ia pūtau kei te tīwae A o te pepa mahi hohe, me te kore e aro ki nga pūtau pātea
  • Te huru"If UBound(dCellValues) < iRow" ka tirohia he nui te huinga dCellValues ​​hei pupuri i nga korero, ki te kore, whakamahia te ReDim hei whakanui ake i te rahi o te huinga ma te 10
  • Ka mutu, ko te matauranga​​dCellValues(iRow) = Cells(iRow, 1).Value” Pupuri ana i te pūtau o nāianei ki te huānga CellValues

He tauira VBA me nga mahi pangarau

Ko te tikanga iti e whai ake nei ka panui i nga uara mai i te pou A o te raumahi kua whakaingoatia ko “Sheet2” me te mahi taurangi i runga i nga uara. Ko nga uara ka puta ka taia ki te pou A o te raumahi hohe o naianei.

Ko tenei tonotono e whakaatu ana:

Panui houtanga
Kaua e ngaro nga korero tino nui mo te mahi auaha. Waitohu ki te whiwhi i a raatau ma te imeera.
  • Whakapuakanga taurangi;
  • Ahanoa Excel (tauwhāitihia, te whakamahi i te Kuputuhi Tautuhi me pehea te uru ki te ahanoa 'Tīwae' mai i te ahanoa 'Rau');
  • He huringa Do Until;
  • Uru ki nga raumahi me nga awhe pūtau i te pukamahi Excel o naianei.
' 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

He tauira VBA me te tuhi ra whakarereke

Me tuhi he tonotono VBA ngawari e ka ana ina whakahōuhia he pūtau kei roto i te awhe motuhake o to tatou rau. Mehemea kei te pirangi koe ki te whai i nga huringa i te pou B (B4 ki B11) ka tuhi i te ra me te wa o te huringa ki te pou A.
Kia penei ta tatou haere:

  • I te ripa Developer pāwhiritia te kōwhiringa "Visual Basic” ki te whakatuwhera i te ētita VBA.
  • I roto i te ētita VBA, pāwhiri-rua i te ētita waehere e pā ana ki te Rau2.
  • Kōwhiria te Pepamahi mai i te ripa matau (mauī rānei) ka kōwhiri i te kōwhiringa Huri.
  • Tāpiri waehere 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

Tiakina te pukamahi me nga tonotono kua whakahohea (hei tauira, hei konae .xlsm).


Inaianei, i nga wa katoa ka whakahōuhia he pūtau i te tīwae B (mai i te rarangi 1 ki te rarangi 10), ka whakaatu aunoa te pūtau o te tīwae A i te rā me te wā o nāianei.

Ercole Palmeri

Panui houtanga
Kaua e ngaro nga korero tino nui mo te mahi auaha. Waitohu ki te whiwhi i a raatau ma te imeera.

Nga tuhinga o mua

Kei a Veeam te tautoko tino whanui mo te ransomware, mai i te whakamarumaru ki te whakautu me te whakaora

Ka whakarato tonu a Coveware na Veeam i nga ratonga whakautu mai i nga aitua. Ka tukuna e Coveware nga mahi whakawai me te whakatikatika…

23 Paenga-whāwhā 2024

Hurihuri Kakariki me te Mamati: He pehea te Huringa Matapae i te Hurihanga i te Ahumahi Hinu me te Hau

Ko te tiaki matapae kei te huri haere i te waahanga hinu me te hau, me te huarahi auaha me te kaha ki te whakahaere tipu.…

22 Paenga-whāwhā 2024

Ka whakaarahia e te kaiwhakahaere antitrust UK te whakaoho BigTech mo GenAI

Kua tukuna e te UK CMA tetahi whakatupato mo te whanonga a Big Tech i te maakete mohio. I reira…

18 Paenga-whāwhā 2024

Casa Green: hurihanga pūngao mo te oranga tonutanga i Itari

Ko te Whakatau "Nga Whare Kaariki", i hangaia e te Kotahitanga o Europi hei whakarei ake i te kaha o te kaha o nga whare, kua mutu tana mahi ture me te…

18 Paenga-whāwhā 2024