如何使用VB脚本为多页(选项卡)excel执行单键操作?

如何使用VB脚本为多页(选项卡)excel执行单键操作?,excel,vba,basic,Excel,Vba,Basic,我是一个VB脚本的初学者,我有一个带有多选项卡的excel工作表。我在一张excel表格中创建了一个按钮。还有一张桌子。我想用一个简单的按钮在一个包含excel表格的文件中生成一些代码 例如: 此excel手册有一个名为“生成”的选项卡。在生成中,我创建了一个按钮 我有另一个名为country的选项卡,其中包含一个国家列表表 我有另一个选项卡,也叫car,它包含一个car列表表 现在我想创建一个文件“output.txt”,当单击GenerateCode按钮时,该文件应该使用两个选项卡(国

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

例如:

此excel手册有一个名为“生成”的选项卡。在生成中,我创建了一个按钮

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

我有另一个选项卡,也叫car,它包含一个car列表表

现在我想创建一个文件“output.txt”,当单击GenerateCode按钮时,该文件应该使用两个选项卡(国家和车辆)中的一些代码创建

Myoutput.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
}   
const exceldetails[Maxindex] = 
       {
         /* index 0 */
         /* index 1 */
        { 
         { UK, India, brazil,eMaxNoOfcountry, eMaxNoOfcountry},
          {BMW, Wagnor, Swift,eMaxNoOfcar,eMaxNoOfcar },
          index1,
        },
         /*index 2*/
         etc..
    };
excelsheet.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
}   
const exceldetails[Maxindex] = 
       {
         /* index 0 */
         /* index 1 */
        { 
         { UK, India, brazil,eMaxNoOfcountry, eMaxNoOfcountry},
          {BMW, Wagnor, Swift,eMaxNoOfcar,eMaxNoOfcar },
          index1,
        },
         /*index 2*/
         etc..
    };
  • 如果列有“-”,则应取0值,如果为整数,则应取1值,并同时打印六进制值和二进制值

    例如:对于国家索引0:-=>binary:00000=>hexa:0x0 索引1:-1-32=>binary:01011=>hexa:0xB

    数组名称:VAR const US[],法国等。。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循环,因为一个使用列,另一个使用行…

    您到底有什么问题?现在忘了按扭,你还能做其他事情吗?是的。我想知道如何遍历每个选项卡(工作表)表谢谢你的回答。请您解释一下如何从每个表中获取数据。例如,country和car是包含表格的选项卡(表)。@user2986042我已经编辑了我的答案,希望这能有所帮助