vba编程 变量赋值单元格
发布网友
发布时间:2022-05-14 16:00
我来回答
共4个回答
热心网友
时间:2023-08-22 10:34
sub aa()
dim x as string
x="资产"
cells(m,n)=x 'm代表行标,m代表列标,根据不同情况可以用数字代替。
end sub
如果不是给当前工作表赋值,可以用以下格式
Sheets("Sheet1").range("B2")=100 '把100赋值给sheet1工作表中的B2单元格
也可以给某一个区域赋值:
range("A1:A100").select
Selection=100
Selection就可以标识当前选中的单元格,上面2句代码是将当前选中的单元格写入100.
程序可改为下面代码
Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim i As Integer
Dim j As Integer
Dim zcbh As Variant
Dim ytzj As Variant
Dim yjzj As Variant
Dim sh As Worksheet
For i = 3 To 10
zcbh = Sheets("8").Cells(i, 3)
Sheets("6").Activate
Sheets("6").Columns(2).Find(what:=zcbh).Activate
ActiveCell.Offset(0, 5).Select
yjzj = ActiveCell.Value
Sheets("8").Activate
Sheets("8").Cells(i, 8).Value = ytzj
Next i
MsgBox "程序运行完毕"
Application.ScreenUpdating = True
End Sub
修改为以下代码:
Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim i As Integer
Dim j As Integer
Dim zcbh As Variant
Dim ytzj As Variant
Dim yjzj As Variant
Dim sh6 As Worksheet
Dim sh8 As Worksheet
Set sh6 = ThisWorkbook.Sheets("sheet6")
Set sh8 = ThisWorkbook.Sheets("sheet8")
For i = 3 To 10
zcbh = sh8.Cells(i, 3)
sh6.Activate
sh6.Columns(2).Find(what:=zcbh).Activate
ActiveCell.Offset(0, 5).Select
ytzj = ActiveCell.Value
sh8.Cells(i, 8) = ytzj
Next i
MsgBox "程序运行完毕"
Application.ScreenUpdating = True
End Sub
热心网友
时间:2023-08-22 10:34
Sheets(1).Cells(X, Y) = 变量 'sheet(1)代表第一个工作表。Cells(X, Y)代表X行Y列单元格。
或
Sheets(1).Range("A1") = 变量 'Range("A1") 代表"a1"单元格。
热心网友
时间:2023-08-22 10:35
Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim i As Integer
Dim j As Integer
Dim zcbh As Variant
Dim ytzj As Variant
Dim sh As Worksheet
For i = 3 To 10
zcbh = Sheet8.Cells(i, 3).Value
Sheet6.Activate
Sheet6.Columns(2).Find(what:=zcbh).Activate
ActiveCell.Offset(0, 6).Select '第2列向后偏移6列,即H列
ytzj = Selection.Value
Sheet8.Activate
Sheet8.Cells(i, 8).Value = ytzj
Next i
MsgBox "程序运行完毕"
Application.ScreenUpdating = True
End Sub
热心网友
时间:2023-08-22 10:35
cells(x,y).value=变量
或者range("A1").value=变量