简体   繁体   English

Excel VBA代码理解

[英]Excel VBA code understanding

I'm trying build a excel based input form, I have found something online and I'm trying to understand these codes: 我正在尝试构建一个基于excel的输入表单,我在网上找到了一些东西,我正在尝试理解这些代码:

Dim Hsheet,Isheet As Worksheet
Dim NextRow, oCol As Long
Dim MyRng, MyCell As Range
Dim MyCopy, ClearCells As String

Set Hsheet = Worksheet("InputForm")
Set ISheet = Worksheet("Database")

This is the part I don't understand, can someone explain to me please? 这是我不明白的部分,有人可以向我解释一下吗?


With Hsheet
     nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With

With Isheet
    Set myRng = .Range(MyCopy)

    If Application.CountA(myRng) <> myRng.Cells.Count Then
        MsgBox "Please fill in all the cells!"
        Exit Sub
    End If
End With

And also this part, can someone explain to me please? 还有这一部分,有人可以向我解释一下吗?

With Hsheet
    .Cells(nextRow, "a").Value = Application.UserName
    oCol = 1
    For Each myCell In MyRng.Cells
        Hsheet.Cells(NextRow, oCol).Value = myCell.Value
        oCol = oCol + 1
    Next myCell
End With

Thanks in advance :) 提前致谢 :)

With Isheet
  On Error Resume Next
     With .Range(ClearCells).Cells.SpecialCells(xlCellTypeConstants)
          .ClearContents
          Application.Goto .Cells(1) ', Scroll:=True
     End With
  On Error GoTo 0
End With

I can explain what the code does but there are few things which I would like to mention :) 我可以解释代码的作用,但是我想提几件事情:)

A 一种

Dim Hsheet,Isheet As Worksheet
Dim NextRow, oCol As Long
Dim MyRng, MyCell As Range
Dim MyCopy, ClearCells As String

This is not the right way to declare the variables/objects For example if you consider this line which is 这不是声明变量/对象的正确方法。例如,如果您考虑这一行是什么

Dim Hsheet,Isheet As Worksheet

Here, only Isheet has been declared as a worksheet and not Hsheet . 这里,只有Isheet被声明为工作表而不是Hsheet The Hsheet automatically becomes a variant . Hsheet自动成为变体 The right way is 正确的方法是

Dim Hsheet As Worksheet, Isheet As Worksheet
Dim NextRow As Long, oCol As Long
Dim MyRng As Range, MyCell As Range
Dim MyCopy As String, ClearCells As String

B

With Hsheet
     nextRow = .Cells(.Rows.Count, "A").End(xlUp).Offset(1, 0).Row
End With

What this code does is it tries to find the last row which has data in Col A and then offsets one row down to get the next empty row so that you can write to it. 这段代码的作用是它试图找到在Col A中有数据的最后一行,然后向下偏移一行以获得下一个空行,以便您可以写入它。

Another way to write the same thing is mentioned here So the above code can also be written as 这里提到了编写相同内容的另一种方法所以上面的代码也可以写成

With Hsheet
     nextRow = .Range("A" & .Rows.Count).End(xlUp).Row + 1
End With

C C

With Isheet
    Set myRng = .Range(MyCopy)

    If Application.CountA(myRng) <> myRng.Cells.Count Then
        MsgBox "Please fill in all the cells!"
        Exit Sub
    End If
End With

I believe MyCopy is supposed to hold some value which I cannot see it in your code. 我相信MyCopy应该具有一些我在代码中看不到的值。 Assuming that it holds a valid cell address, what the code is trying to do is to ensure that all cells are filled up by comparing the cells count vs the number of cells filled up. 假设它拥有一个有效的单元格地址,代码尝试做的是通过比较单元格数量与填充的单元格数量来确保填满所有单元格。

D d

With Hsheet
    .Cells(NextRow, "a").Value = Application.UserName
    oCol = 1
    For Each myCell In MyRng.Cells
        Hsheet.Cells(NextRow, oCol).Value = myCell.Value
        oCol = oCol + 1
    Next myCell
End With 

This is also pretty straightforward. 这也非常简单。 The code stores the UserName in the next available cell in Col A and then stores the values from Range MyRng in Sheet Isheet in Col A of Sheet Hsheet 代码将UserName存储在Col A中的下一个可用单元格中,然后将Range MyRng中的值存储在Sheet Isheet的Col A中的Sheet Hsheet

HTH HTH

Your piece of code seems to do some copy from one sheet to another, adding the user name on top of a range. 您的代码似乎从一个工作表到另一个工作表进行了一些复制,在一个范围之上添加了用户名。
However the string variable MyCopy does not seem to be initialized, therefore I don't think running this macro as is would produce any desired result (unless the Range function returns some cells when called with an empty string ? I don't know its specs). 但是,字符串变量MyCopy似乎没有被初始化,因此我不认为运行这个宏会产生任何所需的结果(除非Range函数在使用空字符串调用时返回一些单元格?我不知道它的规格)。

I don't remember Excel VBA perfectly, but I think that : 我不完全记得Excel VBA,但我认为:

Cells(.Rows.Count, "A") selects the cell located on the last row, column "A". Cells(.Rows.Count, "A")选择位于最后一行的单元格“A”。
End(xlUp) moves the selection to the top of the range of contiguous non-empty cells (like pressing CTRL + UP in Excel, I think). End(xlUp)将选择移动到连续的非空单元格范围的顶部(如我在Excel中按CTRL + UP )。
Offset(1, 0) moves the selection to one cell to the bottom. Offset(1, 0)将选择移动到一个单元格到底部。 Row returns that cell's row number. Row返回该单元格的行号。

So your first code block sets the row number of the second row of the last range of non-empty cells in column A, to the variable nextRow . 因此,您的第一个代码块将A列中最后一系列非空单元格的第二行的行号设置为变量nextRow

You can follow the same reasoning to understand the purpose of all the other code blocks. 您可以按照相同的推理来理解所有其他代码块的用途。 I suggest your search MSDN's VBA for Excel documentation websites to get more information about the meaning of each function you don't understand yet. 我建议您搜索MSDN的VBA for Excel文档网站,以获取有关您尚不了解的每个功能的含义的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM