【发布时间】:2018-08-21 01:14:51
【问题描述】:
我正在尝试编写一个代码,该代码将获取标题/名称列表,并为每个标题/名称创建一个选项卡,每个工作表都有一个列表中的名称。 例如,给定 ActiveSheet 上的一个表(可能不一定是 sheet1)
Metric | Comments | Title
1 | testing1 | This is Metric1
2 | testing2 | This is Metric2
我想在 ActiveSheet 之后添加 2 个工作表,分别命名为“This is Metric1”和“This is Metric2”(理想情况下,我想用“testing1”填充每个新工作表的单元格 A1 " 和 "testing2" 也一样——在我们跑之前必须先走)。我对 VBA 还是比较陌生,所以请暴露我的错误代码 - 这是我迄今为止尝试过的:
Sub test_tableTOtabs()
Dim fr As Integer
Dim lr As Integer
Dim col As String
fr = Application.InputBox("Starting row of data: ", , 2)
lr = Application.InputBox("Last row of data: ")
col = Application.InputBox("Column for Tab titles: ")
Dim BaseSheet As Worksheet
Set BaseSheet = ActiveSheet
Dim i As Integer
Dim TitleCell As String
Dim title As String
Dim ws As Worksheet
For i = fr To lr
Set TitleCell = col & CStr(i)
title = ActiveSheet.Range("TitleCell").Value
Set ws = Sheets.Add(After:=Sheets(Worksheets.Count))
ws.Name = title
Worksheets(BaseSheet).Activate
Next
End Sub
我知道我可能过于复杂了,但我不确定如何完成 - 请帮助!
【问题讨论】:
-
你的问题到底是什么?
-
如果人们不了解问题,他们将无能为力。请使用编辑来指定您的问题。
标签: vba excel excel-formula excel-2007 excel-tables