2012-11-26 79 views
0

Iam a DB Guy和我不知道VB的任何內容。 我有一個宏在Excel和Excel中我有交叉表格記錄。 我的宏會將Crosstabular記錄轉換爲表格記錄。 但我的要求是我想在Excel以外運行相同的宏。 .VBS文件應該在那裏,每當我們運行.VBS時,它應該從某個地方選擇excel,並將交叉表記錄轉換爲表格記錄並保存在某個不同的位置。 我已經創建了一個代碼相同的谷歌搜索和有人請檢閱我的下面的代碼,並幫助我與正確的代碼。如何在不使用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文件中使用它。如果你可以這樣做,它將非常感激。 在此先感謝。

+0

當您嘗試運行此代碼會發生什麼?什麼錯誤信息(如果有的話),你會得到什麼。當你運行Excel時,Excel如何運作? –

回答

0

這是一個非常好的主意。 Excel文件中的VBA可能會讓用戶感到困惑,因此我儘可能避免這種情況。

我建議將您的過程存儲在Access文件中。有參與其轉換一點的工作,但是這應該讓你開始:

  1. 讓DB
  2. 一個新的Access在您的新的數據庫,使一個新的VBA 模塊。將代碼粘貼在那裏。
  3. 添加最新版本的Microsoft Excel對象庫
  4. 做出各種其他的變化是必要的獲得再次合作的順序碼(你必須做一些試驗和錯誤的,與錯誤信息反覆運行代碼和處理,因爲他們彈出)
  5. 變化您功能(你需要做這從宏觀稱呼它)
  6. 創建一個新的。添加動作RunCode與參數RunMacro()

在未來,所有你需要做的就是打開數據庫,並單擊宏運行的代碼。

+0

正如我所提到的,我不是Java開發人員或編碼人員,我是一個數據庫人員,我不知道任何有關Java的信息。我想將上面的代碼用作.VBS文件。我想要有人更正我的上述代碼以使用它在一個.VBS文件。如果你能做到這一點,將非常感激。提前致謝。 – praveen1108

+0

啊,在這種情況下,我不能有太大的幫助。祝你好運。 – PowerUser

相關問題