我正在寻找一种方法来迭代工作表中的单元格以粘贴值。
下面发布的代码无法识别单元格位置。有没有办法循环单元格位置以逐步将信息粘贴到所需的空间?我希望代码将值粘贴到A1:C1中,然后移动4个空格并发布到E1:G1,依此类推。
不确定如何迭代字母以移动单元格。
Sub test()
Dim x As Integer
Dim y As Integer
Dim InputSheet As Worksheet
Dim myBook As Workbook
Set myBook = Excel.ActiveWorkbook
Set InputSheet = myBook.Sheets("InputSheet")
For y = 0 To 5
x = 4
InputSheet.Range("A1 + 4*y : C1 + 4*y").Value = x
Next y
End Sub
答案 0 :(得分:2)
我会尝试使用“细胞”来做到这一点。
Sub test()
Dim x As Integer
Dim y As Integer
Dim InputSheet As Worksheet
Dim myBook As Workbook
Set myBook = Excel.ActiveWorkbook
Set InputSheet = myBook.Sheets("InputSheet")
For y = 0 To 5
x = 4
InputSheet.Range(InputSheet.Cells(1, 4 * y + 1), InputSheet.Cells(1, 4 * y + 3)).Value = x
Next y
End Sub
答案 1 :(得分:2)
您可以使用偏移功能。
import java.util.Scanner;
class Game {
protected static Code winningCode;
public static void main(String[] args){
}
public Game(){
winningCode = new Code();
}
protected static Code getGuess() {
Scanner userInput = new Scanner(System.in);
int count = 0;
int maxTries = 5;
while(true){
try {
String codeToParse = userInput.next();
Code guess = Code.parse(codeToParse);
return guess;
} catch(RuntimeException notACode) {
System.out.println("That's not a valid peg. You have " + (maxTries - count) + " tries left.");
if (++count == maxTries) throw notACode;
}
}
}
protected static void displayMatches(Code guess){
int nearMatches = winningCode.countNearMatches(guess);
int exactMatches = winningCode.countExactMatches(guess);
System.out.println("You have " + exactMatches + " exact matches and " + nearMatches + " near matches.");
}
protected static void play(){
int turnCount = 0;
int maxTurns = 10;
System.out.println("Greetings. Pick your code of four from Y,O,G,P,C,R.");
while(true){
Code guess = getGuess();
displayMatches(guess);
if (guess == winningCode) {
System.out.print("You win!!");
break;
} else if (++turnCount == maxTurns) {
System.out.print("You lose!!");
break;
}
}
}
}
答案 2 :(得分:1)
你告诉工作簿确切地寻找范围" A1 + 4 * y:C1 + 4 * y"这显然不是一个有效的地址。您需要评估字符串外部的数字表达式,转换回字符串(显式使用Cstr
或者您可以将表达式放在括号中,让编译器为您执行此操作,因为VBA是动态类型的,这是一个重要的您可能想要查找的概念。基本上它能够从上下文中找出它处理字符串类型变量的方法)并最终将其恢复到您的地址以使其工作。
由于您似乎是vba /编码的新手,我建议您弄清楚如何使用断点和监视来查看您的机器如何实际评估您的变量。
InputSheet.Range("A" & (1+4*y) & ":C" & (1+4*y)).Value = x
答案 3 :(得分:0)
我做了以下代码,因为我也遇到了一些运行时错误。希望它有所帮助。
Dim wb As Workbook
Dim ws As Object
Set wb = Workbooks("Libro2")
Set ws = wb.Sheets("Hoja1")
For i = 1 To 10000
Dim strrangoa As String
Dim strrangob As String
'Creating a string variable replaces selecting the cell
strrangoa = "A" & i
strrangob = "B" & i
'If the active cell does not have data, it exits the loop
If ws.Range(strrangoa).Value = "" Then
GoTo Salir
Else
'The data from cells in Column A are passed to Column B
ws.Range(strrangob).Value = ws.Range(strrangoa).Value
End If
Next
Salir:
End Sub
答案 4 :(得分:0)
好的,此处的所有其他答案都未能使用.Resize()
函数来选择推荐方式的单元格范围。
Option Explicit
Sub Test()
Dim x As Integer
Dim y As Integer
Dim InputSheet As Worksheet
Dim myBook As Workbook
Set myBook = Excel.ActiveWorkbook
Set InputSheet = myBook.Sheets("InputSheet")
For y = 0 To 5
x = 4
' Start for "A1", move down 4*y rows, and select
' one row and 3 columns to set the value
InputSheet.Range("A1").Offset(4*y, 0).Resize(1, 3).Value = x
' Similar to using the `.Cells()` method
' InputSheet.Range("A1").Cells(4*y+1, 1).Resize(1, 3).Value = x
Next y
End Sub
PS。你也可以做像
这样的事情InputSheet.Range("A2").Resize(1,3).Value = Array(1,2,3)
将单元格A2,B2,C2设置为1,2,3
或
InputSheet.Range("A2").Resize(3,1).Value = _
WorksheetFunction.Transpose(Array(1,2,3))
将单元格A2,A3,A4设置为1,2,3
阅读Microsoft的 here ,了解如何使用.Resize()
,.Offset()
和.Cells()
。