我想复制标题子集的改编版本'使用VBA将一个工作簿排成一个新工作簿。
最初,我的想法是:
由于我无法找到一种可以容忍的方法来消除和添加数组中的条目,我可能会更好:
我对VBA几乎没有任何经验,但这样一个简单的操作似乎是一场噩梦。
我的尝试如下:
' Initialization
Dim wbkOld As Workbook
Dim wbkNew As Workbook
Set wbkOld = Workbooks.Open(Filename:="filepath", ReadOnly:=True)
Set wbkNew = Workbooks.Add
wbkNew.SaveAs Filename:="filename"
' STEP 1: Only read wanted sections of the original header
' I know the size of the target headers row beforehand
Dim strHeadersArr(1 To 6) As String
' Now the problem:
' The following statement would work if I wanted to load the entire headers row...
strHeadersArr = wbkOld.Sheets(1).UsedRange.Rows(1).Value
'... but I want to load only a subsection
' (e.g. the first 4 headers/cells and then the last 2 headers/cells, dropping the 5 headers/cells in between)
_unknown code here_
' STEP 2: Rename the needed entries
strHeaderArr(2) = "newColumnName"
' STEP 3: Write down
wkbNew.Sheets(1).Range("A1").Value = strHeaderArr
如果您提出了一种不同的/更好的方法来完成我正在寻找的内容,也会受到赞赏。