【问题标题】:Creating worksheet change with KeyCells from different worksheet使用来自不同工作表的 KeyCells 创建工作表更改
【发布时间】:2021-08-11 23:11:27
【问题描述】:

我正在尝试创建一个工作表,该工作表将根据单元格的值创建列。因此,如果我输入 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

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    不确定我是否正确地遵循了您的问题,但您可以在使用 VBA 命令时参考另一个工作表,例如:

    Worksheets("Sheet1").Cells(5, 3).Font.Size = 14
    

    你也可以通过索引来做到这一点:

    Worksheets(1).Cells(5, 3).Value = 2
    

    这样您可以从不同的工作表中提取范围。

    【讨论】:

      猜你喜欢
      • 2022-07-15
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多