ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 957|回复: 8

vba 如何向一列连续的单元格写入文本

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-2-22 15:20 | 显示全部楼层 |阅读模式
比如向一行连续的单元格写入文件,可以这样实现:
Sub aaa()
Range("L20:N20") = [{"aa","bb","cc"}]
End Sub


但向一列写入这样是不行的:
Sub aaa()
Range("A1:A3") = [{"aa","bb","cc"}]
End Sub


可以这样实现但太麻烦了:
Sub aaa()
Range("A1") ="aa"
Range("A2") ="bb"
Range("A3") ="cc"
End Sub


简单点的写法应该是什么呢。。不会了。

TA的精华主题

TA的得分主题

发表于 2020-2-22 15:25 | 显示全部楼层
Range("A1:A3") = Application.Transpose([{"aa","bb","cc"}])

TA的精华主题

TA的得分主题

发表于 2020-2-22 15:34 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2020-2-22 15:39 | 显示全部楼层
不知道是否这样可以
Sub text1()
    Dim arr, i As Long
    arr = Array("aa", "bb", "cc")   '此数组下标从0开始
    For i = 1 To UBound(arr) + 1
        Range("a" & i) = arr(i - 1) '从A1开始依次写入数组的值
    Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-2-22 15:41 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
mmwwdd 发表于 2020-2-22 15:25
Range("A1:A3") = Application.Transpose([{"aa","bb","cc"}])

谢谢。会者不难啊。。

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-2-22 16:47 | 显示全部楼层
一把小刀闯天下 发表于 2020-2-22 15:34
Sub aaa()
  Range("A1:A3") = [{"aa";"bb";"cc"}]
End Sub

谢谢了,麻烦帮看看,这个可以继续用for 简化写法吗?

Sub asdf()
   
    For i = 2 To 10
        Cells(6, (i - 1) * 2) = 29
        If (Cells(6, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(6, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(6, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(7, (i - 1) * 2) = 27
        If (Cells(7, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(7, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(7, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(8, (i - 1) * 2) = 25
        If (Cells(8, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(8, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(8, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(9, (i - 1) * 2) = 23
        If (Cells(9, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(9, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(9, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(10, (i - 1) * 2) = 21
        If (Cells(10, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(10, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(10, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(11, (i - 1) * 2) = 11
        If (Cells(11, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(11, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(11, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(12, (i - 1) * 2) = 13
        If (Cells(12, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(12, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(12, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
    Next
End Sub

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-2-22 16:49 | 显示全部楼层
一把小刀闯天下 发表于 2020-2-22 15:34
Sub aaa()
  Range("A1:A3") = [{"aa";"bb";"cc"}]
End Sub

谢谢了,再帮看看,这个如何继续用for 简化,这个颜色对比应该可以写的,写不出来。。。


Sub asdf()
   
    For i = 2 To 10
        Cells(6, (i - 1) * 2) = 29
        If (Cells(6, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(6, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(6, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(7, (i - 1) * 2) = 27
        If (Cells(7, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(7, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(7, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(8, (i - 1) * 2) = 25
        If (Cells(8, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(8, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(8, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(9, (i - 1) * 2) = 23
        If (Cells(9, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(9, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(9, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(10, (i - 1) * 2) = 21
        If (Cells(10, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(10, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(10, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(11, (i - 1) * 2) = 11
        If (Cells(11, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(11, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(11, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
        Cells(12, (i - 1) * 2) = 13
        If (Cells(12, (i - 1) * 2) > Cells(3, (i - 1) * 2)) Then
            Cells(12, (i - 1) * 2).Font.Color = RGB(255, 0, 0)
        Else
            Cells(12, (i - 1) * 2).Font.Color = RGB(0, 255, 0)
        End If
        
    Next
End Sub

TA的精华主题

TA的得分主题

发表于 2020-2-22 16:54 | 显示全部楼层
ldflmh 发表于 2020-2-22 16:47
谢谢了,麻烦帮看看,这个可以继续用for 简化写法吗?

Sub asdf()

应该可以,上附件,然后作一下简单说明,最好有图示

重写比修改别人的代码大多数时候要快得多,,,

评分

1

查看全部评分

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-2-22 17:20 | 显示全部楼层
一把小刀闯天下 发表于 2020-2-22 16:54
应该可以,上附件,然后作一下简单说明,最好有图示

重写比修改别人的代码大多数时候要快得多,,,

附件在我新发的帖子里了
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-20 06:20 , Processed in 0.050666 second(s), 16 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表