我正在尝试创建一个工作表,该工作表将根据单元格的值创建列。因此,如果我输入10,将创建10列。我能够做到这一点,但是,我希望从不同的工作表中提取范围。我不确定这是否可能,如果可能,如何做到这一点。如果不是,有没有办法在我进入特定的工作表后刷新工作表,以便我可以将该值链接到需要它的每个工作表?我将拥有多张工作表,这些工作表将依赖于该值来显示信息。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range, ColNum As Long, TotalCol As Long, LeftFixedCol As Long
Dim Rng As Range, c As Range
Set KeyCells = Range("B2")
If Application.Intersect(KeyCells, Target) Is Nothing Then Exit Sub
If IsNumeric(KeyCells.Value) = False Then Exit Sub
ColNum = KeyCells.Value
If ColNum <= 0 Then Exit Sub
Set Rng = Range(Cells(4, 1), Cells(4, Columns.Count))
Set c = Rng.Find("END")
If c Is Nothing Then Exit Sub
TotalCol = c.Column
LeftFixedCol = 1
Dim i As Integer
If TotalCol < LeftFixedCol + ColNum + 1 Then
For i = TotalCol To LeftFixedCol + ColNum
Columns(i).Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(4, i).Value = "Member" & i - LeftFixedCol
Cells(5, i).Value = "=DATA!$A$2"
Cells(6, i).Value = "=OFFSET(DATA!$C$2,COLUMN()-2,0)"
Cells(7, i).Value = "=OFFSET(DATA!$D$2,COLUMN()-2,0)"
Cells(8, i).Value = "=OFFSET(DATA!$E$2,COLUMN()-2,0)"
Cells(10, i).Value = "=OFFSET(DATA!$F$2,COLUMN()-2,0)"
Cells(12, i).Value = "=OFFSET(DATA!$G$2,COLUMN()-2,0)"
Cells(13, i).Value = "=OFFSET(DATA!$H$2,COLUMN()-2,0)"
Cells(14, i).Value = "=OFFSET(DATA!$I$2,COLUMN()-2,0)"
Next i
End If
If TotalCol > LeftFixedCol + ColNum + 1 Then
For i = TotalCol - 1 To LeftFixedCol + ColNum + 1 Step -1
Columns(i).Delete
Next i
End If
End Sub
发布于 2021-08-11 07:11:27
我不确定我是否正确理解了您的问题,但您可以在使用VBA命令时引用另一个工作表,例如:
Worksheets("Sheet1").Cells(5, 3).Font.Size = 14
您也可以通过索引来执行此操作:
Worksheets(1).Cells(5, 3).Value = 2
这样,您就可以从不同的工作表中提取范围。
https://stackoverflow.com/questions/68744499
复制相似问题