Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 在指定列的最后一个单元格中查找值(根据其标题)_Excel_Vba - Fatal编程技术网

Excel 在指定列的最后一个单元格中查找值(根据其标题)

Excel 在指定列的最后一个单元格中查找值(根据其标题),excel,vba,Excel,Vba,我有一件(对我来说)棘手的任务要做。我想创建一个宏,它将在指定列的最后一个单元格中找到一个值(根据其标题)。例如,它将在一个名为“test”的列中找到最后一个值 这对你来说很棘手,是因为你没有尝试,还是因为你尝试了却没能找到答案?如果你先提供你的代码,这样我们就可以看到你到底在做什么,以及我们如何帮助它工作,这总是很好的 不管怎么说,这里有一个我做的东西可以满足你的要求。它假设我们将在表1中搜索。在Visual Basic编辑器(VBE)中,打开Sheet1并粘贴此代码。然后,您可以像使用常规宏

我有一件(对我来说)棘手的任务要做。我想创建一个宏,它将在指定列的最后一个单元格中找到一个值(根据其标题)。例如,它将在一个名为“test”的列中找到最后一个值

这对你来说很棘手,是因为你没有尝试,还是因为你尝试了却没能找到答案?如果你先提供你的代码,这样我们就可以看到你到底在做什么,以及我们如何帮助它工作,这总是很好的

不管怎么说,这里有一个我做的东西可以满足你的要求。它假设我们将在
表1
中搜索。在
Visual Basic编辑器(VBE)
中,打开
Sheet1
并粘贴此代码。然后,您可以像使用常规宏一样使用它(确保更改headerrow值和搜索字符串以满足您的需要)

代码

Public Sub FindValueInColumn()

    Dim headerRow As Integer
    Dim totalColumnsInHeaderRow As Integer
    Dim searchColumn As Integer
    Dim lastRowInSearchColumn As Integer
    Dim columnSearchString As String

    headerRow = 1   'change this to correct header row
    totalColumnsInHeaderRow = Cells(headerRow, Columns.Count).End(xlToLeft).Column
    columnSearchString = "Test" 'change to the text to search
    searchColumn = 0

    'for every column that has a value in it in the header row
    Dim currentColumn As Integer
    For currentColumn = 1 To totalColumnsInHeaderRow
        'if that value equals what we're looking for (in this case, "Test")
        If StrComp(Cells(headerRow, currentColumn).Value, columnSearchString, vbTextCompare) = 0 Then
            'save the column that contains the value, then exit the loop
            searchColumn = currentColumn
            Exit For
        End If
    Next

    'if the column of interest exists in the header row
    If searchColumn > 0 Then
        'Set F2 equal to the last value in that column
        lastRowInSearchColumn = Cells(Rows.Count, searchColumn).End(xlUp).Row
        Range("F2").Value = Cells(lastRowInSearchColumn, searchColumn).Value
    End If

End Sub
之前

Public Sub FindValueInColumn()

    Dim headerRow As Integer
    Dim totalColumnsInHeaderRow As Integer
    Dim searchColumn As Integer
    Dim lastRowInSearchColumn As Integer
    Dim columnSearchString As String

    headerRow = 1   'change this to correct header row
    totalColumnsInHeaderRow = Cells(headerRow, Columns.Count).End(xlToLeft).Column
    columnSearchString = "Test" 'change to the text to search
    searchColumn = 0

    'for every column that has a value in it in the header row
    Dim currentColumn As Integer
    For currentColumn = 1 To totalColumnsInHeaderRow
        'if that value equals what we're looking for (in this case, "Test")
        If StrComp(Cells(headerRow, currentColumn).Value, columnSearchString, vbTextCompare) = 0 Then
            'save the column that contains the value, then exit the loop
            searchColumn = currentColumn
            Exit For
        End If
    Next

    'if the column of interest exists in the header row
    If searchColumn > 0 Then
        'Set F2 equal to the last value in that column
        lastRowInSearchColumn = Cells(Rows.Count, searchColumn).End(xlUp).Row
        Range("F2").Value = Cells(lastRowInSearchColumn, searchColumn).Value
    End If

End Sub

之后


我真的很喜欢看到不同的编码风格。显然,剥这只猫皮的方法不止一种

Sub Last_Cell_In_Column()
   Dim sh As Worksheet
   Dim col As Long

   Set sh = ThisWorkbook.ActiveSheet
   col = 0
   col = sh.Rows(1).Find("test").Column
   If col > 0 Then
      MsgBox (sh.Cells(sh.Rows.Count, col).End(xlUp).Value)
   End If
End Sub

你试过什么?也许录制一个宏,然后尝试编辑它,然后在遇到问题时提出问题,而不是询问codeHello Sam。在我发布Stackoverflow上的任何问题之前,我总是尝试自己解决问题。不是每次我都能做到,所以我来了。我的大部分问题都是由我当前的代码组成的。然而,我对VBA的了解有限,因此在这种特殊情况下,我无法修改我在互联网上找到的代码,使其更接近我想要达到的状态。无论如何,您的代码在F2中提供了结果,而这对我来说不是必需的,因为我有这种类型的代码。我唯一需要的是选择测试列中的最后一个值。通常情况下,发布您尝试过的内容是一个好主意,这样我们就可以看到它,并在需要的地方引导您走向正确的方向。让代码将最后一个值输出到
F2
中,这是一个向您展示其工作原理的示例。如果只需选择最后一个单元格,则可以使用
单元格(lastRowInSearchColumn,searchColumn)。选择
代替
范围(“F2”).Value=…
它工作得很好!我知道没有代码的帖子有点不对劲,但正如前面提到的,我当时想不出任何有价值的东西。感谢Sam帮助我,即使我只提供了这么少的细节。我很感激。没问题-很高兴能帮到你。祝你的项目顺利。同意,有很多方法可以做到这一点g、 +1用于选择更短、更简单的路线;)