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

我有一点棘手(对我)任务要做。 我想要创build一个macros,它将在指定列的最后一个单元格(根据其标题)中find一个值。 所以例如它会在名为“test”的列中find最后一个值。

对于你来说是不是很棘手,因为你没有尝试,或者因为你尝试过而无法弄清楚? 如果你先提供代码,那么总是很好,所以我们可以看到你到底在做什么,以及如何帮助它工作。

无论如何,我把这些东西放在一起就是你要求的。 它假设我们将在Sheet 1进行search。 在Visual Basic Editor (VBE) ,打开Sheet1并粘贴此代码。 然后,您可以像使用常规macros一样使用它(请确保更改了标题栏值和searchstring以满足您的需要)。

 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