0

我是一名 DB Guy,我对 VB 一无所知。我在 Excel 中有一个宏,在 Excel 中我有交叉表记录。我的宏会将交叉表记录转换为表格记录。但我的要求是我想在 excel 之外运行相同的宏。.VBS 文件应该在那里,每当我们运行 .VBS 时,它应该从某个地方选择 excel 并将交叉表记录转换为表格记录并保存在某个不同的位置。我通过谷歌搜索创建了相同的代码,有人请查看我的以下代码并帮助我使用正确的代码。

Sub RunMacro()

Dim xlApp 'As Excel.Application 

Dim xlBook 'As Workbook 

Dim xlSheet 'As Worksheet

Dim wsCrossTab 'As Worksheet

Dim wsList 'As Worksheet

Dim iLastCol 'As Long

Dim iLastRow 'As Long

Dim iLastRowList 'As Long

Dim rngCTab 'As Range 'Used for range in Sheet1 cross tab sheet

Dim rngList 'As Range 'Destination range for the list

Dim I 'As Long


Set xlApp = CreateObject("Excel.Application")

Set xlBook = xlApp.Workbooks.Open("D:\Source.xls")

CrossTabToList()

xlBook.SaveAs "D:\Results.xls"

xlApp.Quit

End Sub

Sub CrossTabToList()

Set wsCrossTab = Worksheets("Tabular")

Set wsList = Worksheets.Add


'Find the last row in Sheet1 with the cross tab

iLastRow = wsCrossTab.Cells(Rows.Count, "A").End(xlUp).Row

'Set the initial value for the row in the destination worksheet

iLastRowList = 2

'Find the last column in Sheet1 with the cross tab

iLastCol = wsCrossTab.Range("A8").End(xlToRight).Column

'Create a new sheet and set the heading titles

wsList.Range("A1:C1") = Array("CATEGORY", "SUBCATEGORY", "VALUE")

'Start looping through the cross tab data

For I = 2 To iLastRow

Set rngCTab = wsCrossTab.Range("A" & I) 'initial value A2

Set rngList = wsList.Range("A" & iLastRowList) 'initial value A2

'Copy individual names in Col A (A2 initially) into as many rows as there are data columns in the cross tab (less 1 for Col A).

rngCTab.Copy rngList.Resize(iLastCol - 1)

'Move up a I rows less one and across one column (using offset function) to select heading row. Copy.

rngCTab.Offset(-(I - 1), 1).Resize(, iLastCol - 1).Copy

'Paste transpose to columns in the list sheet alongside the names

rngList.Offset(0,1).PasteSpecial Transpose:=True

'Staying on same row (2 initially) copy the data from the cross tab

rngCTab.Offset(, 1).Resize(, iLastCol - 1).Copy

'Past transpose as column in list sheet

rngList.Offset(0, 2).PasteSpecial Transpose:=True

'Set the new last row in list sheet to be just below the last name copied

iLastRowList = iLastRowList + (iLastCol - 1)

'increment I by 1

 Next I


Application.DisplayAlerts = False

Sheets("Tabular").Select

ActiveWindow.SelectedSheets.Delete

Application.DisplayAlerts = True

Sheets("Sheet1").Select

Sheets("Sheet1").Name = "Results"

objwkbk.SaveAs "D:\Results.xls"


End Sub

谢谢,

普拉文


正如我提到的,我不是 Java 开发人员或编码人员,我是数据库人员,我对 Java 一无所知。我想将上面的代码用作 .VBS 文件。我希望有人更正我上面的代码以使用它一个 .VBS 文件。如果你能做到,我们将不胜感激。提前致谢。

4

1 回答 1

0

这是一个非常好的主意。Excel 文件中的 VBA 可能会使用户感到困惑,因此我尽量避免这种情况。

我建议将您的过程存储在 Access 文件中。转换它涉及一些工作,但这应该让你开始:

  1. 创建一个新的访问数据库
  2. 在您的新数据库中,创建一个新的 VBA模块。将您的代码粘贴到那里。
  3. 添加最新版本的Microsoft Excel 对象库
  4. 进行任何其他必要的更改以使代码再次正常工作(您必须进行一些试验和错误。重复运行代码并在错误消息弹出时处理它们)
  5. 将您的Sub更改为函数(您需要这样做才能从宏中调用它)
  6. 制作一个新的。使用参数RunMacro()添加操作RunCode

以后,您所要做的就是打开数据库并单击宏以运行代码。

于 2012-11-26T16:59:38.637 回答