Excel VBA怎样为变量赋值

Excel VBA怎样为变量赋值,第1张

将代码修改为:
function
x(a
as
string)
as
integer
if
a
=
">第二句代码写反了。
Dim jd1 As String
jd1 = Worksheets("sheet1")Range("A1")Value
TextBox1Text = jd1
注意:要点击textbox1,选择“属性”,“MultiLine"设置成True。

对于你这个问题,
Dim aa$
SelectionMoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
aa = Selection
如果要通用的话就这样
在VBE中,引用 Microsoft Forms 20 工程库
之后,使用如下代码:
Dim MyData As DataObject, MyStr As String
Set MyData = New DataObject
MyDataGetFromClipboard '获得剪切板内容
MyStr = MyDataGetText '赋值给变量
msgbox mystr

Set rng = ApplicationInputBox("请用鼠标选择单元格范围:", "选择区域", , , , , , 8)
rngCopy [f1]
假设 数据选定的区域,复制到F1

数组的长度可以用变量表示,比如下面代码,就可以先定义一个动态数组,然后根据一个算式来确定数组的大小,再给它赋值
Sub abc()
Dim a() As Integer
n = 0
'写入动态数组
x = 3 4
ReDim a(0 To x) '根据表达式计算X的值来确定数组大小
For i = 1 To 3
For j = 2 To 5
a(n) = i + j: n = n + 1
Next
Next
'从动态数组取数
For i = 0 To n
Cells(i + 1, 1) = a(i)
Next
End Sub

首先第一列必须设置成文本格式,然后运行这段代码,选择你要的文件,即可生成你要的要求。
Sub 宏1()
yourFile = ApplicationGetOpenFilename '选择你要导入的TXT
Open yourFile For Input As #1
a = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
Close #1
k = ApplicationWorksheetFunctionText(2, "000000000")
For i = 1 To UBound(a)
Cells(i, 1) = ApplicationWorksheetFunctionText(i, "000000000")
Cells(i, 2) = a(i)
Next
End Sub

注释的!
Private Sub CommandButton5_Click()
Dim Conn As New ADODBConnection '定义ADODB连接对象
Dim ConnStr As String '定义连接字符串
Dim xiao As String

xiao = ComDataText
'对于SQL 2008,如果以IP方式访问服务器,必须要启动SQL Server Browser服务,如果是Express版本,要以“IP(计算机名)\实例名”方式访问
ConnStr = txtDataText
Dim Records As New ADODBRecordset '定义ADODB对象的记录集
Dim Sheet As Worksheet '定义工作表
Set Sheet = ThisWorkbookWorksheets("Sheet2") '给变量Sheet赋值,注意:是工作簿中索引为1的那个表(通常一个新的工作簿Sheet1的索引为1)
'SheetName = "Data" '把Sheet名称改为Data
SheetCellsClear '清空表中原有的数据
'连接状态是打开就不在进行Open *** 作
ConnOpen ConnStr
Dim SQLStr As String '要执行的SQL语句
SQLStr = "select from Shift_Code where Club='" + xiao + "'" '可以执行更复杂的SQL语句
RecordsOpen SQLStr, Conn, adOpenStatic, adLockBatchOptimistic '读取SQL查询结果到Records记录集
'RecordsOpen
Dim i, j, TotalRows, TotalColumns As Integer
j = 0
TotalRows = RecordsRecordCount
TotalColumns = RecordsFieldsCount
'下面的循环把表头(即列名)写到Excel表的第一行
For i = 0 To TotalColumns - 1
Sheet2Cells(1, i + 1) = RecordsFields(i)Name
Next

'下面的循环把查询结果写到Excel表中
Do While Not RecordsEOF
For i = 0 To TotalColumns - 1
Sheet2Cells(j + 2, i + 1) = RecordsFields(i)Value
Next

RecordsMoveNext
j = j + 1

Loop
RecordsClose '关闭记录集
ConnClose '关闭连接
Set Records = Nothing '清空对象
Set Conn = Nothing '清空对象
End Sub
有什么不懂的话Hi我!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13395496.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-28
下一篇 2023-07-28

发表评论

登录后才能评论

评论列表(0条)

保存