VBA Excel:如何循环访问包含字母数字或数字内容的单元格的列B?

我有列B中的数据,我需要循环,然后将D列中的相应值复制到同一工作簿中的另一个工作表。 我需要一个编写的代码来search列B中的每个值,在同一行中返回列D中相应的值,然后从给定范围中按顺序查找下一个编号(在这种情况下,我已将其设置为7到10 )。

因此,循环遍历B列,按顺序查找值7,7a,8,9,10(即使较大的值位于较低的值之前,也是如此),然后将D列中的相应值复制到另一个表中。 Sheet3中的Excel数据图表(不需要A列):

ABCDE

1 1a 78.15 77.68 This is row 7 1a 2 77.18 76.92 2 3 76.92 76.63 3 4 76.13 75.78 4 4a 75.78 75.21 4a 5 75.11 74.87 5 5a 74.87 74.69 5a 6 73.94 73.6 6 6a 73.1 72.71 6a 6b 72.41 72.18 6b 10 72.18 71.6 10 11 71.3 70.89 11 12 70.89 69.83 12 13 69.83 68.68 13 14 68.68 67.68 14 15 67.63 66.46 15 16 66.01 64.84 16 16a 64.24 63.72 16a 16b 56.82 56.37 16b 16c 56.37 55.18 16c OUT 47.28 47.27 7 7a 83.12 76.07 7a 8 76.17 75.99 8 9 74.79 74.41 9 6 74.51 74 This is row 31 

我的问题:当代码遇到一个包含字母和数字的单元格时,它跳过该单元格并移动到该范围中仅包含数字的下一个单元格。 如何在search条件中编辑/重写代码以包含字母数字值?

这里是我的代码循环通过列B,但不包括字母和数字的单元格:

 Sub EditBEST() Dim Startval As Long Dim Endval As Long 'Finds values corresponding 'to input in B and C Dim LastRow As Long LastRow = Sheets("Sheet3").range("B" & Rows.Count).End(xlUp).Row Startval = Worksheets("Sheet3").Cells(1, "O").Value Endval = Worksheets("Sheet3").Cells(1, "P").Value StartRow = 2 'row that first value will be pasted in For x = 7 To LastRow 'decides range to search thru in "Sheet3" If Sheets("Sheet3").Cells(x, 2).Value >= 7 And Sheets("Sheet3").Cells(x, 2).Value <= 10 Then 'if cell is not blank Sheets("Sheet4").Cells(StartRow, 2).Value = _ Sheets("Sheet3").Cells(x, 4).Value 'copy/select cell value in D StartRow = StartRow + 1 'cell.Offset(0, 1).Value = End If If Sheets("Sheet3").Cells(x, 3) >= 7 And Sheets("Sheet3").Cells(x, 3).Offset(0, 1) <= 10 Then Sheets("Sheet4").Cells(StartRow, 2).Value = _ Sheets("Sheet3").Cells(x, 5).Value StartRow = StartRow + 1 End If Next x End Sub 

谢谢

你遇到的主要问题是你有条件检查过滤掉任何string值。 正如@级别'Eh'培根指出的,你需要提供一些方法来处理string值。


你也有一些错误或误导性的评论。

例如,在这里,您添加了“如果单元格不是空白”的注释,但这不是您实际检查的内容。

 If Sheets("Sheet3").Cells(x, 2).Value >= 7 And Sheets("Sheet3").Cells(x, 2).Value <= 10 Then 'if cell is not blank 

如果你想检查一个单元是否空白,你可以检查它的长度。 例如:

 If Len(Sheets("Sheet3").Cells(x, 2).Value) > 0 Then 

现在,这个程序实际上并不是完全必要的,但我只是想指出,因为你的评论表明你正在尝试做一些不同于你的代码的东西。


我没有testing过你的代码,但是我写了一个函数为你抽出一个string。 这一切都没有经过testing,所以你可能需要debugging它,但应该得到你的string问题sorting。

 Sub EditBEST() Dim Startval As Long Dim Endval As Long 'Finds values corresponding 'to input in B and C Dim StartOutputRow as Long Dim LastRow As Long Dim Val as Long Dim Val2 as Long LastRow = Sheets("Sheet3").range("B" & Rows.Count).End(xlUp).Row Startval = Worksheets("Sheet3").Cells(1, "O").Value Endval = Worksheets("Sheet3").Cells(1, "P").Value StartOutputRow =2 'first row we will output to OutputRow = StartOutputRow 'row of the cell to which matching values will be pasted For x = 7 To LastRow Val = GetSingleFromString(Sheets("Sheet3").Cells(x, 2).Value) If Val >= 7 And Val <= 10 Then 'if value is within range Sheets("Sheet4").Cells(OutputRow , 2).Value = _ Sheets("Sheet3").Cells(x, 4).Value 'copy cell value from D @the current row to column B @the output row OutputRow = OutputRow + 1 'Next value will be on the next row End If Val = GetSingleFromString(Sheets("Sheet3").Cells(x, 3).Value) Val2 = GetSingleFromString(Sheets("Sheet3").Cells(x, 3).Offset(0, 1).Value) If Val >= 7 And Val2 <= 10 Then Sheets("Sheet4").Cells(OutputRow , 2).Value = _ Sheets("Sheet3").Cells(x, 5).Value 'copy cell value from E @the current row to column B @the output row OutputRow = OutputRow + 1 End If Next x 'Sort the output: Sheets("Sheet4").Range("B:B").Sort key1:=Range(.Cells(StartOutputRow,2), order1:=xlAscending, header:=xlNo End Sub Private Function GetSingleFromString(ByVal InString As String) As Single If Len(InString) <= -1 Then GetSingleFromString = -1 Exit Function End If Dim X As Long Dim Temp1 As String Dim Output As String For X = 1 To Len(InString) Temp1 = Mid(InString, X, 1) If IsNumeric(Temp1) Or Temp1 = "." Then Output = Output & Temp1 Next If Len(Output) > 0 Then GetSingleFromString = CSng(Output) Else GetSingleFromString = -1 End If End Function