首页 > 解决方案 > 如何使用 VB 脚本对多表(选项卡)excel 执行单按钮操作?

问题描述

我是 VB 脚本的初学者,我有一个带有多选项卡的 excel 表。我在一张 excel 表中创建了一个按钮。另一张有一些桌子的床单。我想使用一个简单的按钮在带有这些 excel 表的文件中生成一些代码。

例如 :

这本 excel 书有一个名为 Generate 的选项卡。在 Generate 我创建了一个按钮。

在此处输入图像描述

我有另一个名为 country 的选项卡,其中包含一个国家列表表

在此处输入图像描述

我有另一个选项卡也命名为汽车,其中包含汽车列表表

在此处输入图像描述

现在我想创建一个文件“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..
    };
  1. 如果该列有“-”,它应该取 0 值,如果它是整数,它应该取 1 值并打印十六进制值和二进制值

    例如:对于国家索引 0 : - - - - - => 二进制 : 00000 => hexa : 0x0 索引 1 : - 1 - 3 2 => 二进制 : 01011 => hexa : 0xB

    数组名称: VAR const US[],france etc.. VAR const polo[],swift etc..

  2. 如果列包含 1 ,则为默认值,并为数组中的每个索引打印默认值列名

    数组名称: VAR DefaultCountry[],VAR DefaultCAR[])

  3. 创建另一个文件“exceldetails.txt”并将每个国家的顺序和汽车详细信息写入数组。如果“-”出现,则作为 eMaxNoOf。

    数组名称: exceldetails[Maxindex])

这个怎么做 ?有什么帮助吗?任何参考也有帮助。如何使用一个按钮从多个选项卡中获取表格值?

标签: excelvbabasic

解决方案


我给你一个小片段代码,它不能回答你所有的问题,但只有当你将十进制值转换为二进制和十六进制时。我不明白你想要什么

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 循环,因为一个使用列,另一个使用行...


推荐阅读