VBA根据input值查找下一列

在我现在要写的一个程序中,我把两列数字进行计算。 我不知道这两列的位置,直到用户告诉我(他们在工作簿中的单元格中input我的代码所在的列值)。

例如,如果用户input“A”和“B”作为全部信息所在的列,则可以基于这些值进行计算。 同样,如果他们想要分析另一个工作表(或工作簿),并且这些列是“F”和“G”,他们可以input这些。 问题是我要求用户input这两列以及其他四列(最后四列是结果列)。 我希望我能够做到这一点,但现在灵活性是可以接受的。

我的问题是,如果我给出了一些信息的位置(比如说“F”),那么我怎样才能找出在这个input值之后或之前的列。 所以如果我只给了“F”,我可以创build一个variables来保存“G”列。

下面是我需要做这个新问题之前variables如何工作的例子:

Dim first_Column As String Dim second_Column As String Dim third_Column As String first_Column = Range("B2").Text second_Column = Range("B3").Text third_Column = Range("B4").Text 

这里单元格B2-B4是用户input值的地方。 一般来说,我希望能够不再有B3和B4了。 我觉得偏移(0,1)可能能够帮助某种方式,但到目前为止,我一直无法正确实施。

谢谢,

Jesse Smothermon

这里有两个函数可以帮助你处理> Z的列。 它们将列的文本forms转换为列索引(作为Long值),反之亦然:

 Function ColTextToInt(ByVal col As String) As Long Dim c1 As String, c2 As String col = UCase(col) 'Make sure we are dealing with "A", not with "a" If Len(col) = 1 Then 'if "A" to "Z" is given, there is just one letter to decode ColTextToInt = Asc(col) - Asc("A") + 1 ElseIf Len(col) = 2 Then c1 = Left(col, 1) ' two letter columns: split to left and right letter c2 = Right(col, 1) ' calculate the column indexes from both letters ColTextToInt = (Asc(c1) - Asc("A") + 1) * 26 + (Asc(c2) - Asc("A") + 1) Else ColTextToInt = 0 End If End Function Function ColIntToText(ByVal col As Long) As String Dim i1 As Long, i2 As Long i1 = (col - 1) \ 26 ' col - 1 =i1*26+i2 : this calculates i1 and i2 from col i2 = (col - 1) Mod 26 ColIntToText = Chr(Asc("A") + i2) ' if i1 is 0, this is the column from "A" to "Z" If i1 > 0 Then 'in this case, i1 represents the first letter of the two-letter columns ColIntToText = Chr(Asc("A") + i1 - 1) & ColIntToText ' add the first letter to the result End If End Function 

现在,您的问题可以轻松解决,例如

 newColumn = ColIntToText(ColTextToInt(oldColumn)+1) 

编辑相应的mwolfe02的评论:

当然,如果你对列名不感兴趣,但是只想得到用户给定的列下面给定行中特定单元格的范围对象,这个代码是“过度杀伤”的。 在这种情况下,一个简单的

  Dim r as Range Dim row as long, oldColumn as String ' ... init row and oldColumn here ... Set r = mysheet.Range(oldColumn & row).Offset(0,1) ' now use r to manipulate the cell right to the original cell 

会做的。

你正在Offset正确的轨道上。 下面是一个testing函数,展示了几种不同的方法:

 Sub test() Dim first_Column As String Dim second_Column As String Dim third_Column As String Dim r As Range first_Column = Range("B2").Text second_Column = Range("B2").Offset(1, 0).Text third_Column = Range("B2").Offset(2, 0).Text Debug.Print first_Column, second_Column, third_Column Set r = Range("B2") first_Column = r.Text Set r = r.Offset(1, 0) second_Column = r.Text Set r = r.Offset(1, 0) third_Column = r.Text Debug.Print first_Column, second_Column, third_Column End Sub 

更新 :重新读你的问题后,我意识到你正在尝试基于用户input的列字母做偏移量。 @ rskar的答案将会移动列字母,但是在代码中使用列号会更容易。 例如:

 Sub test() Dim first_Col As Integer, second_Col As Integer first_Col = Cells(, Range("B2").Text).Column second_Col = first_Col + 1 Cells.Columns(first_Col).Font.Bold = True Cells.Columns(second_Col).Font.Italic = True End Sub 

@ rskar的答案有一些语法问题。 但是,根据input列“letter”和右边所需的偏移量,生成抓取列“letter”的函数是有帮助的:

 Public Function GetNextCol(TheCol As String, OffsetRight As Integer) As String Dim TempCol1 As String Dim TempCol2 As String TempCol1 = Range(TheCol & "1").Address TempCol2 = Range(TempCol1).Offset(0, OffsetRight).Address(0, 0, xlA1) GetNextCol = Left(TempCol2, Len(TempCol2) - 1) End Function 

根据其他人的意见(他们都提出了有效的观点),使用OffsetAddress ,这是一个更好的解决scheme:

 Dim first_Column As String Dim second_Column As String Dim p As Integer first_Column = Range("B2").Text second_Column = _ Range(first_Column + ":" + first_Column).Offset(0, 1).Address(0, 0, xlA1) p = InStr(second_Column, ":") second_Column = Left(second_Column, p - 1) 

以上内容适用于任何有效的列名,包括“Z”和“AA”等。

在VBA中使用Asc()和Chr()函数,如下所示:

 Dim first_Column As String Dim second_Column As String first_Column = Range("B2").Text second_Column = Chr(Asc(first_Column) + 1) 

Asc( s )函数返回string“s”的第一个字符的ASCII码(整数,通常在0到255之间)。

Chr( c )函数返回一个string,其中包含与给定代码“c”对应的字符。

大写字母(A到Z)是ASCII代码65到90.只是谷歌ASCII更多的细节。

注意:只要first_Column在“A”和“Y”之间,上面的代码就可以了。 对于列“AA”等,它将需要更多的工作,但Asc()和Chr()仍然是编码的票。