![](/img/trans.png)
[英]How to sort rows in a excel sheet and print to a file using VB script?
[英]How to do single button action for multi sheet (tabs) excel using VB script?
我是 VB 脚本的初学者,我有一个带有多选项卡的 excel 表。 我在一张 Excel 表中创建了一个按钮。 另一张有一些桌子的床单。 我想使用一个简单的按钮在带有这些 excel 表的文件中生成一些代码。
例如 :
这本 excel 书有一个名为 Generate 的选项卡。 在 Generate 中,我创建了一个按钮。
我有另一个名为 country 的选项卡,其中包含一个国家/地区列表表格
我有另一个名为 car 的选项卡,其中包含一个汽车列表表
现在我想创建一个文件“output.txt”,当单击“生成代码”按钮时,它应该使用来自两个选项卡(国家和车辆)的一些代码创建。
我的output.txt格式:
*from sheet1 Country*/
VAR const US[] =
{
0x0,/*binary 00000*/
0xB,/*binary 01011*/
0x3,/*binary 00011*/
0x3,/*binary 00011*/
0xB,/*binary 01011*/
1xB /*binary 11011*/
};
//need to crate hexa array for Uk,france,brazil and india
VAR DefaultCountry[] =
{
invalid,
UK,
Brazil,
Brazil,
UK,
India
};
/* from sheet2 car */
VAR const polo[] =
{
};
//need to crate hexa array for BMW,i20,Swift and wagnor
VAR DefaultCAR[] =
{
invalid,
BMW,
Swift,
Swift,
BMW,
Wagnor
}
excelsheet.txt格式:
const exceldetails[Maxindex] =
{
/* index 0 */
/* index 1 */
{
{ UK, India, brazil,eMaxNoOfcountry, eMaxNoOfcountry},
{BMW, Wagnor, Swift,eMaxNoOfcar,eMaxNoOfcar },
index1,
},
/*index 2*/
etc..
};
如果列有 "-" ,它应该取 0 值,如果它是整数,它应该取 1 值并打印六进制值和二进制值
例如:对于国家索引 0:- - - - - => 二进制:00000 => 六:0x0 索引 1:- 1 - 3 2 => 二进制:01011 => 六:0xB
数组名称: VAR const US[],france 等.. VAR const polo[],swift 等..
如果列包含 1 ,则为默认值并打印数组中每个索引的默认值列名
数组名称: VAR DefaultCountry[],VAR DefaultCAR[])
创建另一个文件“exceldetails.txt”并将每个国家的顺序和汽车详细信息写入数组。 如果出现“-”则作为 eMaxNoOf。
数组名称: exceldetails[Maxindex])
这该怎么做 ? 有什么帮助吗? 任何参考也有帮助。 如何使用一键从多选项卡中获取表值?
我给你一个小片段代码,它不能回答你所有的问题,但只有当你将十进制值转换为二进制和十六进制时。 我不明白你想要什么
Sub test()
'binary code and hex code
With Application.WorksheetFunction ' with this row you can use the functions Dec2Bin, Dec2Hex
'convert decimal in binary
Cells(1, 1) = .Dec2Bin(Cells(1, 2)) ' input 3 -> out: 11
'convert binary in hex
Cells(2, 1) = .Dec2Hex(Cells(2, 2)) ' input 11 -> out B
End With
End Sub
这是您可以在其中创建和写入文件 txt 的片段代码
Sub test()
'create and write into file txt
'when you execute again the macro the file is overwritten
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim Fileout As Object
Set Fileout = fso.CreateTextFile("yourPath\MyFile.txt", True, True)
Fileout.Write "your string goes here"
Fileout.Close
End Sub
我创建了两个宏,因此您可以尝试每个宏。 您必须创建一个唯一的宏,其中包含所有代码...
希望这可以帮助
编辑以回答您的评论如果您想在下面的工作表中工作,则有一个示例
Sub test()
Dim sh1, sh2 As Worksheet
Dim i, r, numRows, numColumns As Long
'set sh1 and the works it
Set sh1 = Sheets("Country") ' sheet name
'count how many rows there are into sheet1
'numRows = sh1.Range("A:A").Cells.SpecialCells(xlCellTypeContants).Count
numRows = sh1.Cells(Rows.Count, 1).End(xlUp).Row
'MsgBox numRows
'count how many columns there are into sheet1
numColumns = sh1.Cells(1, Columns.Count).End(xlToLeft).Column
'MsgBox numColumns
With sh1
For j = 2 To numColumns
For i = 2 To numRows - 1
If .Cells(i, j) = "-" Then
'msgbox "the item into cell is empity: "
'you code..
Else
'msgbox "the item into cells is: " & .cells(i,j)
'your code...
End If
Next i
Next j
End With
'----repeat operation for the sheet2
'set sh2 and the works it
Set sh2 = Sheets("car") ' sheet name
'count how many rows there are into sheet2
numRows = sh2.Cells(Rows.Count, 1).End(xlUp).Row
'MsgBox numRows
'count how many columns there are into sheet2
numColumns = sh2.Cells(1, Columns.Count).End(xlToLeft).Column
'MsgBox numColumns
With sh2
For j = 2 To numColumns
For i = 2 To numRows - 1
If .Cells(i, j) = "-" Then
'msgbox "the item into cell is empity: "
'you code..
Else
'msgbox "the item into cells is: " & .cells(i,j)
'your code...
End If
Next i
Next j
End With
End Sub
有两个 for 循环,因为一个使用列,另一个使用行......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.