我有一些可以解决年龄问题的代码,现在我的数据已经超过了z列,并且该代码不再有效,一旦它超过了z列,范围字符串的值为[29而不是AA29,AB29等。任何人都可以给我指导,说明如何进行这项工作,至少需要转到AW专栏。其他人告诉我,我犯了一个基本错误,因为我应该使用列号而不是名称。我不是专家,并且通过吞噬我在这里和其他地方找到的代码来设法使它正常工作。我的代码在下面,希望可以理解。
Sub Copy_picked_X1s_to_final_56()
Dim SelectedName() As String 'this array holds the "SelectedName" for a specific date
Dim AllNames() As Variant 'this array holds all the names in the "Final Assignments"
Dim NameCount1 As Long, NameCount2 As Long 'namecount 1 holds a count of the "SelectedName", namecount 2 holds a count of "AllNames" in the "Final Assignments"
Dim Services() As Variant 'this array holds a list of all the "Services"
Dim Columncounter As Long 'this array holds a count of all the columns that have "Services"
Dim NameCell As String 'this string holds the location of the cell in "Final Assignments" where the "SelectedName" appears
Dim location As String
Dim Name1 As String
Dim Name2 As String
Dim NameFound As Long
Dim FillArray As Long 'used to loop through the various arrays
Dim RangeString As String
Dim ServiceCount As Long
'***********************************************************************************************'
' Fill the services array with the full list of services and loop through the services
'***********************************************************************************************'
Sheets("Final Assignments").Select 'select "Final Assignments" worksheet
ServiceCount = Range("B3", Range("B3").End(xlToRight)).Cells.Count - 1 'set range of "Services" to count
ReDim Services(0 To ServiceCount) 'Redimension the "Services" array
For Columncounter = 0 To ServiceCount
'Services(Columncounter) = Range("B3").Offset(0, Columncounter).Value 'collect the values
Next Columncounter
'***********************************************************************************************'
' Loop through all the services
'***********************************************************************************************'
For Columncounter = 0 To ServiceCount
Sheets("Sorted X1").Select 'select "Sorted X1" worksheet
RangeString = Chr(65 + Columncounter) & "29" 'set the range based on the columncounter
NameCount1 = Range(RangeString, Range(RangeString).End(xlDown)).Cells.Count - 1 'count the number of names for the first date
ReDim SelectedName(0 To NameCount1) 'Redimension the "SelectedName" array
If SelectedName(0) = "" And SelectedName(1) = "" And NameCount1 = 1 Then
For FillArray = 0 To NameCount1 'gather the names
SelectedName(FillArray) = Range(RangeString).Offset(FillArray).Value
Next FillArray
End If
'***********************************************************************************************'
'Now select the first name in the final assignments list
'***********************************************************************************************'
Sheets("Final Assignments").Select 'select "Final Assignments" worksheet
NameCount2 = Range("A4", Range("A4").End(xlDown)).Count - 1 'count the number of "AllNames" in the "Final Assignments"
Range("A3").Select
If NameCount1 < 4 Then
For NameFound = 0 To NameCount2
ActiveCell.Offset(1, 0).Select
For FillArray = 0 To NameCount1
If SelectedName(FillArray) = ActiveCell.Value Then
ActiveCell.Offset(0, Columncounter + 1).Value = "X1" 'Services(Columncounter)
End If
Next FillArray
Next NameFound
End If
Next Columncounter 'increment along the row
End Sub
答案 0 :(得分:2)
问题出在:
RangeString = Chr(65 + Columncounter) & "29" 'set the range based on the columncounter
这部分代码的确可以工作到Z
。这不是一个好的选择。使用Offset
属性来获取正确的地址。例如,以下代码给出了第一行第5000列的地址:
Sub TestMe()
Dim myRange As Range
Dim addCols As Long: addCols = 5000
Set myRange = Worksheets(1).Range("A1")
Debug.Print myRange.Offset(0, addCols).Address
End Sub
或者甚至将新范围作为范围,而无需将其地址分配给变量:
Sub TestMe()
Dim myRange As Range, newRange as Range
Dim addCols As Long: addCols = 5000
Set myRange = Worksheets(1).Range("A1")
Set newRange = myRange.Offset(0, addCols)
Debug.Print newRange.Address
End Sub
答案 1 :(得分:0)
一个快速而肮脏的解决方法是替换此行
RangeString = Chr(65 + Columncounter) & "29"
类似
RangeString = Cells(29, ColumnCounter+1).address(RowAbsolute:=False, ColumnAbsolute:=False)
这应生成RangeString
变量,而不依赖于Chr(
。在此过程中,我将一个添加到您的ColumnCounter
中,因为循环从0开始,小于一列的最小值1。