热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在excelVBA中使用变量-UseofvariablesinexcelVBA

NewtoexcelVBA.Iknowthefollowingisincorrectasx&iwillgiveastring,notavariabl

New to excel VBA. I know the following is incorrect as "x" & i will give a string, not a variable. But suppose I have 100 xs and I want to populate into cells. I do not want to put all x values in an array and later call from the array. Is there a quick and easy way to fix the following?

擅长VBA的新手。我知道以下是不正确的“x”&我将给出一个字符串,而不是一个变量。但假设我有100 xs,我想填入细胞。我不想将所有x值都放在一个数组中,然后再从数组中调用。有没有快速简便的方法来解决以下问题?

Sub test()
    x1 = 1
    x2 = 2
    x3 = 3
    For i = 1 To 3
        Cells(i, 1) = "x" & i
    Next
End Sub

1 个解决方案

#1


0  

Re-consider using arrays:

重新考虑使用数组:

Sub test()
    x = Array(1, 2, 3)
    For I = 0 To 2
        Cells(I + 1, 1) = x(I)
    Next I
End Sub

is easy and compact.

简单而紧凑。

EDIT:

Sub test()
    x = Array(1, 2, 3)
    Range("A1:A3") = Application.WorksheetFunction.Transpose(x)
End Sub

is even quicker

甚至更快


推荐阅读
author-avatar
要做转播达人
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有