VBAvariables没有取值

我有以下代码:

Dim intCounter As Integer Dim intCounter2 As Integer Dim rngExchange As Range Dim rngExchange2 As Range Dim control As Integer Dim control2 As Integer intCounter = 1 intCounter2 = 1 Do While Worksheets("Sheet2").Cells(2, intCounter2) <> "" If Worksheets("Sheet2").Cells(2, intCounter2).Value = "Isin" Then With Worksheets("Sheet2") Set rngExchange2 = .Range(.Cells(2, intCounter2), .Cells(2, intCounter2)) control2 = intCounter2 End With End If intCounter2 = intCounter2 + 1 Loop 

代码find标题为Isin的列,然后使用variablescontrol2来操作该列。 但是,首先rngExchange2不接受值“Isin”,其次控制2variables保持为0.您可以请帮忙。

你的循环的逻辑是错误的,它在前面的问题中得到了纠正。

但是我build议你使用更清晰的东西来查找特定文本的列,比如Range.Find方法:

 Sub test_Anton() Dim SearchString As String Dim ConTrol2 As Integer Dim wS As Worksheet Dim cF As Range SearchString = "Isin" Set wS = ThisWorkbook.Sheets("Sheet2") With wS .Activate With .Range("2:2") .Cells(1, 1).Activate 'First, define properly the Find method Set cF = .Find(What:=SearchString, _ After:=ActiveCell, _ LookIn:=xlValues, _ LookAt:=xlPart, _ SearchOrder:=xlByRows, _ SearchDirection:=xlNext, _ MatchCase:=False, _ SearchFormat:=False) 'If there is a result, stock the column If Not cF Is Nothing Then ConTrol2 = cF.Column Else MsgBox SearchString & " not found in " & wS.Name, vbOKOnly + vbCritical End If End With '.Range("2:2") End With 'wS MsgBox SearchString & " found in column " & ConTrol2 & " in sheet " & wS.Name, vbOKOnly + vbInformation End Sub 

你在哪里使用.cells(2,intCounter2),第一个参数指定行,第二个参数是你的variables是列。 我已经用自己的工作表testing了你的代码,它似乎能够find“Isin”。

我会检查你是否在正确的地方,然后检查Isin的情况,以确保它符合你的VBA代码,因为它是区分大小写的。

这应该有希望为你工作。