簡體   English   中英

如何使用VB腳本對多表(標簽)excel進行單按鈕操作?

[英]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..
    };
  1. 如果列有 "-" ,它應該取 0 值,如果它是整數,它應該取 1 值並打印六進制值和二進制值

    例如:對於國家索引 0:- - - - - => 二進制:00000 => 六:0x0 索引 1:- 1 - 3 2 => 二進制:01011 => 六:0xB

    數組名稱: VAR const US[],france 等.. VAR const polo[],swift 等..

  2. 如果列包含 1 ,則為默認值並打印數組中每個索引的默認值列名

    數組名稱: VAR DefaultCountry[],VAR DefaultCAR[])

  3. 創建另一個文件“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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM